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

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

View File

@@ -27,6 +27,12 @@ cmp.PreselectMode = {}
cmp.PreselectMode.Item = 'item'
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
---@field public reason cmp.ContextReason|nil
@@ -91,6 +97,7 @@ cmp.PreselectMode.None = 'none'
---@field public comparators function[]
---@class cmp.FormattingConfig
---@field public fields cmp.ItemField[]
---@field public format fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem
---@class cmp.SnippetConfig

View File

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

View File

@@ -45,12 +45,17 @@ custom_entries_view.new = function()
return
end
local c = config.get().formatting.fields
for i = top, bot do
local e = self.entries[i + 1]
if e then
local v = e:get_view(self.offset)
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, {
end_line = i,
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
end
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_col = m.word_match_end + 1,
end_col = a + m.word_match_end,
hl_group = m.fuzzy and 'CmpItemAbbrMatchFuzzy' or 'CmpItemAbbrMatch',
hl_mode = 'combine',
ephemeral = true,
@@ -177,18 +183,17 @@ custom_entries_view.draw = function(self)
local topline = info.topline - 1
local botline = info.topline + info.height - 1
local texts = {}
local fields = config.get().formatting.fields
for i = topline, botline - 1 do
local e = self.entries[i + 1]
if e then
local view = e:get_view(self.offset)
local text = {}
table.insert(text, ' ')
table.insert(text, view.abbr.text)
table.insert(text, string.rep(' ', 1 + self.column_width.abbr - view.abbr.width))
table.insert(text, view.kind.text)
table.insert(text, string.rep(' ', 1 + self.column_width.kind - view.kind.width))
table.insert(text, view.menu.text)
table.insert(text, string.rep(' ', 1 + self.column_width.menu - view.menu.width))
for _, field in ipairs(fields) do
table.insert(text, view[field].text)
table.insert(text, string.rep(' ', 1 + self.column_width[field] - view[field].width))
end
table.insert(text, ' ')
table.insert(texts, table.concat(text, ''))
end