hrsh7th / cmp-path

nvim-cmp source for path
MIT License
599 stars 55 forks source link

Trailing slashes in cmdline? #40

Closed PowerUser64 closed 2 years ago

PowerUser64 commented 2 years ago

I have noticed that quite frequently I will get trailing slashes appended to the completions.


Steps to reproduce:

  1. Type the name of a directory and press tab

Notice there is a slash appended to the result

image

  1. Type a slash to get cmp-path to read from the next directory

But wait, now it's trying to complete from the root directory?

image

  1. Press backspace to remove the slash

Wait, now there are two of every file

image


This doesn't happen in the buffer, only in the cmdline. I have noticed that it works properly sometimes, but it is rare and I don't know what causes it to work properly. Although, I'm pretty sure it has to do with the trailing slash because in the cmdline, a slash is appended, and in the buffer, a slash is not appended.

Is this a known bug or is there a way to solve this?

otavioschwanck commented 2 years ago

Same here, if i use with cmp-cmdline, :e ~/ it does't add the slash, if i try to use just :e /some/path it adds the slash, super annoying

iwataka commented 2 years ago

I think current cmp-path is triggered only by / or .. So you should start with one of these characters to use this plugin's feature.

https://github.com/hrsh7th/cmp-path/blob/f244d8c33387b2da8915eabf9d2296a4f4ba3e45/lua/cmp_path/init.lua#L38-L40

For example, without these characters, it behaves like below, adding trailing slash. The type of completion candidates becomes Variable because cmp-path doesn't work properly.

Screen Shot 2022-06-13 at 2 01 28

To make it works properly, you should start with ./, like below. The type of completion candidates becomes File/Folder, which means cmp-path is working.

Screen Shot 2022-06-13 at 2 05 04

Also, cmp-path works with leading / and ~/.

Screen Shot 2022-06-13 at 2 07 30 Screen Shot 2022-06-13 at 2 08 36
PowerUser64 commented 2 years ago

After doing some messing around, I determined my configuration was wrong. I took a look at what you have in your configuration @iwataka, and I discovered I had put some things in the wrong places (lua is confusing lol).

The cmdline parts of my config had things that should have been called as functions, but I put them in the cmp.setup{} table.

Here's what I had:

   cmp.setup {
      -- ...

      cmp.setup.cmdline('/', {
         sources = {
            { name = 'buffer' }
         }
      }),
      cmp.setup.cmdline(':', {
         mapping = cmp.mapping.preset.cmdline(),
         sources = cmp.config.sources({
            { name = 'path' },
            { name = 'cmdline' }
         }),
      }),

      -- ...
   }

And here's what I have now:

   cmp.setup {
      -- ...
   }

   -- Thanks to iwataka on github for this bit
   local search_config = {
      mapping = cmp.mapping.preset.cmdline(),
      sources = {
         { name = 'buffer' },
      }
   }

   -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
   cmp.setup.cmdline('/', search_config)
   cmp.setup.cmdline('?', search_config)

   -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
   cmp.setup.cmdline(':', {
      mapping = cmp.mapping.preset.cmdline(),
      sources = cmp.config.sources({
         { name = 'path' }
      }, {
         { name = 'cmdline' }
      })
   })

Thanks, @iwataka!

PowerUser64 commented 2 years ago

Upon further inspection, adopting part of iwataka's config was only part of the solution. I believe the real problem demonstrated here is that cmp-cmdline is completing the path, rather than cmp-path, and cmp-cmdline is not well-equipped to complete paths. So, when additional / characters are added and then deleted, it returns double the number of completions because doing // on UNIX systems is usually interpreted as a single /.

I have no idea how to keep cmp-cmdline from trying to do cmp-path's job, but if there is a way to do such a thing, I'm pretty sure it would solve this problem.

I have opened issue hrsh7th/cmp-cmdline#60 to discuss this further, as I can now confirm it is not a problem with cmp-path.

Hubro commented 2 years ago

image

:exploding_head: