20

Set up Fuzzy Completion for Vim-lsp

 3 years ago
source link: https://jdhao.github.io/2020/11/16/vim_lsp_fuzzy_completion/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Set up Fuzzy Completion for Vim-lsp

2020-11-16240 words 2 mins read 156 times read

In my older post, I have shared how to set up auto-completion for vim-lsp with the help of deoplete. One annoyance is that I can not use fuzzy matching for auto-completion: the completion items in the pop-up completion menu seems to be selected base on prefix matching.

After consulting the deoplete documentation, I add the following config:

call deoplete#custom#source('_', 'matchers', ['matcher_full_fuzzy'])
call deoplete#custom#option({'camel_case': v:true,})

Interestingly, the above config works for ccls, i.e., fuzzy matching now works for cpp files. For example, cv::imread("test.jpg", cv::imcolor) can show cv::IMREAD_COLOR as a completion candidate.

However, for pyls, the above config does not work. Under the hood, pyls uses jedi for code completion feature (see here).

In newer version of Jedi, it actually supports fuzzy code completion in complete() method. Pyls turns off this feature by default. So to enable fuzzy matching, we need to config it manually. Based on example config here, I have figured out the correct config for vim-lsp:

if executable('pyls')
 " pip install python-language-server
  augroup pyls_setup
    autocmd!
    autocmd User lsp_setup call lsp#register_server({
          \ 'name': 'pyls',
          \ 'cmd': {server_info->['pyls']},
          \ 'allowlist': ['python'],
          \ 'workspace_config': {
          \    'pyls':
          \        {'configurationSources': ['flake8'],
          \         'plugins': {'flake8': {'enabled': v:true},
          \                     'pyflakes': {'enabled': v:false},
          \                     'pycodestyle': {'enabled': v:false},
          \                     'jedi_completion': {'fuzzy': v:true},
          \                    }
          \        }
          \ }})
  augroup END
endif

Note that you also need to install the latest version of jedi1:

pip install -U jedi

Now everything should work as expected.

References


  1. The jedi version tested is 0.17.2 ↩︎

Author jdhao

LastMod 2020-11-16

License CC BY-NC-ND 4.0

Reward
Prev Next

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK