Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

chain complete support

haorenW1025 edited this page Nov 20, 2020 · 7 revisions

Setup Chain Completion

completion-nvim has chain completion support, this is greatly inspired by vim-mucomplete(thanks vigoux for this feature suggestion). In short, you can divide completion sources in group and having ins-completion method as backup completion.

  • By default, chain completion list is listed as below, which means you have lsp and snippet in the first completion source, <c-p> in ins-complete as the second source and <c-n> in ins-complete as the third source.
let g:completion_chain_complete_list = [
    \{'complete_items': ['lsp', 'snippet']},
    \{'mode': '<c-p>'},
    \{'mode': '<c-n>'}
\]

Switching between completion list

  • You can switch to next or previous completion source with some mapping, for example
imap <c-j> <Plug>(completion_next_source) "use <c-j> to switch to previous completion
imap <c-k> <Plug>(completion_prev_source) "use <c-k> to switch to next completion
  • Or if you want to change sources whenever this completion source has no complete item, you can turn on auto changing sources by
let g:completion_auto_change_source = 1

Customization of chain_completion_list

  • You can customize your own chain completion list, for not ins-complete sources, you can choose to put them in the same source or separate them. For example, if you want to separate lsp and snippet into two different source, do it like this
" non ins-complete method should be specified in 'mode'
let g:completion_chain_complete_list = [
    \{'complete_items': ['lsp']},
    \{'complete_items': ['snippet']},
    \{'mode': '<c-p>'},
    \{'mode': '<c-n>'}
\]
  • You can also use ins-complete sources, possible 'mode' to the actual key in vim are listed below.
"<c-n>" : i_CTRL-N
"<c-p>" : i_CTRL-P
"cmd" : i_CTRL-X_CTRL-V
"defs": i_CTRL-X_CTRL-D
"dict": i_CTRL-X_CTRL-K
"file": i_CTRL-X_CTRL-F
"incl": i_CTRL-X_CTRL-I
"keyn": i_CTRL-X_CTRL-N
"keyp": i_CTRL-X_CTRL-P
"omni": i_CTRL-X_CTRL-O
"line": i_CTRL-X_CTRL-L
"spel": i_CTRL-X_s
"tags": i_CTRL-X_CTRL-]
"thes": i_CTRL-X_CTRL-T
"user": i_CTRL-X_CTRL-U
  • If you want to use different chain complete list for different filetypes, you can add the filetype key, but remember to specify the default key for all the other filetypes that are not specified.
let g:completion_chain_complete_list = {
    \ 'vim': [
    \    {'mode': '<c-p>'},
    \    {'mode': '<c-n>'}
    \],
    \ 'lua': [
    \    {'mode': '<c-p>'},
    \    {'mode': '<c-n>'}
    \],
    \ 'default': [
    \    {'complete_items': ['lsp', 'snippet']},
    \    {'mode': '<c-p>'},
    \    {'mode': '<c-n>'}
    \]
\}
  • You can take a step further to specified different "scope" of different filetype. "scope" is literally syntax in your file. Say that you want different completion list in comment and function call, string and etc, you can have it done easily. Here is an example.
 let g:completion_chain_complete_list = {
    \ 'lua': [
    \    'string': [
    \        {'mode': '<c-p>'},
    \        {'mode': '<c-n>'}],
    \    'func' : [
    \        {'complete_items': ['lsp']}],
    \    'default': [
    \       {'complete_items': ['lsp', 'snippet']},
    \       {'mode': '<c-p>'},
    \       {'mode': '<c-n>'}],
    \],
    \ 'default' : {
    \   'default': [
    \       {'complete_items': ['lsp', 'snippet']},
    \       {'mode': '<c-p>'},
    \       {'mode': '<c-n>'}],
    \   'comment': []
    \   }
    \}
  • Every syntax highlighter have different syntax name defined(most of them are similar though). You can check your syntax name under your cursor by :echo synIDattr(synID(line('.'), col('.'), 1), "name"). You only need to specified a part of the result in the scope since it use regex pattern to match it ( For example: if the result is luaComment you only need to specified comment, case doesn't matter).