Skip to main content
  1. Posts/

Set up Fuzzy Completion for Vim-lsp

··267 words·2 mins·
Table of Contents

Update 2021-07-15: Note that pyls has been deprecated. There is new fork of it called pylsp, which is maintained by the community.

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 ↩︎

Related

Flake8 Config in Pyls for Code Linting.
··378 words·2 mins
Replace Deoplete-jedi with LSP Finally
··545 words·3 mins
Pylsp setup for Neovim in 2023
··1020 words·5 mins