Add item fields order

Fixes #337
This commit is contained in:
hrsh7th
2021-10-12 20:49:21 +09:00
parent 2a8dc6208a
commit 49acc84853
5 changed files with 26 additions and 11 deletions

View File

@@ -355,6 +355,10 @@ The documentation window's max height.
The documentation window's zindex. The documentation window's zindex.
#### formatting.fields (type: cmp.ItemField[])
The order of item's fields for completion menu.
#### formatting.format (type: fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem) #### formatting.format (type: fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem)
A function to customize completion menu. A function to customize completion menu.

View File

@@ -92,6 +92,7 @@ return function()
}, },
formatting = { formatting = {
fields = { 'abbr', 'kind', 'menu' },
format = function(_, vim_item) format = function(_, vim_item)
return vim_item return vim_item
end, end,

View File

@@ -27,6 +27,12 @@ cmp.PreselectMode = {}
cmp.PreselectMode.Item = 'item' cmp.PreselectMode.Item = 'item'
cmp.PreselectMode.None = 'none' cmp.PreselectMode.None = 'none'
---@alias cmp.ItemField "'abbr'" | "'kind'" | "'menu'"
cmp.ItemField = {}
cmp.ItemField.Abbr = 'abbr'
cmp.ItemField.Kind = 'kind'
cmp.ItemField.Menu = 'menu'
---@class cmp.ContextOption ---@class cmp.ContextOption
---@field public reason cmp.ContextReason|nil ---@field public reason cmp.ContextReason|nil
@@ -91,6 +97,7 @@ cmp.PreselectMode.None = 'none'
---@field public comparators function[] ---@field public comparators function[]
---@class cmp.FormattingConfig ---@class cmp.FormattingConfig
---@field public fields cmp.ItemField[]
---@field public format fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem ---@field public format fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem
---@class cmp.SnippetConfig ---@class cmp.SnippetConfig

View File

@@ -103,9 +103,7 @@ keymap.feedkeys = setmetatable({
return callback and callback() or nil return callback and callback() or nil
end end
vim.api.nvim_feedkeys(keymap.t('<Cmd>set eventignore=all<CR>'), 'n', true)
vim.api.nvim_feedkeys(keys, mode, true) vim.api.nvim_feedkeys(keys, mode, true)
vim.api.nvim_feedkeys(keymap.t(('<Cmd>set eventignore=%s<CR>'):format(vim.o.eventignore)), 'n', true)
if callback then if callback then
if vim.fn.reg_recording() == '' then if vim.fn.reg_recording() == '' then

View File

@@ -45,12 +45,17 @@ custom_entries_view.new = function()
return return
end end
local c = config.get().formatting.fields
for i = top, bot do for i = top, bot do
local e = self.entries[i + 1] local e = self.entries[i + 1]
if e then if e then
local v = e:get_view(self.offset) local v = e:get_view(self.offset)
local o = 1 local o = 1
for _, key in ipairs({ 'abbr', 'kind', 'menu' }) do local a = 0
for _, key in ipairs(c) do
if key == types.cmp.ItemField.Abbr then
a = o
end
vim.api.nvim_buf_set_extmark(buf, custom_entries_view.ns, i, o, { vim.api.nvim_buf_set_extmark(buf, custom_entries_view.ns, i, o, {
end_line = i, end_line = i,
end_col = o + v[key].bytes, end_col = o + v[key].bytes,
@@ -60,10 +65,11 @@ custom_entries_view.new = function()
}) })
o = o + v[key].bytes + (self.column_width[key] - v[key].width) + 1 o = o + v[key].bytes + (self.column_width[key] - v[key].width) + 1
end end
for _, m in ipairs(e.matches or {}) do for _, m in ipairs(e.matches or {}) do
vim.api.nvim_buf_set_extmark(buf, custom_entries_view.ns, i, m.word_match_start, { vim.api.nvim_buf_set_extmark(buf, custom_entries_view.ns, i, a + m.word_match_start - 1, {
end_line = i, end_line = i,
end_col = m.word_match_end + 1, end_col = a + m.word_match_end,
hl_group = m.fuzzy and 'CmpItemAbbrMatchFuzzy' or 'CmpItemAbbrMatch', hl_group = m.fuzzy and 'CmpItemAbbrMatchFuzzy' or 'CmpItemAbbrMatch',
hl_mode = 'combine', hl_mode = 'combine',
ephemeral = true, ephemeral = true,
@@ -177,18 +183,17 @@ custom_entries_view.draw = function(self)
local topline = info.topline - 1 local topline = info.topline - 1
local botline = info.topline + info.height - 1 local botline = info.topline + info.height - 1
local texts = {} local texts = {}
local fields = config.get().formatting.fields
for i = topline, botline - 1 do for i = topline, botline - 1 do
local e = self.entries[i + 1] local e = self.entries[i + 1]
if e then if e then
local view = e:get_view(self.offset) local view = e:get_view(self.offset)
local text = {} local text = {}
table.insert(text, ' ') table.insert(text, ' ')
table.insert(text, view.abbr.text) for _, field in ipairs(fields) do
table.insert(text, string.rep(' ', 1 + self.column_width.abbr - view.abbr.width)) table.insert(text, view[field].text)
table.insert(text, view.kind.text) table.insert(text, string.rep(' ', 1 + self.column_width[field] - view[field].width))
table.insert(text, string.rep(' ', 1 + self.column_width.kind - view.kind.width)) end
table.insert(text, view.menu.text)
table.insert(text, string.rep(' ', 1 + self.column_width.menu - view.menu.width))
table.insert(text, ' ') table.insert(text, ' ')
table.insert(texts, table.concat(text, '')) table.insert(texts, table.concat(text, ''))
end end