Add filetype config
Use view.entries == 'native' instead of experimental.native_menu
This commit is contained in:
@@ -17,6 +17,9 @@ config.global = require('cmp.config.default')()
|
||||
---@type table<number, cmp.ConfigSchema>
|
||||
config.buffers = {}
|
||||
|
||||
---@type table<string, cmp.ConfigSchema>
|
||||
config.filetypes = {}
|
||||
|
||||
---@type table<string, cmp.ConfigSchema>
|
||||
config.cmdline = {}
|
||||
|
||||
@@ -40,6 +43,15 @@ config.set_buffer = function(c, bufnr)
|
||||
config.buffers[bufnr].revision = revision + 1
|
||||
end
|
||||
|
||||
---Set configuration for filetype
|
||||
---@param c cmp.ConfigSchema
|
||||
---@param filetype string
|
||||
config.set_filetype = function(c, filetype)
|
||||
local revision = (config.filetypes[filetype] or {}).revision or 1
|
||||
config.filetypes[filetype] = c or {}
|
||||
config.filetypes[filetype].revision = revision + 1
|
||||
end
|
||||
|
||||
---Set configuration for cmdline
|
||||
---@param c cmp.ConfigSchema
|
||||
---@param cmdtype string
|
||||
@@ -59,23 +71,48 @@ end
|
||||
|
||||
---@return cmp.ConfigSchema
|
||||
config.get = function()
|
||||
local global = config.global
|
||||
local global_config = config.global
|
||||
if config.onetime.sources then
|
||||
local onetime = config.onetime
|
||||
return config.cache:ensure({ 'get_onetime', global.revision or 0, onetime.revision or 0 }, function()
|
||||
return misc.merge(config.normalize(onetime), config.normalize(global))
|
||||
local onetime_config = config.onetime
|
||||
return config.cache:ensure({
|
||||
'get',
|
||||
'onetime',
|
||||
global_config.revision or 0,
|
||||
onetime_config.revision or 0
|
||||
}, function()
|
||||
return misc.merge(config.normalize(onetime_config), config.normalize(global_config))
|
||||
end)
|
||||
elseif api.is_cmdline_mode() then
|
||||
local cmdtype = vim.fn.getcmdtype()
|
||||
local cmdline = config.cmdline[cmdtype] or { revision = 1, sources = {} }
|
||||
return config.cache:ensure({ 'get_cmdline', cmdtype, global.revision or 0, cmdline.revision or 0 }, function()
|
||||
return misc.merge(config.normalize(cmdline), config.normalize(global))
|
||||
local cmdline_config = config.cmdline[cmdtype] or { revision = 1, sources = {} }
|
||||
return config.cache:ensure({
|
||||
'get',
|
||||
'cmdline',
|
||||
global_config.revision or 0,
|
||||
cmdtype,
|
||||
cmdline_config.revision or 0
|
||||
}, function()
|
||||
return misc.merge(config.normalize(cmdline_config), config.normalize(global_config))
|
||||
end)
|
||||
else
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local buffer = config.buffers[bufnr] or { revision = 1 }
|
||||
return config.cache:ensure({ 'get_buffer', bufnr, global.revision or 0, buffer.revision or 0 }, function()
|
||||
return misc.merge(config.normalize(buffer), config.normalize(global))
|
||||
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
|
||||
local buffer_config = config.buffers[bufnr] or { revision = 1 }
|
||||
local filetype_config = config.filetypes[filetype] or { revision = 1 }
|
||||
return config.cache:ensure({
|
||||
'get',
|
||||
'default',
|
||||
global_config.revision or 0,
|
||||
filetype,
|
||||
filetype_config.revision or 0,
|
||||
bufnr,
|
||||
buffer_config.revision or 0,
|
||||
}, function()
|
||||
local c = {}
|
||||
c = misc.merge(c, config.normalize(buffer_config))
|
||||
c = misc.merge(c, config.normalize(filetype_config))
|
||||
c = misc.merge(c, config.normalize(global_config))
|
||||
return c
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -102,6 +139,18 @@ config.get_source_config = function(name)
|
||||
return nil
|
||||
end
|
||||
|
||||
---Return the current menu is native or not.
|
||||
config.is_native_menu = function()
|
||||
local c = config.get()
|
||||
if c.experimental and c.experimental.native_menu then
|
||||
return true
|
||||
end
|
||||
if c.view and c.view.entries then
|
||||
return c.view.entries == 'native' or c.view.entries.name == 'native'
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---Normalize mapping key
|
||||
---@param c cmp.ConfigSchema
|
||||
---@return cmp.ConfigSchema
|
||||
@@ -114,6 +163,20 @@ config.normalize = function(c)
|
||||
c.mapping = normalized
|
||||
end
|
||||
|
||||
if c.experimental and c.experimental.native_menu then
|
||||
vim.api.nvim_echo({
|
||||
{ '[nvim-cmp] ', 'Normal' },
|
||||
{ 'experimental.native_menu', 'WarningMsg' },
|
||||
{ ' is deprecated.\n', 'Normal' },
|
||||
{ '[nvim-cmp] Please use ', 'Normal' },
|
||||
{ 'view.entries = "native"', 'WarningMsg' },
|
||||
{ ' instead.', 'Normal' },
|
||||
}, true, {})
|
||||
|
||||
c.view = c.view or {}
|
||||
c.view.entries = c.view.entries or 'native'
|
||||
end
|
||||
|
||||
if c.sources then
|
||||
for _, s in ipairs(c.sources) do
|
||||
if s.opts and not s.option then
|
||||
|
||||
@@ -10,53 +10,9 @@ return function()
|
||||
enabled = function()
|
||||
return vim.api.nvim_buf_get_option(0, 'buftype') ~= 'prompt'
|
||||
end,
|
||||
completion = {
|
||||
autocomplete = {
|
||||
types.cmp.TriggerEvent.TextChanged,
|
||||
},
|
||||
completeopt = 'menu,menuone,noselect',
|
||||
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
|
||||
keyword_length = 1,
|
||||
},
|
||||
|
||||
snippet = {
|
||||
expand = function()
|
||||
error('snippet engine is not configured.')
|
||||
end,
|
||||
},
|
||||
|
||||
preselect = types.cmp.PreselectMode.Item,
|
||||
|
||||
documentation = {
|
||||
border = { '', '', '', ' ', '', '', '', ' ' },
|
||||
winhighlight = 'NormalFloat:NormalFloat,FloatBorder:NormalFloat',
|
||||
maxwidth = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
|
||||
maxheight = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
|
||||
},
|
||||
|
||||
confirmation = {
|
||||
default_behavior = types.cmp.ConfirmBehavior.Insert,
|
||||
get_commit_characters = function(commit_characters)
|
||||
return commit_characters
|
||||
end,
|
||||
},
|
||||
|
||||
sorting = {
|
||||
priority_weight = 2,
|
||||
comparators = {
|
||||
compare.offset,
|
||||
compare.exact,
|
||||
compare.score,
|
||||
compare.recently_used,
|
||||
compare.kind,
|
||||
compare.sort_text,
|
||||
compare.length,
|
||||
compare.order,
|
||||
},
|
||||
},
|
||||
|
||||
event = {},
|
||||
|
||||
mapping = {
|
||||
['<Down>'] = mapping({
|
||||
i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
|
||||
@@ -79,7 +35,7 @@ return function()
|
||||
['<Tab>'] = mapping({
|
||||
c = function(fallback)
|
||||
local cmp = require('cmp')
|
||||
if #cmp.core:get_sources() > 0 and not cmp.get_config().experimental.native_menu then
|
||||
if #cmp.core:get_sources() > 0 and not require('cmp.config').is_native_menu() then
|
||||
if cmp.visible() then
|
||||
cmp.select_next_item()
|
||||
else
|
||||
@@ -93,7 +49,7 @@ return function()
|
||||
['<S-Tab>'] = mapping({
|
||||
c = function(fallback)
|
||||
local cmp = require('cmp')
|
||||
if #cmp.core:get_sources() > 0 and not cmp.get_config().experimental.native_menu then
|
||||
if #cmp.core:get_sources() > 0 and not require('cmp.config').is_native_menu() then
|
||||
if cmp.visible() then
|
||||
cmp.select_prev_item()
|
||||
else
|
||||
@@ -110,6 +66,21 @@ return function()
|
||||
['<C-e>'] = mapping.abort(),
|
||||
},
|
||||
|
||||
snippet = {
|
||||
expand = function()
|
||||
error('snippet engine is not configured.')
|
||||
end,
|
||||
},
|
||||
|
||||
completion = {
|
||||
keyword_length = 1,
|
||||
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
|
||||
autocomplete = {
|
||||
types.cmp.TriggerEvent.TextChanged,
|
||||
},
|
||||
completeopt = 'menu,menuone,noselect',
|
||||
},
|
||||
|
||||
formatting = {
|
||||
fields = { 'abbr', 'kind', 'menu' },
|
||||
format = function(_, vim_item)
|
||||
@@ -117,15 +88,44 @@ return function()
|
||||
end,
|
||||
},
|
||||
|
||||
experimental = {
|
||||
native_menu = false,
|
||||
ghost_text = false,
|
||||
sorting = {
|
||||
priority_weight = 2,
|
||||
comparators = {
|
||||
compare.offset,
|
||||
compare.exact,
|
||||
compare.score,
|
||||
compare.recently_used,
|
||||
compare.kind,
|
||||
compare.sort_text,
|
||||
compare.length,
|
||||
compare.order,
|
||||
},
|
||||
},
|
||||
|
||||
sources = {},
|
||||
|
||||
documentation = {
|
||||
border = { '', '', '', ' ', '', '', '', ' ' },
|
||||
winhighlight = 'NormalFloat:NormalFloat,FloatBorder:NormalFloat',
|
||||
maxwidth = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
|
||||
maxheight = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
|
||||
},
|
||||
|
||||
confirmation = {
|
||||
default_behavior = types.cmp.ConfirmBehavior.Insert,
|
||||
get_commit_characters = function(commit_characters)
|
||||
return commit_characters
|
||||
end,
|
||||
},
|
||||
|
||||
event = {},
|
||||
|
||||
experimental = {
|
||||
ghost_text = false,
|
||||
},
|
||||
|
||||
view = {
|
||||
entries = { name = 'custom' },
|
||||
entries = 'custom',
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
@@ -236,7 +236,7 @@ cmp.status = function()
|
||||
for _, name in ipairs(kinds.available) do
|
||||
vim.api.nvim_echo({ { ('- %s\n'):format(name), 'Normal' } }, false, {})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if #kinds.unavailable > 0 then
|
||||
vim.api.nvim_echo({ { '\n', 'Normal' } }, false, {})
|
||||
@@ -268,6 +268,9 @@ cmp.setup = setmetatable({
|
||||
global = function(c)
|
||||
config.set_global(c)
|
||||
end,
|
||||
filetype = function(filetype, c)
|
||||
config.set_filetype(c, filetype)
|
||||
end,
|
||||
buffer = function(c)
|
||||
config.set_buffer(c, vim.api.nvim_get_current_buf())
|
||||
end,
|
||||
|
||||
@@ -131,12 +131,19 @@ cmp.ItemField.Menu = 'menu'
|
||||
---@field public max_item_count number|nil
|
||||
---@field public group_index number|nil
|
||||
|
||||
---@class cmp.EntriesConfig
|
||||
---@field name string
|
||||
---@class cmp.ViewConfig
|
||||
---@field public entries cmp.EntriesConfig
|
||||
|
||||
---@alias cmp.EntriesConfig cmp.CustomEntriesConfig|cmp.NativeEntriesConfig|cmp.WildmenuEntriesConfig|string
|
||||
|
||||
---@class cmp.CustomEntriesConfig
|
||||
---@field name "'custom'"
|
||||
|
||||
---@class cmp.NativeEntriesConfig
|
||||
---@field name "'native'"
|
||||
|
||||
---@class cmp.WildmenuEntriesConfig
|
||||
---@field name "'wildmenu'"
|
||||
---@field separator string|nil
|
||||
|
||||
---@class cmp.ViewConfig
|
||||
---@field public entries string|cmp.EntriesConfig
|
||||
---@field public separator string
|
||||
|
||||
return cmp
|
||||
|
||||
Reference in New Issue
Block a user