Skip to content

Latest commit

 

History

History
352 lines (269 loc) · 9.12 KB

README.md

File metadata and controls

352 lines (269 loc) · 9.12 KB

nvim-compe

Auto completion plugin for nvim.

Table Of Contents

Concept

  • Simple core
  • No flicker
  • Lua source & Vim source
  • Better matching algorithm
  • Support LSP completion features (trigger character, isIncomplete, expansion)
  • Respect VSCode/LSP API design

Features

  • VSCode compatible expansion handling
    • rust-analyzer's Magic completion
    • vscode-html-languageserver-bin's closing tag completion
    • Other complex expansion are supported
  • Flexible Custom Source API
    • The source can support documentation / resolve / confirm
  • Better fuzzy matching algorithm
    • gu can be matched get_user
    • fmodify can be matched fnamemodify
    • See matcher.lua for implementation details
  • Buffer source carefully crafted
    • The buffer source will index buffer words by filetype specific regular expression if needed

Usage

Detailed docs in here or :help compe.

Prerequisite

You must set completeopt to menuone,noselect which can be easily done as follows.

Using Vim script

set completeopt=menuone,noselect

Using Lua

vim.o.completeopt = "menuone,noselect"

The source option is required if you want to enable but others can be omitted.

Vim script Config

let g:compe = {}
let g:compe.enabled = v:true
let g:compe.autocomplete = v:true
let g:compe.debug = v:false
let g:compe.min_length = 1
let g:compe.preselect = 'enable'
let g:compe.throttle_time = 80
let g:compe.source_timeout = 200
let g:compe.incomplete_delay = 400
let g:compe.max_abbr_width = 100
let g:compe.max_kind_width = 100
let g:compe.max_menu_width = 100
let g:compe.documentation = v:true

let g:compe.source = {}
let g:compe.source.path = v:true
let g:compe.source.buffer = v:true
let g:compe.source.calc = v:true
let g:compe.source.vsnip = v:true
let g:compe.source.nvim_lsp = v:true
let g:compe.source.nvim_lua = v:true
let g:compe.source.spell = v:true
let g:compe.source.tags = v:true
let g:compe.source.snippets_nvim = v:true
let g:compe.source.treesitter = v:true
let g:compe.source.omni = v:true

Lua Config

require'compe'.setup {
  enabled = true;
  autocomplete = true;
  debug = false;
  min_length = 1;
  preselect = 'enable';
  throttle_time = 80;
  source_timeout = 200;
  incomplete_delay = 400;
  max_abbr_width = 100;
  max_kind_width = 100;
  max_menu_width = 100;
  documentation = true;

  source = {
    path = true;
    buffer = true;
    calc = true;
    vsnip = true;
    nvim_lsp = true;
    nvim_lua = true;
    spell = true;
    tags = true;
    snippets_nvim = true;
    treesitter = true;
  };
}

Mappings

inoremap <silent><expr> <C-Space> compe#complete()
inoremap <silent><expr> <CR>      compe#confirm('<CR>')
inoremap <silent><expr> <C-e>     compe#close('<C-e>')
inoremap <silent><expr> <C-f>     compe#scroll({ 'delta': +4 })
inoremap <silent><expr> <C-d>     compe#scroll({ 'delta': -4 })

If you use cohama/lexima.vim

" NOTE: Order is important. You can't lazy loading lexima.vim.
let g:lexima_no_default_rules = v:true
call lexima#set_default_rules()
inoremap <silent><expr> <C-Space> compe#complete()
inoremap <silent><expr> <CR>      compe#confirm(lexima#expand('<LT>CR>', 'i'))
inoremap <silent><expr> <C-e>     compe#close('<C-e>')
inoremap <silent><expr> <C-f>     compe#scroll({ 'delta': +4 })
inoremap <silent><expr> <C-d>     compe#scroll({ 'delta': -4 })

If you use Raimondi/delimitMate

inoremap <silent><expr> <C-Space> compe#complete()
inoremap <silent><expr> <CR>      compe#confirm({ 'keys': "\<Plug>delimitMateCR", 'mode': '' })
inoremap <silent><expr> <C-e>     compe#close('<C-e>')
inoremap <silent><expr> <C-f>     compe#scroll({ 'delta': +4 })
inoremap <silent><expr> <C-d>     compe#scroll({ 'delta': -4 })

Highlight

You can change documentation window's highlight group via following.

highlight link CompeDocumentation NormalFloat

Built-in sources

Common

  • buffer
  • path
  • tags
  • spell
  • calc
  • omni

Neovim-specific

  • nvim_lsp
  • nvim_lua

External-plugin

External sources

FAQ

Can't get sorting to work correctly.

If you are using nvim_lsp source and omni source both, you should remove set omnifunc=v:lua.vim.lsp.omnifunc from your configuration.

See #230

How to use LSP snippet?

  1. Set snippetSupport=true for LSP capabilities.

    local capabilities = vim.lsp.protocol.make_client_capabilities()
    capabilities.textDocument.completion.completionItem.snippetSupport = true
    
    require'lspconfig'.rust_analyzer.setup {
      capabilities = capabilities,
    }
  2. Install vim-vsnip

    Plug 'hrsh7th/vim-vsnip'

    or snippets.nvim

    Plug 'norcalli/snippets.nvim'

How to use tab to navigate completion menu?

Tab and S-Tab keys need to be mapped to <C-n> and <C-p> when completion menu is visible. Following example will use Tab and S-Tab (shift+tab) to navigate completion menu and jump between vim-vsnip placeholders when possible:

local t = function(str)
  return vim.api.nvim_replace_termcodes(str, true, true, true)
end

local check_back_space = function()
    local col = vim.fn.col('.') - 1
    if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
        return true
    else
        return false
    end
end

-- Use (s-)tab to:
--- move to prev/next item in completion menuone
--- jump to prev/next snippet's placeholder
_G.tab_complete = function()
  if vim.fn.pumvisible() == 1 then
    return t "<C-n>"
  elseif vim.fn.call("vsnip#available", {1}) == 1 then
    return t "<Plug>(vsnip-expand-or-jump)"
  elseif check_back_space() then
    return t "<Tab>"
  else
    return vim.fn['compe#complete']()
  end
end
_G.s_tab_complete = function()
  if vim.fn.pumvisible() == 1 then
    return t "<C-p>"
  elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then
    return t "<Plug>(vsnip-jump-prev)"
  else
    return t "<S-Tab>"
  end
end

vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})

How to expand snippets from completion menu?

Use compe#confirm() mapping, as described in section Mappings.

Demo

Auto Import

auto import

lsp

Buffer Source Completion

buffer

Calc Completion

calc

Nvim Lua Completion

nvim lua

Vsnip Completion

vsnip

Snippets.nvim Completion

snippets.nvim

Treesitter Completion

treesitter.nvim

Tag Completion

tag

Spell Completion

spell