completion-nvim icon indicating copy to clipboard operation
completion-nvim copied to clipboard

[Feature request] Relative path completion

Open kristijanhusak opened this issue 4 years ago • 31 comments

Hi,

would you consider adding a relative file path completion as an optional source? Something like this.

I tried adding it myself, but since i don't know lua, everything turned out to be calls to vimscript.

kristijanhusak avatar Apr 06 '20 20:04 kristijanhusak

I'd loved to implement new sources! May I ask a small question though, how is this different from the filename completion of ins-complete?

haorenW1025 avatar Apr 07 '20 02:04 haorenW1025

ins-complete file autocompletes only absolute paths and paths relative to the CWD. Path completion allow autocompleting paths relative to the current file.

kristijanhusak avatar Apr 07 '20 05:04 kristijanhusak

Understand. That's actually pretty helpful. I'm pretty busy recently though... Hopefully I'll be able to make some progress in the weekends.

haorenW1025 avatar Apr 07 '20 05:04 haorenW1025

Sorry for pending this so long...The first step of path completion should be done in the latest commit. It should still have lots of issues and it doesn't support definite path for now. You can enable it by specified path in complete_items, the given example allow path completion only in string. Feel free to report any issue after trying it!

let g:completion_chain_complete_list = {
            \ 'default' : {
            \   'default': [
            \       {'complete_items': ['lsp', 'snippet']},
            \       {'mode': '<c-p>'},
            \       {'mode': '<c-n>'}],
            \   'comment': [],
            \   'string' : [
            \       {'complete_items': ['path']}]
            \   }

haorenW1025 avatar Apr 14 '20 14:04 haorenW1025

Sorry for late response. I tested it, looks good to me for now.

kristijanhusak avatar May 01 '20 16:05 kristijanhusak

Few minor issues that I found:

  • If I try to autocomplete with slash after file, I get the full thing in the autocomplete menu. for example, having this /usr/local/bin/nvim/ shows /usr/local/bin/nvim Path

  • Hidden folders are not autocompleted, for example, ~/.conf doesn't autcomplete ~/.config. Note that once i write the full folder name and continue completing, it works, so ~/.config/nv properly completes to ~/.config/nvim

  • Root is not autocompleted, but this may be due to permission issue, i'm not sure. So for example /ho doesn't show anything (should be /home), but same as for hidden folders, works after the first part.

kristijanhusak avatar May 01 '20 16:05 kristijanhusak

Okay, the first and the third issue should be fixed. The second one is a little tricky and I might need some time...

haorenW1025 avatar May 06 '20 17:05 haorenW1025

Yeah it's fixed, thanks!

kristijanhusak avatar May 06 '20 17:05 kristijanhusak

I think i found some new issues unfortunately.

looks like relative completion is now broken.

path automatically completes without anything: So if I do this:

let g:test_path = '

It automatically starts autocompleting this file directory items, which is I guess fine. The problem is that doing ./ is broken.

So doing this:

let g:test_path = './

should basically do the same thing, but it starts listing files and folders from cwd.

If i try to go one parent up, it goes up from cwd instead of current directory of the file

let g:test_path = '../

kristijanhusak avatar May 07 '20 10:05 kristijanhusak

Should be fixed now, please check if it works.

haorenW1025 avatar May 08 '20 06:05 haorenW1025

Works great! Even hidden folders are shown.

I just ran into one minor issue when testing. When I tried to autocomplete from root with /, i properly got the root folders (etc, home, usr), but when I type any of them, it doesn't continue autocompleting.

For example typing /ho properly shows /home. Then when I add slash, i should get kristijan as a single option, but I don't get anything. Once I type out kristijan it properly shows all the files from home folder.

Another one that doesn't go deeper is for example /etc I assume this has something to do with user permissions.

I'm not sure how the implementation is done, but maybe you could just use the native file autocompletion source if autocompletion starts with /. Native one properly returns any level.

kristijanhusak avatar May 08 '20 08:05 kristijanhusak

That's actually some implementation flaw on my side. It should work fine now, however it's still buggy when completing hidden files.

haorenW1025 avatar May 08 '20 09:05 haorenW1025

Looks good, thanks!

kristijanhusak avatar May 08 '20 09:05 kristijanhusak

@haorenW1025 I'm having one issue in javascript with path completion. Here's min vimrc:

set rtp+=~/path/to/completion-nvim

autocmd BufEnter * lua require'completion'.on_attach()

let g:completion_chain_complete_list = {
      \ 'default': [
      \    {'complete_items': ['path']},
      \  ]}

Steps to reproduce:

  1. Open any js file, for example, create new file test.js
  2. start typing this:
const test = { f

Once you type f, it will start throwing this error:

Error executing vim.schedule lua callback: Vim:E220: Missing }.

kristijanhusak avatar May 13 '20 09:05 kristijanhusak

Should be fixed in the latest commit. Please update and check.

haorenW1025 avatar May 13 '20 15:05 haorenW1025

It works, thanks!

kristijanhusak avatar May 13 '20 15:05 kristijanhusak

@haorenW1025 would you consider autocompleting files only if there's a valid prefix (./, ../, /) ?

It's a bit annoying that I get all the files from current directory while typing out a string that's not a path.

kristijanhusak avatar May 21 '20 14:05 kristijanhusak

If so, please consider to make if configurable (the trigger chars). There are definitely cases when this fits. And if the priority setting are this, I think there must not be an issue for everyone.

weilbith avatar May 21 '20 16:05 weilbith

I have some feature that haven't reveal yet(cause I'm still thinking about how it should be), so you can specify triggered_only for completion source. Use this in completion_chain_complete_list to have it only trigger in some character. For example,

