repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(cmdline): refactor state sharing + add popupmenu match/kind highlights

7ae6fe5df1a66f7460ba0a663409f09adfaba2a3

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Refactoring:
- format.lua: add icon_highlight() and winhighlight() helpers, move ICON_HIGHLIGHT_MAP from render
- render.lua: remove ICON_HIGHLIGHT_MAP, get_winhighlight(), nvim_get_cmdline_info() hack; add apply_icon(), get_text()
- popupmenu.lua: use format.winhighlight() instead of inline lookup

Popupmenu highlights (items 6+7):
- Add PmenuMatch extmark over matched prefix in completion items
- Add PmenuKind extmark over kind icon region
- Prefix computed from render.get_text() + col in popupmenu_show handler
- 8 new tests covering all highlight cases

Changed files (6)

neovim/lua/bugabinga/cmdline/PLAN.md

Modified
41 41 ### 6. Popupmenu match highlighting
43 -- [ ] Compute typed prefix from cmdline text
44 -- [ ] In `format_item()`, split text into matched/unmatched portions
45 -- [ ] Apply `PmenuMatch` highlight to matched portion via extmarks
46 -- Files: `popupmenu.lua`
43 +- [x] Compute typed prefix from cmdline text via `render.get_text()` + `col`
44 + from popupmenu_show
45 +- [x] Apply `PmenuMatch` highlight to matched prefix portion via extmarks after
46 + `nvim_buf_set_lines`
47 +- [x] Case-insensitive prefix matching
48 +- Files: `popupmenu.lua`, `init.lua`, `render.lua`
48 50 ### 7. Kind-specific popupmenu highlights
50 -- [ ] Define `CompletionItemKind*` highlight groups
51 -- [ ] Apply per-kind highlight to icon portion in popupmenu
52 -- Files: `popupmenu.lua`, `nugu/init.lua`
52 +- [x] Use standard `PmenuKind`/`PmenuKindSel` highlight groups (already defined
53 + in `nugu/init.lua`)
54 +- [x] Apply per-kind highlight to icon portion in popupmenu via extmarks
55 +- Files: `popupmenu.lua`
54 57 ## Nice to Have

neovim/lua/bugabinga/cmdline/format.lua

