Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
hrsh7th edited this page Oct 25, 2020 · 17 revisions

Creating a source

nvim-compe provides the custom source register API.

The custom source can be written in Vim script or Lua.

Overview

  1. The custom source is dictionary.
  2. The custom source must have get_metadata, datermine, complete.

get_metadata

This method should return the source's metadata.

The metadata can have...

  • dup: 1/0
    • see :help complete-items
  • menu: string
    • see :help complete-items
  • priority: number
    • This uses nvim-compe's filtering implementation.
    • The items always sort priority order.
  • sort: boolean
    • This uses nvim-compe's filtering implementation.
    • The items do not sort so the source should sort items manually.

datermine

The datermine method should return { keyword_pattern_offset?: number, trigger_character_offset?: number }.

If the datermine return an empty object, nvim-compe does not trigger completion.

The keyword_pattern_offset means pum position. In PHP, When the line is $|, keyword_pattern_offset should 1.

complete

The complete method should complete items and callback it.

In Vim script

The most simple source can be created by the below example of codes.

let s:source = {
  'get_metadata': function('s:get_metadata'),
  'datermine': function('s:datermine'),
  'complete': function('s:complete'),
}

function! s:get_medatada(...) abort
  return {
  \   'priority': 10,
  \   'menu': '[MY CUSTOM SOURCE]',
  \ }
endfunction

function! s:determine(context) abort
  let l:keyword_pattern_offset = compe#pattern#get_keyword_pattern_offset(a:context)
  if l:keyword_pattern_offset > 0
    return {
    \   'keyword_pattern_offset': l:keyword_pattern_offset
    \ }
  endif
  return {}
endfunction

function! s:complete(context) abort
  call a:context.callback({
  \   'items': [
  \     { 'word': 'January' },
  \     { 'word': 'February' },
  \     { 'word': 'March' },
  \     { 'word': 'April' },
  \     { 'word': 'May' },
  \     { 'word': 'June' },
  \     { 'word': 'July' },
  \     { 'word': 'August' },
  \     { 'word': 'August' },
  \     { 'word': 'September' },
  \     { 'word': 'October' },
  \     { 'word': 'November' },
  \     { 'word': 'December' },
  \   ]
  \ })
endfunction

" Register your custom source.
call compe#source#vim_bridge#register('month', s:source)

In Lua

The most simple source can be created by the below example of codes.

local Source = {}

function Source.new()
  return setmetatable({}, { __index = Source })
end

function Source.get_metadata(self)
  -- similar to vim.
end

function Source.datermine(self, context)
  -- similar to vim.
end

function Source.complete(self, context)
  -- similar to vim.
end

-- Register your custom source.
require'compe':register_lua_source('month', Source)
Clone this wiki locally