Allow to take the full SourceConfig from source API (#561)

* - SourceConfig.opts -> SourceConfig.option
- Add SourceConfig.trigger_characters
- Allow accessing full SourceConfig from source

* fmt&lint
This commit is contained in:
hrsh7th
2021-11-23 21:17:03 +09:00
committed by GitHub
parent ea10d5bd2f
commit 1944b46336
7 changed files with 73 additions and 38 deletions

View File

@@ -255,7 +255,7 @@ which defines the source name as `buffer`.
The source name.
#### sources[number].opts (type: table)
#### sources[number].option (type: table)
The source customization options. It is defined by each source.

View File

@@ -78,9 +78,6 @@ config.get_source_config = function(name)
local c = config.get()
for _, s in ipairs(c.sources) do
if s.name == name then
if type(s.opts) ~= 'table' then
s.opts = {}
end
return s
end
end
@@ -98,6 +95,25 @@ config.normalize = function(c)
end
c.mapping = normalized
end
if c.sources then
for _, s in ipairs(c.sources) do
if s.opts and not s.option then
s.option = s.opts
s.opts = nil
vim.api.nvim_echo({
{ '[nvim-cmp] ', 'Normal' },
{ 'sources[number].opts', 'WarningMsg' },
{ ' is deprecated.\n', 'Normal' },
{ '[nvim-cmp] Please use ', 'Normal' },
{ 'sources[number].option', 'WarningMsg' },
{ ' instead.', 'Normal' },
}, true, {})
end
s.option = s.option or {}
end
end
return c
end

View File

@@ -17,9 +17,6 @@ return function()
completeopt = 'menu,menuone,noselect',
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
keyword_length = 1,
get_trigger_characters = function(trigger_characters)
return trigger_characters
end,
},
snippet = {

View File

@@ -185,6 +185,24 @@ source.is_available = function(self)
return true
end
---Get trigger_characters
---@return string[]
source.get_trigger_characters = function(self)
local c = self:get_config()
if c.trigger_characters then
return c.trigger_characters
end
local trigger_characters = {}
if self.source.get_trigger_characters then
trigger_characters = self.source:get_trigger_characters(misc.readonly(self:get_config())) or {}
end
if config.get().completion.get_trigger_characters then
return config.get().completion.get_trigger_characters(trigger_characters)
end
return trigger_characters
end
---Get keyword_pattern
---@return string
source.get_keyword_pattern = function(self)
@@ -193,9 +211,7 @@ source.get_keyword_pattern = function(self)
return c.keyword_pattern
end
if self.source.get_keyword_pattern then
return self.source:get_keyword_pattern({
option = self:get_config().opts,
})
return self.source:get_keyword_pattern(misc.readonly(c))
end
return config.get().completion.keyword_pattern
end
@@ -210,21 +226,6 @@ source.get_keyword_length = function(self)
return config.get().completion.keyword_length or 1
end
---Get trigger_characters
---@return string[]
source.get_trigger_characters = function(self)
local trigger_characters = {}
if self.source.get_trigger_characters then
trigger_characters = self.source:get_trigger_characters({
option = self:get_config().opts,
}) or {}
end
if config.get().completion.get_trigger_characters then
return config.get().completion.get_trigger_characters(trigger_characters)
end
return trigger_characters
end
---Invoke completion
---@param ctx cmp.Context
---@param callback function
@@ -287,12 +288,11 @@ source.complete = function(self, ctx, callback)
self.context = ctx
self.completion_context = completion_context
self.source:complete(
{
context = ctx,
vim.tbl_extend('keep', misc.copy(self:get_config()), {
offset = self.offset,
option = self:get_config().opts,
context = ctx,
completion_context = completion_context,
},
}),
self.complete_dedup(vim.schedule_wrap(function(response)
response = response or {}

View File

@@ -52,12 +52,11 @@ cmp.ItemField.Menu = 'menu'
---@field public global fun(c: cmp.ConfigSchema)
---@field public cmdline fun(type: string, c: cmp.ConfigSchema)
---@class cmp.SourceBaseApiParams
---@field public option table
---@class cmp.SourceApiParams: cmp.SourceConfig
---@class cmp.SourceCompletionApiParams : cmp.SourceBaseApiParams
---@field public context cmp.Context
---@class cmp.SourceCompletionApiParams : cmp.SourceConfig
---@field public offset number
---@field public context cmp.Context
---@field public completion_context lsp.CompletionContext
---@class cmp.Mapping
@@ -118,11 +117,12 @@ cmp.ItemField.Menu = 'menu'
---@class cmp.SourceConfig
---@field public name string
---@field public opts table
---@field public option table|nil
---@field public priority number|nil
---@field public keyword_pattern string
---@field public keyword_length number
---@field public max_item_count number
---@field public group_index number
---@field public trigger_characters string[]|nil
---@field public keyword_pattern string|nil
---@field public keyword_length number|nil
---@field public max_item_count number|nil
---@field public group_index number|nil
return cmp

View File

@@ -168,6 +168,19 @@ misc.to_vimindex = function(text, utfindex)
return utfindex + 1
end
---Return readonly version object
---@generic T
---@param tbl T
---@return T
misc.readonly = function(tbl)
return setmetatable({}, {
__index = tbl,
__newindex = function()
error('this table is readonly.')
end,
})
end
---Mark the function as deprecated
misc.deprecated = function(fn, msg)
local printed = false

View File

@@ -48,4 +48,13 @@ describe('misc', function()
})
assert.are.equal(merged.a, nil)
end)
it('readonly', function()
local o = { a = 1, b = 2 }
local r = misc.readonly(o)
assert.are.equal(r.a, 1)
assert.has_error(function()
r.a = 5
end)
end)
end)