Modified
12 12 cmdline = { border = 'CmdlineBorderCmdline', corner = 'CmdlineBorderCornerCmdline' },
15 +local ICON_HIGHLIGHT_MAP = {
16 + search_down = 'CmdlineIconSearch',
17 + search_up = 'CmdlineIconSearch',
18 + shell = 'CmdlineIconShell',
19 + help = 'CmdlineIconHelp',
20 + lua = 'CmdlineIconLua',
21 + input = 'CmdlineIconInput',
22 + cmdline = 'CmdlineIconCmdline',
15 25 return {
16 26 detect = function(firstc, content, prompt)
17 27 if prompt and prompt ~= '' then
40 50 return { type = 'cmdline', icon = icon.command, lang = 'vim' }
41 51 end,
42 53 border_highlight_map = BORDER_HIGHLIGHT_MAP,
55 + icon_highlight = function(type)
56 + return ICON_HIGHLIGHT_MAP[type] or 'CmdlineIconCmdline'
57 + end,
59 + winhighlight = function(type, base)
60 + local hl = BORDER_HIGHLIGHT_MAP[type] or BORDER_HIGHLIGHT_MAP.cmdline
61 + return string.format('Normal:%s,FloatBorder:%s,FloatBorderCorner:%s', base, hl.border, hl.corner)
62 + end,

neovim/lua/bugabinga/cmdline/init.lua

Modified
18 18 render.hide()
19 19 end,
20 20 popupmenu_show = function(items, selected, row, col, grid)
21 - popupmenu.on_show(items, selected, row, col, grid)
21 + local text = render.get_text() or ''
22 + local prefix = (col >= 0 and col < #text) and text:sub(col + 1) or ''
23 + popupmenu.on_show(items, selected, row, col, grid, prefix)
22 24 end,
23 25 popupmenu_select = function(selected)
24 26 popupmenu.on_select(selected)

neovim/lua/bugabinga/cmdline/popupmenu.lua

Modified
2 2 local icon = require 'std.icon'
3 3 local table = require 'std.table'
4 4 local position = require 'bugabinga.cmdline.position'
5 5 local format = require 'bugabinga.cmdline.format'
6 +local render = require 'bugabinga.cmdline.render'
7 8 local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.popupmenu'
15 16 format_type = 'cmdline',
18 -local function get_current_fmt_type()
19 - local ok, info = pcall(vim.api.nvim_get_cmdline_info)
20 - if not ok or not info then
21 - return 'cmdline'
22 - end
23 - local raw = info.cmdline and #info.cmdline > 0 and table.concat(info.cmdline, '') or ''
24 - local firstc = info.firstc and info.firstc ~= '' and info.firstc or ''
25 - local content = raw
26 - if firstc ~= '' and raw:find('^' .. firstc) then
27 - content = raw:sub(#firstc + 1)
28 - end
29 - local prompt = ''
30 - local fmt = format.detect(firstc, content, prompt)
31 - return fmt.type
32 -end
34 19 local function kind_icon(kind)
35 20 if not kind or kind == '' then
36 21 return ''
38 - return icon[kind] or icon.variable or ''
23 + return kind
41 26 local function format_item(item)
121 106 vim.wo[state.window].cursorline = true
122 107 vim.wo[state.window].winbar = ''
123 108 vim.wo[state.window].foldenable = false
124 - local current_fmt_type = get_current_fmt_type()
125 - local hl = format.border_highlight_map[current_fmt_type] or format.border_highlight_map.cmdline
126 - vim.wo[state.window].winhighlight = string.format(
127 - 'Normal:Pmenu,CursorLine:PmenuSel,FloatBorder:%s,FloatBorderCorner:%s',
128 - hl.border, hl.corner
129 - )
109 + vim.wo[state.window].winhighlight = format.winhighlight(state.format_type, 'Pmenu')
130 110 return state.window
133 113 local M = {}
135 -function M.on_show(items, selected, row, col, grid)
115 +local function apply_highlights(buf, items, lines, prefix)
116 + for i = 0, #lines - 1 do
117 + local item_idx = state.above and (#items - 1 - i) or i
118 + local item = items[item_idx + 1]
119 + if not item then
120 + break
121 + end
122 + local col_offset = 0
123 + local kind_str = kind_icon(item[2])
124 + if kind_str ~= '' then
125 + vim.api.nvim_buf_set_extmark(buf, ns, i, 0, {
126 + end_col = #kind_str + 1,
127 + hl_group = 'PmenuKind',
128 + })
129 + col_offset = #kind_str + 1
130 + end
131 + local abbr = item[1] or ''
132 + if prefix ~= '' and abbr:sub(1, #prefix):lower() == prefix:lower() then
133 + vim.api.nvim_buf_set_extmark(buf, ns, i, col_offset, {
134 + end_col = col_offset + #prefix,
135 + hl_group = 'PmenuMatch',
136 + })
137 + end
138 + end
139 +end
141 +function M.on_show(items, selected, row, col, grid, prefix)
136 142 if not items or #items == 0 then
137 143 M.on_hide()
138 144 return
140 146 state.items = items
141 147 state.selected = selected
142 148 state.grid = grid
149 + state.format_type = render.get_format_type()
143 150 local width = math.min(max_width(items) + 2, vim.o.columns - 4)
144 151 local pumheight = vim.o.pumheight
145 152 local height = pumheight > 0 and math.min(#items, pumheight) or #items
153 160 row = anchor.row,
154 161 col = anchor.col,
163 + vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
156 164 local lines = vim.iter(items):map(function(item)
157 165 return format_item(item)
158 166 end):totable()
160 168 lines = vim.iter(lines):rev():totable()
162 170 vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
171 + apply_highlights(buf, items, lines, prefix or '')
163 172 if selected >= 0 and selected < #items then
164 173 if state.above then
165 174 local visible_start = #items - height
178 187 return
180 189 state.selected = selected
181 - local current_fmt_type = get_current_fmt_type()
182 - local current_fmt = format.border_highlight_map[current_fmt_type] or format.border_highlight_map.cmdline
183 - vim.wo[state.window].winhighlight = string.format(
184 - 'Normal:Pmenu,CursorLine:PmenuSel,FloatBorder:%s,FloatBorderCorner:%s',
185 - current_fmt.border, current_fmt.corner
186 - )
187 190 if selected >= 0 and selected < #state.items then
188 191 if state.above then
189 192 local reversed_idx = #state.items - 1 - selected

neovim/lua/bugabinga/cmdline/render.lua

Modified
2 2 local format = require 'bugabinga.cmdline.format'
3 3 local position = require 'bugabinga.cmdline.position'
4 4 local cursor = require 'bugabinga.cmdline.cursor'
5 5 local highlight = require 'bugabinga.cmdline.highlight'
6 -local table = require 'std.table'
8 7 local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline'
9 8 local hl_ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.hl'
12 11 window = -1,
13 12 current_fmt = nil,
14 13 text = '',
14 + firstc = '',
15 + prompt = '',
17 -local ICON_HIGHLIGHT_MAP = {
18 - search_down = 'CmdlineIconSearch',
19 - search_up = 'CmdlineIconSearch',
20 - shell = 'CmdlineIconShell',
21 - help = 'CmdlineIconHelp',
22 - lua = 'CmdlineIconLua',
23 - input = 'CmdlineIconInput',
24 - cmdline = 'CmdlineIconCmdline',
27 -local function get_winhighlight(type)
28 - local hl = format.border_highlight_map[type] or format.border_highlight_map.cmdline
29 - return string.format('Normal:NormalFloat,FloatBorder:%s,FloatBorderCorner:%s', hl.border, hl.corner)
30 -end
32 18 local function get_buf()
33 19 if vim.api.nvim_buf_is_valid(state.buffer) then
34 20 return state.buffer
58 44 vim.wo[state.window].cursorline = false
59 45 vim.wo[state.window].winbar = ''
60 46 vim.wo[state.window].foldenable = false
61 - vim.wo[state.window].winhighlight = get_winhighlight(fmt.type)
47 + vim.wo[state.window].winhighlight = format.winhighlight(fmt.type, 'NormalFloat')
62 48 return state.window
65 51 local M = {}
53 +local function apply_icon(buf, fmt)
54 + vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
55 + local icon_hl = format.icon_highlight(fmt.type)
56 + vim.api.nvim_buf_set_extmark(buf, ns, 0, 0, {
57 + virt_text = { { fmt.icon, icon_hl } },
58 + virt_text_pos = 'overlay',
59 + })
60 +end
67 62 function M.show(cmdline_state)
68 63 local text = cmdline_state:text()
64 + local firstc = cmdline_state.firstc
65 + local prompt = cmdline_state.prompt
69 66 state.text = text
70 - local fmt = format.detect(cmdline_state.firstc, text, cmdline_state.prompt)
67 + state.firstc = firstc
68 + state.prompt = prompt
69 + local fmt = format.detect(firstc, text, prompt)
71 70 local fmt_changed = not state.current_fmt or fmt.type ~= state.current_fmt.type
72 71 state.current_fmt = fmt
73 72 local display_text = ' ' .. text .. ' '
95 94 vim.g.ui_cmdline_pos = { anchor.screenpos.row, anchor.screenpos.col }
97 96 if fmt_changed then
98 - vim.wo[win].winhighlight = get_winhighlight(fmt.type)
97 + vim.wo[win].winhighlight = format.winhighlight(fmt.type, 'NormalFloat')
100 99 vim.api.nvim_buf_set_lines(buf, 0, -1, false, { display_text })
101 - vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
102 - local icon_hl = ICON_HIGHLIGHT_MAP[fmt.type] or 'CmdlineIconCmdline'
103 - vim.api.nvim_buf_set_extmark(buf, ns, 0, 0, {
104 - virt_text = { { fmt.icon, icon_hl } },
105 - virt_text_pos = 'overlay',
106 - })
100 + apply_icon(buf, fmt)
107 101 highlight.apply(buf, hl_ns, text, fmt.lang, 0, 2)
108 102 local cursor_byte = cmdline_state:cursor_byte()
109 103 local cursor_col = 2 + cursor_byte
123 117 state.window = -1
124 118 state.current_fmt = nil
125 119 state.text = ''
120 + state.firstc = ''
121 + state.prompt = ''
126 122 position.set_cmdline_anchor(nil)
127 123 vim.g.ui_cmdline_pos = nil
131 127 if not vim.api.nvim_win_is_valid(state.window) then
132 128 return
134 - local info = vim.api.nvim_get_cmdline_info()
135 - local raw = info.cmdline and #info.cmdline > 0 and table.concat(info.cmdline, '') or ''
136 - local firstc = info.firstc and info.firstc ~= '' and info.firstc or ''
137 - local content = raw
138 - if firstc ~= '' and raw:find('^' .. firstc) then
139 - content = raw:sub(#firstc + 1)
140 - end
141 - if content ~= state.text then
142 - state.text = content
143 - local fmt = format.detect(firstc, content, cmdline_state.prompt)
144 - if fmt.type ~= state.current_fmt.type then
145 - state.current_fmt = fmt
146 - vim.wo[state.window].winhighlight = get_winhighlight(fmt.type)
147 - vim.api.nvim_buf_clear_namespace(state.buffer, ns, 0, -1)
148 - local icon_hl = ICON_HIGHLIGHT_MAP[fmt.type] or 'CmdlineIconCmdline'
149 - vim.api.nvim_buf_set_extmark(state.buffer, ns, 0, 0, {
150 - virt_text = { { fmt.icon, icon_hl } },
151 - virt_text_pos = 'overlay',
152 - })
153 - end
154 - end
155 130 local cursor_byte = cmdline_state:cursor_byte()
156 131 local cursor_col = 2 + cursor_byte
157 132 local anchor = position.get_cmdline_anchor()
166 141 return state.current_fmt and state.current_fmt.type or 'cmdline'
144 +function M.get_text()
145 + return state.text
146 +end
169 148 return M

neovim/tests/bugabinga/cmdline/popupmenu_spec.lua

Modified
21 21 it('creates window with items', function()
22 22 local popupmenu = require 'bugabinga.cmdline.popupmenu'
23 - local items = {
24 - { 'foo', 'function', '', '' },
25 - { 'bar', 'variable', '', '' },
26 - }
27 - local ok = pcall(function()
28 - popupmenu.on_show(items, 0, 0, 0, -1)
29 - end)
30 - assert.is_true(ok)
31 - popupmenu.on_hide()
23 + local initial_win_count = #vim.api.nvim_list_wins()
24 + popupmenu.on_show({{'foo', 'Variable', ''}, {'bar', 'Function', ''}}, 0, 0, 0, -1)
25 + local wins = vim.api.nvim_list_wins()
26 + assert.equals(initial_win_count + 1, #wins, 'should create a new window')
27 + local new_win = wins[#wins]
28 + local config = vim.api.nvim_win_get_config(new_win)
29 + assert.is_table(config, 'window config should be a table')
30 + local buf = vim.api.nvim_win_get_buf(new_win)
31 + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
32 + assert.is_true(#lines > 0, 'buffer should have lines')
32 33 end)
34 35 it('hides safely when no window exists', function()
77 78 it('deletes buffer after on_hide', function()
78 79 local popupmenu = require 'bugabinga.cmdline.popupmenu'
79 - local items = {
80 - { 'foo', 'function', '', '' },
81 - { 'bar', 'variable', '', '' },
82 - }
83 - popupmenu.on_show(items, 0, 0, 0, -1)
80 + local initial_buf_count = #vim.api.nvim_list_bufs()
81 + popupmenu.on_show({{'foo', 'Variable', ''}}, 0, 0, 0, -1)
82 + local buf_count_after_show = #vim.api.nvim_list_bufs()
83 + assert.is_true(buf_count_after_show > initial_buf_count, 'should create a new buffer')
84 + local wins_before_hide = #vim.api.nvim_list_wins()
84 85 popupmenu.on_hide()
85 - assert.is_true(true)
86 + local wins_after_hide = #vim.api.nvim_list_wins()
87 + assert.is_true(wins_after_hide < wins_before_hide, 'window should be hidden')
88 + assert.is_true(buf_count_after_show > #vim.api.nvim_list_bufs(), 'buffer should be deleted')
89 + local ok = pcall(popupmenu.on_hide)
90 + assert.is_true(ok, 'on_hide should be idempotent')
91 + end)
92 +end)
94 +describe('popupmenu highlights', function()
95 + before_each(function()
96 + package.loaded['bugabinga.cmdline.popupmenu'] = nil
97 + package.loaded['bugabinga.cmdline.render'] = nil
98 + package.loaded['bugabinga.cmdline.position'] = nil
99 + end)
101 + after_each(function()
102 + local ok, popupmenu = pcall(require, 'bugabinga.cmdline.popupmenu')
103 + if ok then
104 + popupmenu.on_hide()
105 + end
106 + end)
108 + local function find_buf()
109 + local wins = vim.api.nvim_list_wins()
110 + for _, win in ipairs(wins) do
111 + local config = vim.api.nvim_win_get_config(win)
112 + if config.zindex and config.zindex == 201 then
113 + return vim.api.nvim_win_get_buf(win)
114 + end
115 + end
116 + return nil
117 + end
119 + local function get_extmarks(buf)
120 + local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.popupmenu'
121 + return vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
122 + end
124 + it('applies PmenuMatch extmark when prefix matches abbr', function()
125 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
126 + popupmenu.on_show({{'foobar', '', ''}}, 0, 0, 0, -1, 'foo')
127 + local buf = find_buf()
128 + assert.is_not_nil(buf, 'popupmenu buffer should exist')
129 + local extmarks = get_extmarks(buf)
130 + local match_marks = vim.iter(extmarks):filter(function(m)
131 + return m[4].hl_group == 'PmenuMatch'
132 + end):totable()
133 + assert.equals(1, #match_marks, 'should have one PmenuMatch extmark')
134 + assert.equals(0, match_marks[1][3], 'PmenuMatch should start at col 0')
135 + assert.equals(3, match_marks[1][4].end_col, 'PmenuMatch should span 3 bytes (foo)')
136 + end)
138 + it('applies PmenuKind extmark over kind icon', function()
139 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
140 + popupmenu.on_show({{'bar', 'Function', ''}}, 0, 0, 0, -1, '')
141 + local buf = find_buf()
142 + assert.is_not_nil(buf)
143 + local extmarks = get_extmarks(buf)
144 + local kind_marks = vim.iter(extmarks):filter(function(m)
145 + return m[4].hl_group == 'PmenuKind'
146 + end):totable()
147 + assert.equals(1, #kind_marks, 'should have one PmenuKind extmark')
148 + assert.equals(0, kind_marks[1][3], 'PmenuKind should start at col 0')
149 + assert.is_true(kind_marks[1][4].end_col > 1, 'PmenuKind should span icon + space')
150 + end)
152 + it('applies no extmarks when prefix empty and no kind', function()
153 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
154 + popupmenu.on_show({{'baz', '', ''}}, 0, 0, 0, -1, '')
155 + local buf = find_buf()
156 + assert.is_not_nil(buf)
157 + local extmarks = get_extmarks(buf)
158 + assert.equals(0, #extmarks, 'should have no extmarks')
159 + end)
161 + it('applies both PmenuMatch and PmenuKind when both apply', function()
162 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
163 + local icon = require 'std.icon'
164 + popupmenu.on_show({{'foobar', 'Variable', ''}}, 0, 0, 0, -1, 'foo')
165 + local buf = find_buf()
166 + assert.is_not_nil(buf)
167 + local extmarks = get_extmarks(buf)
168 + local hl_groups = vim.iter(extmarks):map(function(m)
169 + return m[4].hl_group
170 + end):totable()
171 + assert.is_true(vim.list_contains(hl_groups, 'PmenuMatch'), 'should have PmenuMatch')
172 + assert.is_true(vim.list_contains(hl_groups, 'PmenuKind'), 'should have PmenuKind')
173 + end)
175 + it('applies no PmenuMatch when prefix does not match abbr', function()
176 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
177 + popupmenu.on_show({{'bar', '', ''}}, 0, 0, 0, -1, 'foo')
178 + local buf = find_buf()
179 + assert.is_not_nil(buf)
180 + local extmarks = get_extmarks(buf)
181 + local match_marks = vim.iter(extmarks):filter(function(m)
182 + return m[4].hl_group == 'PmenuMatch'
183 + end):totable()
184 + assert.equals(0, #match_marks, 'should have no PmenuMatch when prefix does not match')
185 + end)
187 + it('offsets PmenuMatch by kind icon when kind present', function()
188 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
189 + popupmenu.on_show({{'foobar', 'Function', ''}}, 0, 0, 0, -1, 'foo')
190 + local buf = find_buf()
191 + assert.is_not_nil(buf)
192 + local extmarks = get_extmarks(buf)
193 + local match_marks = vim.iter(extmarks):filter(function(m)
194 + return m[4].hl_group == 'PmenuMatch'
195 + end):totable()
196 + assert.equals(1, #match_marks)
197 + local kind_marks = vim.iter(extmarks):filter(function(m)
198 + return m[4].hl_group == 'PmenuKind'
199 + end):totable()
200 + assert.equals(1, #kind_marks, 'should have a PmenuKind extmark')
201 + assert.equals(kind_marks[1][4].end_col, match_marks[1][3], 'PmenuMatch should start where PmenuKind ended')
202 + assert.equals(match_marks[1][3] + 3, match_marks[1][4].end_col, 'PmenuMatch should span 3 bytes')
203 + end)
205 + it('highlights multiple items each with their own match extmark', function()
206 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
207 + popupmenu.on_show({{'foo', '', ''}, {'fbar', '', ''}}, 0, 0, 0, -1, 'f')
208 + local buf = find_buf()
209 + assert.is_not_nil(buf)
210 + local extmarks = get_extmarks(buf)
211 + local match_marks = vim.iter(extmarks):filter(function(m)
212 + return m[4].hl_group == 'PmenuMatch'
213 + end):totable()
214 + assert.equals(2, #match_marks, 'each item should have its own PmenuMatch extmark')
215 + end)
217 + it('does not error when prefix is nil', function()
218 + local popupmenu = require 'bugabinga.cmdline.popupmenu'
219 + local ok = pcall(function()
220 + popupmenu.on_show({{'foo', '', ''}}, 0, 0, 0, -1)
221 + end)
222 + assert.is_true(ok, 'should not error with nil prefix')
223 + local buf = find_buf()
224 + assert.is_not_nil(buf)
225 + local extmarks = get_extmarks(buf)
226 + local match_marks = vim.iter(extmarks):filter(function(m)
227 + return m[4].hl_group == 'PmenuMatch'
228 + end):totable()
229 + assert.equals(0, #match_marks, 'should have no PmenuMatch with nil/empty prefix')