Add filetype config
Use view.entries == 'native' instead of experimental.native_menu
This commit is contained in:
@@ -97,6 +97,15 @@ lua <<EOF
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- Set configuration for specific filetype.
|
||||||
|
cmp.setup.filetype('gitcommit', {
|
||||||
|
sources = cmp.config.sources({
|
||||||
|
{ name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it.
|
||||||
|
}, {
|
||||||
|
{ name = 'buffer' },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
|
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
|
||||||
cmp.setup.cmdline('/', {
|
cmp.setup.cmdline('/', {
|
||||||
sources = {
|
sources = {
|
||||||
|
|||||||
47
doc/cmp.txt
47
doc/cmp.txt
@@ -48,7 +48,7 @@ The recommendation configurations are the below.
|
|||||||
|
|
||||||
NOTE:
|
NOTE:
|
||||||
1. You must setup `snippet.expand` function.
|
1. You must setup `snippet.expand` function.
|
||||||
2. The `cmp.setup.cmdline` won't work if you specified the `experimental.native_menu` option.
|
2. The `cmp.setup.cmdline` won't work if you are using `native` completion menu.
|
||||||
3. You can disable the `default` options via specifying `cmp.config.disable` value.
|
3. You can disable the `default` options via specifying `cmp.config.disable` value.
|
||||||
>
|
>
|
||||||
call plug#begin(s:plug_dir)
|
call plug#begin(s:plug_dir)
|
||||||
@@ -148,6 +148,9 @@ NOTE: You can call these functions in mapping via `<Cmd>lua require('cmp').compl
|
|||||||
*cmp.setup* (config: cmp.ConfigSchema)
|
*cmp.setup* (config: cmp.ConfigSchema)
|
||||||
Setup global configuration. See configuration option.
|
Setup global configuration. See configuration option.
|
||||||
|
|
||||||
|
*cmp.setup.filetype* (filetype: string, config: cmp.ConfigSchema)
|
||||||
|
Setup filetype configuration to the specific filetype.
|
||||||
|
|
||||||
*cmp.setup.buffer* (config: cmp.ConfigSchema)
|
*cmp.setup.buffer* (config: cmp.ConfigSchema)
|
||||||
Setup buffer configuration to the current buffer.
|
Setup buffer configuration to the current buffer.
|
||||||
|
|
||||||
@@ -380,18 +383,25 @@ completion.autocomplete~
|
|||||||
nvim-cmp does not completion automatically but you can still use the manual
|
nvim-cmp does not completion automatically but you can still use the manual
|
||||||
completion though.
|
completion though.
|
||||||
|
|
||||||
*cmp-config.formatting.format*
|
*cmp-config.completion.completeopt*
|
||||||
formatting.format~
|
completion.completeopt~
|
||||||
`fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem`
|
`string`
|
||||||
The function to customize the completion menu appearance. See |complete-items|.
|
The vim's completeopt like setting. See 'completeopt'.
|
||||||
NOTE: The `vim.CompletedItem` can have special properties `abbr_hl_group`,
|
Besically, You don't need to modify this.
|
||||||
`kind_hl_group` and `menu_hl_group`.
|
|
||||||
|
|
||||||
*cmp-config.formatting.fields*
|
*cmp-config.formatting.fields*
|
||||||
formatting.fields~
|
formatting.fields~
|
||||||
`cmp.ItemField[]`
|
`cmp.ItemField[]`
|
||||||
The array of completion menu field to specify the order of them.
|
The array of completion menu field to specify the order of them.
|
||||||
|
|
||||||
|
*cmp-config.formatting.format*
|
||||||
|
formatting.format~
|
||||||
|
`fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem`
|
||||||
|
The function to customize the completion menu appearance. See |complete-items|.
|
||||||
|
This value also can be used to modify `dup` property.
|
||||||
|
NOTE: The `vim.CompletedItem` can have special properties `abbr_hl_group`,
|
||||||
|
`kind_hl_group` and `menu_hl_group`.
|
||||||
|
|
||||||
*cmp-config.sorting.priority_weight*
|
*cmp-config.sorting.priority_weight*
|
||||||
sorting.priority_weight~
|
sorting.priority_weight~
|
||||||
`number`
|
`number`
|
||||||
@@ -473,16 +483,17 @@ sources[n].group_index~
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
<
|
<
|
||||||
|
*cmp-config.view*
|
||||||
|
view~
|
||||||
|
`{ entries: cmp.EntriesConfig|string }`
|
||||||
|
Specify the view class to customize appearance.
|
||||||
|
Currently, the possible configurations are:
|
||||||
|
|
||||||
*cmp-config.experimental.ghost_text*
|
*cmp-config.experimental.ghost_text*
|
||||||
experimental.ghost_text~
|
experimental.ghost_text~
|
||||||
`boolean | { hl_group = string }`
|
`boolean | { hl_group = string }`
|
||||||
The boolean value to enable or disable the ghost_text feature.
|
The boolean value to enable or disable the ghost_text feature.
|
||||||
|
|
||||||
*cmp-config.experimental.native_menu*
|
|
||||||
experimental.native_menu~
|
|
||||||
`boolean`
|
|
||||||
The boolean value to enable or disable the native completion menu.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
@@ -592,12 +603,12 @@ How to setup on the specific buffer?~
|
|||||||
|
|
||||||
You can setup buffer specific configuration like this.
|
You can setup buffer specific configuration like this.
|
||||||
>
|
>
|
||||||
autocmd FileType markdown lua require('cmp').setup.buffer {
|
cmp.setup.filetype('markdown', {
|
||||||
\ sources = {
|
sources = {
|
||||||
\ { name = 'path' },
|
{ name = 'path' },
|
||||||
\ { name = 'buffer' },
|
{ name = 'buffer' },
|
||||||
\ }
|
}
|
||||||
\ }
|
})
|
||||||
<
|
<
|
||||||
|
|
||||||
How to customize menu appearance?~
|
How to customize menu appearance?~
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ config.global = require('cmp.config.default')()
|
|||||||
---@type table<number, cmp.ConfigSchema>
|
---@type table<number, cmp.ConfigSchema>
|
||||||
config.buffers = {}
|
config.buffers = {}
|
||||||
|
|
||||||
|
---@type table<string, cmp.ConfigSchema>
|
||||||
|
config.filetypes = {}
|
||||||
|
|
||||||
---@type table<string, cmp.ConfigSchema>
|
---@type table<string, cmp.ConfigSchema>
|
||||||
config.cmdline = {}
|
config.cmdline = {}
|
||||||
|
|
||||||
@@ -40,6 +43,15 @@ config.set_buffer = function(c, bufnr)
|
|||||||
config.buffers[bufnr].revision = revision + 1
|
config.buffers[bufnr].revision = revision + 1
|
||||||
end
|
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
|
---Set configuration for cmdline
|
||||||
---@param c cmp.ConfigSchema
|
---@param c cmp.ConfigSchema
|
||||||
---@param cmdtype string
|
---@param cmdtype string
|
||||||
@@ -59,23 +71,48 @@ end
|
|||||||
|
|
||||||
---@return cmp.ConfigSchema
|
---@return cmp.ConfigSchema
|
||||||
config.get = function()
|
config.get = function()
|
||||||
local global = config.global
|
local global_config = config.global
|
||||||
if config.onetime.sources then
|
if config.onetime.sources then
|
||||||
local onetime = config.onetime
|
local onetime_config = config.onetime
|
||||||
return config.cache:ensure({ 'get_onetime', global.revision or 0, onetime.revision or 0 }, function()
|
return config.cache:ensure({
|
||||||
return misc.merge(config.normalize(onetime), config.normalize(global))
|
'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)
|
end)
|
||||||
elseif api.is_cmdline_mode() then
|
elseif api.is_cmdline_mode() then
|
||||||
local cmdtype = vim.fn.getcmdtype()
|
local cmdtype = vim.fn.getcmdtype()
|
||||||
local cmdline = config.cmdline[cmdtype] or { revision = 1, sources = {} }
|
local cmdline_config = config.cmdline[cmdtype] or { revision = 1, sources = {} }
|
||||||
return config.cache:ensure({ 'get_cmdline', cmdtype, global.revision or 0, cmdline.revision or 0 }, function()
|
return config.cache:ensure({
|
||||||
return misc.merge(config.normalize(cmdline), config.normalize(global))
|
'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)
|
end)
|
||||||
else
|
else
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
local buffer = config.buffers[bufnr] or { revision = 1 }
|
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
|
||||||
return config.cache:ensure({ 'get_buffer', bufnr, global.revision or 0, buffer.revision or 0 }, function()
|
local buffer_config = config.buffers[bufnr] or { revision = 1 }
|
||||||
return misc.merge(config.normalize(buffer), config.normalize(global))
|
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
|
end
|
||||||
end
|
end
|
||||||
@@ -102,6 +139,18 @@ config.get_source_config = function(name)
|
|||||||
return nil
|
return nil
|
||||||
end
|
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
|
---Normalize mapping key
|
||||||
---@param c cmp.ConfigSchema
|
---@param c cmp.ConfigSchema
|
||||||
---@return cmp.ConfigSchema
|
---@return cmp.ConfigSchema
|
||||||
@@ -114,6 +163,20 @@ config.normalize = function(c)
|
|||||||
c.mapping = normalized
|
c.mapping = normalized
|
||||||
end
|
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
|
if c.sources then
|
||||||
for _, s in ipairs(c.sources) do
|
for _, s in ipairs(c.sources) do
|
||||||
if s.opts and not s.option then
|
if s.opts and not s.option then
|
||||||
|
|||||||
@@ -10,53 +10,9 @@ return function()
|
|||||||
enabled = function()
|
enabled = function()
|
||||||
return vim.api.nvim_buf_get_option(0, 'buftype') ~= 'prompt'
|
return vim.api.nvim_buf_get_option(0, 'buftype') ~= 'prompt'
|
||||||
end,
|
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,
|
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 = {
|
mapping = {
|
||||||
['<Down>'] = mapping({
|
['<Down>'] = mapping({
|
||||||
i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
|
i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
|
||||||
@@ -79,7 +35,7 @@ return function()
|
|||||||
['<Tab>'] = mapping({
|
['<Tab>'] = mapping({
|
||||||
c = function(fallback)
|
c = function(fallback)
|
||||||
local cmp = require('cmp')
|
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
|
if cmp.visible() then
|
||||||
cmp.select_next_item()
|
cmp.select_next_item()
|
||||||
else
|
else
|
||||||
@@ -93,7 +49,7 @@ return function()
|
|||||||
['<S-Tab>'] = mapping({
|
['<S-Tab>'] = mapping({
|
||||||
c = function(fallback)
|
c = function(fallback)
|
||||||
local cmp = require('cmp')
|
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
|
if cmp.visible() then
|
||||||
cmp.select_prev_item()
|
cmp.select_prev_item()
|
||||||
else
|
else
|
||||||
@@ -110,6 +66,21 @@ return function()
|
|||||||
['<C-e>'] = mapping.abort(),
|
['<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 = {
|
formatting = {
|
||||||
fields = { 'abbr', 'kind', 'menu' },
|
fields = { 'abbr', 'kind', 'menu' },
|
||||||
format = function(_, vim_item)
|
format = function(_, vim_item)
|
||||||
@@ -117,15 +88,44 @@ return function()
|
|||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
experimental = {
|
sorting = {
|
||||||
native_menu = false,
|
priority_weight = 2,
|
||||||
ghost_text = false,
|
comparators = {
|
||||||
|
compare.offset,
|
||||||
|
compare.exact,
|
||||||
|
compare.score,
|
||||||
|
compare.recently_used,
|
||||||
|
compare.kind,
|
||||||
|
compare.sort_text,
|
||||||
|
compare.length,
|
||||||
|
compare.order,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
sources = {},
|
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 = {
|
view = {
|
||||||
entries = { name = 'custom' },
|
entries = 'custom',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ cmp.status = function()
|
|||||||
for _, name in ipairs(kinds.available) do
|
for _, name in ipairs(kinds.available) do
|
||||||
vim.api.nvim_echo({ { ('- %s\n'):format(name), 'Normal' } }, false, {})
|
vim.api.nvim_echo({ { ('- %s\n'):format(name), 'Normal' } }, false, {})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if #kinds.unavailable > 0 then
|
if #kinds.unavailable > 0 then
|
||||||
vim.api.nvim_echo({ { '\n', 'Normal' } }, false, {})
|
vim.api.nvim_echo({ { '\n', 'Normal' } }, false, {})
|
||||||
@@ -268,6 +268,9 @@ cmp.setup = setmetatable({
|
|||||||
global = function(c)
|
global = function(c)
|
||||||
config.set_global(c)
|
config.set_global(c)
|
||||||
end,
|
end,
|
||||||
|
filetype = function(filetype, c)
|
||||||
|
config.set_filetype(c, filetype)
|
||||||
|
end,
|
||||||
buffer = function(c)
|
buffer = function(c)
|
||||||
config.set_buffer(c, vim.api.nvim_get_current_buf())
|
config.set_buffer(c, vim.api.nvim_get_current_buf())
|
||||||
end,
|
end,
|
||||||
|
|||||||
@@ -131,12 +131,19 @@ cmp.ItemField.Menu = 'menu'
|
|||||||
---@field public max_item_count number|nil
|
---@field public max_item_count number|nil
|
||||||
---@field public group_index number|nil
|
---@field public group_index number|nil
|
||||||
|
|
||||||
---@class cmp.EntriesConfig
|
---@class cmp.ViewConfig
|
||||||
---@field name string
|
---@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
|
---@field separator string|nil
|
||||||
|
|
||||||
---@class cmp.ViewConfig
|
|
||||||
---@field public entries string|cmp.EntriesConfig
|
|
||||||
---@field public separator string
|
|
||||||
|
|
||||||
return cmp
|
return cmp
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ vim.cmd [[
|
|||||||
]]
|
]]
|
||||||
|
|
||||||
misc.set(_G, { 'cmp', 'plugin', 'cmdline', 'enter' }, function()
|
misc.set(_G, { 'cmp', 'plugin', 'cmdline', 'enter' }, function()
|
||||||
if config.get().experimental.native_menu then
|
if config.is_native_menu() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if vim.fn.expand('<afile>')~= '=' then
|
if vim.fn.expand('<afile>')~= '=' then
|
||||||
|
|||||||
Reference in New Issue
Block a user