Add Vim script API docs
This commit is contained in:
35
README.md
35
README.md
@@ -607,5 +607,38 @@ function source:execute(completion_item, callback)
|
|||||||
callback(completion_item)
|
callback(completion_item)
|
||||||
end
|
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())
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -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, 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()))
|
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
|
||||||
keymap.feedkeys(table.concat(restore, ''), 'n', function()
|
keymap.feedkeys(table.concat(restore, ''), 'n', function()
|
||||||
|
|
||||||
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
--@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
|
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
||||||
local pre = context.new()
|
local pre = context.new()
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ end
|
|||||||
---@param callback function
|
---@param callback function
|
||||||
---@return number
|
---@return number
|
||||||
vim_source.to_callback = setmetatable({
|
vim_source.to_callback = setmetatable({
|
||||||
callbacks = {}
|
callbacks = {},
|
||||||
}, {
|
}, {
|
||||||
__call = function(self, callback)
|
__call = function(self, callback)
|
||||||
local id = misc.id('cmp.vim_source.to_callback')
|
local id = misc.id('cmp.vim_source.to_callback')
|
||||||
@@ -20,7 +20,7 @@ vim_source.to_callback = setmetatable({
|
|||||||
self.callbacks[id] = nil
|
self.callbacks[id] = nil
|
||||||
end
|
end
|
||||||
return id
|
return id
|
||||||
end
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
---Convert to serializable args.
|
---Convert to serializable args.
|
||||||
@@ -36,7 +36,7 @@ end
|
|||||||
|
|
||||||
---@param id number
|
---@param id number
|
||||||
---@param methods string[]
|
---@param methods string[]
|
||||||
vim_source.new = function (bridge_id, methods)
|
vim_source.new = function(bridge_id, methods)
|
||||||
local self = {}
|
local self = {}
|
||||||
for _, method in ipairs(methods) do
|
for _, method in ipairs(methods) do
|
||||||
self[method] = (function(m)
|
self[method] = (function(m)
|
||||||
@@ -49,4 +49,3 @@ vim_source.new = function (bridge_id, methods)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return vim_source
|
return vim_source
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user