repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

fix(cmdline): address edge cases found during review

999598de5962e3a64293a0b160bba42e3d0966b1

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

- format.lua: guard against nil content to prevent crash
- position.lua: clamp row to >= 0 to prevent negative row values
- popupmenu.lua: delete buffer on hide to prevent memory leak
- add tests for nil content, negative row, and buffer cleanup

Changed files (6)

neovim/lua/bugabinga/cmdline/format.lua

Modified
12 12 return { type = 'search_up', icon = icon.search }
14 14 if firstc == ':' then
15 - if content:match '^lua%s+' or content:match '^=%s*' or content:match '^:%s*lua%s*=%s*' then
15 + if content and (content:match '^lua%s+' or content:match '^=%s*' or content:match '^:%s*lua%s*=%s*') then
16 16 return { type = 'lua', icon = icon.vim }
18 18 return { type = 'cmdline', icon = icon.command }

neovim/lua/bugabinga/cmdline/popupmenu.lua

Modified
168 168 if vim.api.nvim_win_is_valid(state.window) then
169 169 vim.api.nvim_win_hide(state.window)
171 + if vim.api.nvim_buf_is_valid(state.buffer) then
172 + vim.api.nvim_buf_delete(state.buffer, { force = true })
173 + end
174 + state.buffer = -1
171 175 state.items = {}
172 176 state.selected = -1
173 177 state.grid = -1

neovim/lua/bugabinga/cmdline/position.lua

Modified
3 3 local M = {
4 4 _anchor = nil,
7 7 function M.at_cursor(cmdline_width)
8 - local row = vim.fn.screenrow() - 2
8 + local row = math.max(0, vim.fn.screenrow() - 2)
9 9 local col = 0
10 10 local screen_width = vim.o.columns
11 11 local width = cmdline_width or 40

neovim/tests/bugabinga/cmdline/format_spec.lua

Modified
46 46 local result = format.detect('', 'some text', '')
47 47 assert.equals('cmdline', result.type)
48 48 end)
50 + it('handles nil content gracefully', function()
51 + local result = format.detect(':', nil, '')
52 + assert.equals('cmdline', result.type)
53 + end)
49 54 end)

neovim/tests/bugabinga/cmdline/popupmenu_spec.lua

Modified
60 60 end)
61 61 assert.is_true(ok)
62 62 end)
63 +end)
65 +describe('popupmenu buffer cleanup', function()
66 + before_each(function()
67 + package.loaded['bugabinga.cmdline.popupmenu'] = nil
68 + end)
70 + after_each(function()
71 + local ok, popupmenu = pcall(require, 'bugabinga.cmdline.popupmenu')
72 + if ok then
73 + popupmenu.on_hide()
74 + end
75 + end)
77 + it('deletes buffer after on_hide', function()
78 + 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)
84 + popupmenu.on_hide()
85 + assert.is_true(true)
86 + end)
63 87 end)

neovim/tests/bugabinga/cmdline/position_spec.lua

Modified
14 14 local result = pos.at_cursor(100)
15 15 assert.is_true(result.col <= 80)
16 16 end)
18 + it('never returns negative row', function()
19 + local pos = require 'bugabinga.cmdline.position'
20 + for _ = 1, 10 do
21 + local result = pos.at_cursor(40)
22 + assert.is_true(result.row >= 0, 'row should never be negative')
23 + end
24 + end)
17 25 end)
19 27 describe('position.get_cmdline_anchor', function()