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

custom completion source

haorenW1025 edited this page May 21, 2020 · 5 revisions

Create custom completion source

You can create your own completion source and integrate it into the autocomplete items easily. Let's use a completion source that autocomplete month for example.

  • Create a lua file in your &rtp path(for example: ~/.config/nvim/lua). Let's call it month.lua.
  • Put this code in month.lua
local M = {}

function M.getCompletionItems(prefix, score_func)
  local complete_items = {}
  -- define your total completion items
  local items = {'January', 'Feburary', 'March', 'April', 'May', 'June',
                'July', 'August', 'September', 'October', 'Novermber', 
                'December'}
  -- find matches items and put them into complete_items
  for _, month in ipairs(items) do
    -- score_func is a fuzzy match scoring function
    local score = score_func(prefix, month)
    if score < #prefix/2 then
      -- if you're not familiar with complete_items, see `:h complete-items`
      table.insert(complete_items, {
          word = month,
          kind = 'month',
          score = score,
          icase = 1,
          dup = 1,
          empty = 1,
        })
    end
  end
  return complete_items
end

M.complete_item = {
  item = M.getCompletionItems
}

return M
  • You can register this completion source by putting a single line to your init.vim
" addCompleteItems is defined in source.lua,
" the first argument refer to the chain completion list's index,
" the second argument is the function that get your complete items
lua require'completion'.addCompletionSource('month', require'month'.complete_item)
  • You have successfully create your own completion source! Just put it in your chain completion list and you can use it. For example put this in your init.vim:
let g:completion_chain_complete_list = [
    \{'ins_complete': v:false, 'complete_items': ['lsp', 'snippet', 'month']},
    \{'ins_complete': v:true,  'mode': '<c-p>'},
\]|
  • Enjoy your new completion source!

Create completion source with omni-func

completion-nvim can integrate with any plugins with omni-func implemented. Here's a guide using vimtex as an example.

  • Create a lua file in your &rtp path(for example: ~/.config/nvim/lua). This time we call it vimtex.lua.
  • Put this code in vimtex.lua
local M = {}

function M.getCompletionItems(prefix)
  -- define your total completion items
  local items = vim.api.nvim_call_function('vimtex#complete#omnifunc',{0, prefix})
  return items
end

M.complete_item = {
  item = M.getCompletionItems
}

return M
  • Just like the previous example, register this completion source by
lua require'completion'.addCompletionSource('vimtex', require'vimtex'.complete_item)
  • Let's say we want to use vimtex along with texlab(the LSP for latex). We need to setup the completion_chain_complete_list.
let g:completion_chain_complete_list = {
            \ 'tex' : [
            \     {'complete_items': ['vimtex', 'lsp']}, 
            \   ],
            \ }
  • Or you want vimtex as a backup completion source to texlab, set it up by
let g:completion_chain_complete_list = {
            \ 'tex' : [
            \     {'complete_items': ['lsp']},
            \     {'complete_items': ['vimtex']}, 
            \   ],
  • Use <cmd>lua require'source'.nextCompletion()<CR> and <cmd>lua require'source'.prevCompletion()<CR> to jump between complete sources or let g:completion_auto_change_source = 1 to automatically switch to different source.

You can now use vimtex together with texlab!