let g:completion_chain_complete_list = {
            \ 'default' : {
            \   'default': [
            \       {'complete_items': ['lsp', 'snippet']},
            \       {'mode': '<c-p>'},
            \       {'mode': '<c-n>'}],
            \   'comment': [],
            \   'string' : [
            \       {'complete_items': ['path'], 'triggered_only': ['/']}]
            \   }}

This might change in the future, but it can fit your use case for now.

haorenW1025 avatar May 21 '20 16:05 haorenW1025

Ok, i'll give it a try, thanks!

kristijanhusak avatar May 21 '20 17:05 kristijanhusak

It works, thanks!

kristijanhusak avatar May 22 '20 12:05 kristijanhusak

I found one issue with it though. I'm using this config:

let g:completion_auto_change_source = 1
let g:completion_chain_complete_list = {
      \ 'default': [
      \    {'complete_items': ['lsp']},
      \    {'complete_items': ['path'], 'triggered_only': ['/']},
      \    {'mode': 'tags'},
      \    {'mode': 'keyn'},
      \    {'mode': '<c-p>'},
      \  ]}

And auto change source never comes to the tags and everything else after. Like that triggered_only continues to be used for all sources after it and it's not able to find anything.

kristijanhusak avatar May 22 '20 12:05 kristijanhusak

Yeah that's definitely a problem now... the triggered_only key kind of destroy the chain completion. Would you expect to skip the path completion (in your example) to use other completion instead if not on the triggered_only characters?

haorenW1025 avatar May 22 '20 13:05 haorenW1025

Yeah, i would expect it just to continue as if path complete item wasn't even there.

kristijanhusak avatar May 22 '20 14:05 kristijanhusak

I think we can close this issue. I believe it is resolved. If anything new arise, we can reopen it. I'll leave this decision to you.

Thanks for adding the feature!

kristijanhusak avatar Jun 10 '20 15:06 kristijanhusak

I think there is the hidden files issue still exist, right now the file will show but will disappear upon typing .. I'll leave this issue open but the priority of solving this will be lower.

haorenW1025 avatar Jun 10 '20 16:06 haorenW1025

I'm not sure how's your logic around it, but you could check how mucomplete does path completion, I didn't had any issues with it. https://github.com/lifepillar/vim-mucomplete/blob/master/autoload/mucomplete/path.vim#L78

kristijanhusak avatar Jun 11 '20 08:06 kristijanhusak

Error executing vim.schedule lua callback: Vim:E220: Missing }.

I'm still having this issue with strings in lua completion

else
  local sumneko_root_path = vim.fn.getenv("HOME").."/.local/bin/sumneko_lua"
  sumneko_cmd = {sumneko_root_path.."/bin/macOS(enter here)/lua-language-server", "-E", sumneko_root_path.."/main.lua" }
end

mjlbach avatar Feb 03 '21 18:02 mjlbach

I am also having problems with completion of path. It does work when typing '/':

def test():
    "/|"

Inside the quotes, :echo synIDattr(synID(line("."),col("."),1),"name") returns pythonString

It does not work inside a string for a method call in python for example. When specifying the file in the method "open" in python, it does not autocomplete the path. Even though it is recognised as a pythonString with :echo synIDattr(synID(line("."),col("."),1),"name")

def test():
    f = open("/|")

This is the completion chain I am using:

vim.g.completion_chain_complete_list = {
    default = {
        default = {
            {complete_items = {'lsp'}},
            {complete_items = {'buffers'}}
        },
        tex = {
            {complete_items = {'vimtex', 'lsp'}}
        },
        comment = {
            {complete_items = {'buffers'}}
        },
        string = {
            {complete_items = {'path'}}
        },
    }
}

seblj avatar Feb 10 '21 22:02 seblj

@seblj i was just going over your post, shouldn't "tex" list come outside the second default table? like this

vim.g.completion_chain_complete_list = {
    default = {
        default = {
            {complete_items = {'lsp'}},
            {complete_items = {'buffers'}}
        },
        comment = {
            {complete_items = {'buffers'}}
        },
        string = {
            {complete_items = {'path'}}
        },
    },
    tex = {
         {complete_items = {'vimtex', 'lsp'}}
    },
}

ranjithshegde avatar Jul 04 '21 08:07 ranjithshegde