From 49acc848531b8cbe051c598dddd0cc46ec64f4b4 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Tue, 12 Oct 2021 20:49:21 +0900 Subject: [PATCH] Add item fields order Fixes #337 --- README.md | 4 ++++ lua/cmp/config/default.lua | 1 + lua/cmp/types/cmp.lua | 7 +++++++ lua/cmp/utils/keymap.lua | 2 -- lua/cmp/view/custom_entries_view.lua | 23 ++++++++++++++--------- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ce33d84..3b5fe79 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,10 @@ The documentation window's max height. 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) A function to customize completion menu. diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 5b294e8..eccefaf 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -92,6 +92,7 @@ return function() }, formatting = { + fields = { 'abbr', 'kind', 'menu' }, format = function(_, vim_item) return vim_item end, diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index abed474..0778e5a 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -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 diff --git a/lua/cmp/utils/keymap.lua b/lua/cmp/utils/keymap.lua index 3439067..41216c4 100644 --- a/lua/cmp/utils/keymap.lua +++ b/lua/cmp/utils/keymap.lua @@ -103,9 +103,7 @@ keymap.feedkeys = setmetatable({ return callback and callback() or nil end - vim.api.nvim_feedkeys(keymap.t('set eventignore=all'), 'n', true) vim.api.nvim_feedkeys(keys, mode, true) - vim.api.nvim_feedkeys(keymap.t(('set eventignore=%s'):format(vim.o.eventignore)), 'n', true) if callback then if vim.fn.reg_recording() == '' then diff --git a/lua/cmp/view/custom_entries_view.lua b/lua/cmp/view/custom_entries_view.lua index d5f70b4..80d7881 100644 --- a/lua/cmp/view/custom_entries_view.lua +++ b/lua/cmp/view/custom_entries_view.lua @@ -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