Add Vim script API docs

This commit is contained in:
hrsh7th
2021-09-14 01:28:33 +09:00
parent 03f121fa23
commit 44ffee0e0c
3 changed files with 37 additions and 6 deletions

View File

@@ -607,5 +607,38 @@ function source:execute(completion_item, callback)
callback(completion_item)
end
return source
require('cmp').register_source(source.new())
```
You can also create source by Vim script like this (This is useful to support callback style plugins).
- If you want to return `boolean`, you must return `v:true`/`v:false`. It doesn't `0`/`1`.
```vim
let s:source = {}
function! s:source.new() abort
return extend(deepcopy(s:source))
endfunction
" The other APIs are also available.
function! s:source.complete(params, callback) abort
call a:callback({
\ { 'label': 'January' },
\ { 'label': 'February' },
\ { 'label': 'March' },
\ { 'label': 'April' },
\ { 'label': 'May' },
\ { 'label': 'June' },
\ { 'label': 'July' },
\ { 'label': 'August' },
\ { 'label': 'September' },
\ { 'label': 'October' },
\ { 'label': 'November' },
\ { 'label': 'December' },
\ })
endfunction
call cmp#register_source('month', s:source.new())
```

View File

@@ -307,7 +307,6 @@ core.confirm = function(e, option, callback)
table.insert(restore, keymap.t(string.rep('<C-g>U<Left><Del>', vim.fn.strchars(e:get_word()))))
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
keymap.feedkeys(table.concat(restore, ''), 'n', function()
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
local pre = context.new()

View File

@@ -11,7 +11,7 @@ end
---@param callback function
---@return number
vim_source.to_callback = setmetatable({
callbacks = {}
callbacks = {},
}, {
__call = function(self, callback)
local id = misc.id('cmp.vim_source.to_callback')
@@ -20,7 +20,7 @@ vim_source.to_callback = setmetatable({
self.callbacks[id] = nil
end
return id
end
end,
})
---Convert to serializable args.
@@ -49,4 +49,3 @@ vim_source.new = function (bridge_id, methods)
end
return vim_source