diff --git a/README.md b/README.md index 43d3d70..e1160c7 100644 --- a/README.md +++ b/README.md @@ -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()) ``` diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 612fabb..b7b8454 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -307,7 +307,6 @@ core.confirm = function(e, option, callback) table.insert(restore, keymap.t(string.rep('U', 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() diff --git a/lua/cmp/vim_source.lua b/lua/cmp/vim_source.lua index 90f938b..a991b85 100644 --- a/lua/cmp/vim_source.lua +++ b/lua/cmp/vim_source.lua @@ -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. @@ -36,7 +36,7 @@ end ---@param id number ---@param methods string[] -vim_source.new = function (bridge_id, methods) +vim_source.new = function(bridge_id, methods) local self = {} for _, method in ipairs(methods) do self[method] = (function(m) @@ -49,4 +49,3 @@ vim_source.new = function (bridge_id, methods) end return vim_source -