repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(cmdline): prompt-as-title for input() with virt_lines fallback

c160e9534a405b520ebc84829d6aca0acd94d5bc

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (6)

neovim/justfile

Modified
15 15 @nu puk.nu update
16 16 @if ( "puk.plugins.updated.nuon" | path exists ) { mv puk.plugins.updated.nuon puk.plugins.nuon }
18 -# delete all current neovim state, cache and other ephemeral data. does NOT delete the configs
18 +# DANGER NEVER CALL! delete all current neovim state, cache and other ephemeral data. does NOT delete the configs
19 19 nuke:
20 20 rm --interactive --recursive --force (nvim "--headless" "--clean" +'lua print(vim.fn.stdpath[[data]])' +q | complete | get stderr)
21 21 rm --interactive --recursive --force (nvim "--headless" "--clean" +'lua print(vim.fn.stdpath[[cache]])' +q | complete | get stderr)
22 22 rm --interactive --recursive --force (nvim "--headless" "--clean" +'lua print(vim.fn.stdpath[[state]])' +q | complete | get stderr)
24 -# run all tests
25 -test:
26 - nvim --headless -c "packadd plenary" -c "PlenaryBustedDirectory tests/" +q
24 +# run all tests, or only the ones in the directory of file given
25 +test path="tests":
26 + timeit { nvim --headless --noplugin -u tests/minimal_init.lua -c "PlenaryBustedDirectory {{ path }} {minimal_init = 'tests/minimal_init.lua'}" +q }
28 -# run a single test file, e.g. just test-file tests/std/const_spec.lua
29 -test-file file:
30 - nvim --headless -c "packadd plenary" -c 'PlenaryBustedFile {{ file }}' +q
28 +# run manual tests
29 +test-manual:
30 + nvim -c "set rtp^=." -c "luafile tests/manual/input.lua"
32 32 # run ast-grep refactoring on the neovim configuration
33 33 refac:

neovim/lua/bugabinga/cmdline/format.lua

Modified
60 60 local hl = BORDER_HIGHLIGHT_MAP[type] or BORDER_HIGHLIGHT_MAP.cmdline
61 61 return string.format('Normal:%s,FloatBorder:%s,FloatBorderCorner:%s', base, hl.border, hl.corner)
62 62 end,
64 + prompt_lines = function(prompt, max_width)
65 + if not prompt or prompt == '' then
66 + return {}
67 + end
68 + vim.validate({ prompt = { prompt, 'string' }, max_width = { max_width, 'number' } })
69 + local lines = {}
70 + for line in (prompt .. '\n'):gmatch('([^\n]*)\n') do
71 + table.insert(lines, line)
72 + end
73 + if #lines > 0 and lines[#lines] == '' then
74 + table.remove(lines)
75 + end
76 + local result = {}
77 + for _, line in ipairs(lines) do
78 + if vim.fn.strdisplaywidth(line) <= max_width then
79 + table.insert(result, line)
80 + else
81 + local remaining = line
82 + while remaining ~= '' and vim.fn.strdisplaywidth(remaining) > max_width do
83 + local cutoff = 0
84 + for i = 1, #remaining do
85 + if vim.fn.strdisplaywidth(remaining:sub(1, i)) > max_width then
86 + cutoff = i - 1
87 + break
88 + end
89 + end
90 + if cutoff == 0 then
91 + cutoff = math.min(max_width, #remaining)
92 + end
93 + local chunk = remaining:sub(1, cutoff)
94 + local last_space = chunk:match '()%s[^%s]*$'
95 + if last_space and last_space > 1 then
96 + local ws = chunk:match '%s+[^%s]*$'
97 + cutoff = #chunk - #ws
98 + chunk = chunk:sub(1, cutoff):gsub('%s+$', '')
99 + end
100 + table.insert(result, chunk)
101 + remaining = remaining:sub(cutoff + 1):gsub('^%s+', '')
102 + end
103 + if remaining ~= '' then
104 + table.insert(result, remaining)
105 + end
106 + end
107 + end
108 + return result
109 + end,
111 + PROMPT_MAX_RATIO = 0.85,

neovim/lua/bugabinga/cmdline/render.lua

Modified
6 6 local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline'
7 7 local hl_ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.hl'
8 +local prompt_ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.prompt'
9 10 local state = {
10 11 buffer = -1,
99 100 vim.api.nvim_buf_set_lines(buf, 0, -1, false, { display_text })
100 101 apply_icon(buf, fmt)
101 102 highlight.apply(buf, hl_ns, text, fmt.lang, 0, 2)
104 + if fmt.type == 'input' and prompt and prompt ~= '' then
105 + vim.api.nvim_buf_clear_namespace(buf, prompt_ns, 0, -1)
106 + local max_prompt_width = math.floor(vim.o.columns * format.PROMPT_MAX_RATIO)
107 + local prompt_is_single_line = not prompt:match '\n'
108 + local prompt_fits = vim.fn.strdisplaywidth(prompt) <= max_prompt_width
109 + if prompt_is_single_line and prompt_fits then
110 + local title_text = fmt.icon .. ' ' .. prompt
111 + vim.api.nvim_win_set_config(win, {
112 + border = vim.g.border_style,
113 + title = { { title_text, 'CmdlineBorderInput' } },
114 + title_pos = 'left',
115 + })
116 + else
117 + local prompt_chunks = format.prompt_lines(prompt, max_prompt_width)
118 + local virt_line_tuples = {}
119 + for _, chunk in ipairs(prompt_chunks) do
120 + table.insert(virt_line_tuples, { { chunk, 'CmdlineBorderInput' } })
121 + end
122 + vim.api.nvim_buf_set_extmark(buf, prompt_ns, 0, 0, {
123 + virt_lines = virt_line_tuples,
124 + virt_lines_above = true,
125 + })
126 + vim.api.nvim_win_set_config(win, {
127 + border = vim.g.border_style,
128 + title = { { fmt.icon, 'CmdlineBorderInput' } },
129 + title_pos = 'left',
130 + height = #prompt_chunks + 1,
131 + })
132 + end
133 + end
102 135 local cursor_byte = cmdline_state:cursor_byte()
103 136 local cursor_col = 2 + cursor_byte
104 137 local anchor = position.get_cmdline_anchor()
113 146 function M.hide()
114 147 if vim.api.nvim_win_is_valid(state.window) then
148 + vim.api.nvim_buf_clear_namespace(state.buffer, prompt_ns, 0, -1)
115 149 vim.api.nvim_win_hide(state.window)
117 151 state.window = -1

neovim/lua/bugabinga/cmdline/state.lua

Modified
1 +local table = require 'std.table'
1 3 local M = {}
3 5 function M.new(content, pos, firstc, prompt, indent, level)

neovim/tests/bugabinga/cmdline/format_spec.lua

Modified
115 115 local result = format.detect(':', nil, '')
116 116 assert.equals('cmdline', result.type)
118 +end)
120 +describe('format.prompt_lines', function()
121 + it('returns empty table for nil prompt', function()
122 + local result = format.prompt_lines(nil, 80)
123 + assert.equals(0, #result)
124 + end)
126 + it('returns empty table for empty prompt', function()
127 + local result = format.prompt_lines('', 80)
128 + assert.equals(0, #result)
129 + end)
131 + it('returns single entry for short prompt', function()
132 + local result = format.prompt_lines('Enter name: ', 80)
133 + assert.equals(1, #result)
134 + assert.equals('Enter name: ', result[1])
135 + end)
137 + it('returns single entry when prompt fits exactly at max_width', function()
138 + local prompt = 'abc'
139 + local max_w = 3
140 + local result = format.prompt_lines(prompt, max_w)
141 + assert.equals(1, #result)
142 + assert.equals('abc', result[1])
143 + end)
145 + it('splits on newline', function()
146 + local result = format.prompt_lines('line 1\nline 2', 80)
147 + assert.equals(2, #result)
148 + assert.equals('line 1', result[1])
149 + assert.equals('line 2', result[2])
150 + end)
152 + it('double newline produces blank line', function()
153 + local result = format.prompt_lines('a\n\nb', 80)
154 + assert.equals(3, #result)
155 + assert.equals('a', result[1])
156 + assert.equals('', result[2])
157 + assert.equals('b', result[3])
158 + end)
160 + it('trailing newline is stripped', function()
161 + local result = format.prompt_lines('hello\n', 80)
162 + assert.equals(1, #result)
163 + assert.equals('hello', result[1])
164 + end)
166 + it('wraps long line at word boundary', function()
167 + local long = 'the quick brown fox jumps over the lazy dog'
168 + local max_w = 20
169 + local result = format.prompt_lines(long, max_w)
170 + for _, line in ipairs(result) do
171 + assert.is_true(vim.fn.strdisplaywidth(line) <= max_w, 'line exceeds max_width: ' .. line)
172 + end
173 + local concatenated = table.concat(result, ' ')
174 + assert.equals(long, concatenated)
175 + end)
177 + it('hard-breaks single word exceeding max_width', function()
178 + local long = 'supercalifragilisticexpialidocious'
179 + local max_w = 10
180 + local result = format.prompt_lines(long, max_w)
181 + assert.is_true(#result > 1, 'single long word should be broken into multiple pieces')
182 + for _, line in ipairs(result) do
183 + assert.is_true(vim.fn.strdisplaywidth(line) <= max_w, 'line exceeds max_width: ' .. line)
184 + end
185 + end)
187 + it('no wrapping when prompt shorter than max_width', function()
188 + local result = format.prompt_lines('short', 80)
189 + assert.equals(1, #result)
190 + assert.equals('short', result[1])
191 + end)
192 +end)
194 +describe('format.PROMPT_MAX_RATIO', function()
195 + it('is 0.85', function()
196 + assert.equals(0.85, format.PROMPT_MAX_RATIO)
197 + end)

neovim/tests/bugabinga/cmdline/render_spec.lua

Modified
226 226 assert.is_true(corner1 ~= corner2, 'corner should change: ' .. corner1 .. ' vs ' .. corner2)
227 227 render.hide()
229 +end)
231 +describe('render.show with input prompt', function()
232 + before_each(function()
233 + package.loaded['bugabinga.cmdline.state'] = nil
234 + package.loaded['bugabinga.cmdline.format'] = nil
235 + package.loaded['bugabinga.cmdline.position'] = nil
236 + package.loaded['bugabinga.cmdline.cursor'] = nil
237 + package.loaded['bugabinga.cmdline.highlight'] = nil
238 + package.loaded['bugabinga.cmdline.render'] = nil
239 + end)
241 + after_each(function()
242 + local ok, render = pcall(require, 'bugabinga.cmdline.render')
243 + if ok then
244 + render.hide()
245 + end
246 + local ok2, pos = pcall(require, 'bugabinga.cmdline.position')
247 + if ok2 then
248 + pos.set_cmdline_anchor(nil)
249 + end
250 + end)
252 + it('input with short prompt does not put prompt in buffer text', function()
253 + local state = require 'bugabinga.cmdline.state'
254 + local render = require 'bugabinga.cmdline.render'
255 + local cmdline_state = state.new({
256 + { {}, 'my input' },
257 + }, 8, ':', 'Name: ', 0, 1)
258 + render.show(cmdline_state)
259 + local pos = require('bugabinga.cmdline.position')
260 + local anchor = pos.get_cmdline_anchor()
261 + local buf = anchor.buf
262 + local lines = vim.api.nvim_buf_get_lines(buf, 0, 1, false)
263 + assert.is_true(lines[1]:find('Name:') == nil, 'prompt should not appear in buffer: ' .. lines[1])
264 + assert.is_true(lines[1]:find('my input') ~= nil, 'user input should appear in buffer')
265 + render.hide()
266 + end)
268 + it('input with empty prompt uses normal rendering', function()
269 + local state = require 'bugabinga.cmdline.state'
270 + local render = require 'bugabinga.cmdline.render'
271 + local cmdline_state = state.new({
272 + { {}, ':hello' },
273 + }, 6, ':', '', 0, 1)
274 + render.show(cmdline_state)
275 + local pos = require('bugabinga.cmdline.position')
276 + local anchor = pos.get_cmdline_anchor()
277 + local buf = anchor.buf
278 + local lines = vim.api.nvim_buf_get_lines(buf, 0, 1, false)
279 + assert.is_true(lines[1]:find(':hello') ~= nil)
280 + render.hide()
281 + end)
283 + it('input format uses input icon (prompt_prefix)', function()
284 + local state = require 'bugabinga.cmdline.state'
285 + local render = require 'bugabinga.cmdline.render'
286 + local cmdline_state = state.new({
287 + { {}, '' },
288 + }, 0, ':', 'Prompt: ', 0, 1)
289 + render.show(cmdline_state)
290 + local fmt_type = render.get_format_type()
291 + assert.equals('input', fmt_type)
292 + render.hide()
293 + end)
295 + it('cursor column is offset by 2 for input', function()
296 + local state = require 'bugabinga.cmdline.state'
297 + local render = require 'bugabinga.cmdline.render'
298 + local cmdline_state = state.new({
299 + { {}, 'test' },
300 + }, 2, ':', 'Prompt: ', 0, 1)
301 + render.show(cmdline_state)
302 + local pos = require('bugabinga.cmdline.position')
303 + local anchor = pos.get_cmdline_anchor()
304 + local cursor = vim.api.nvim_win_get_cursor(anchor.win)
305 + assert.equals(2 + 2, cursor[2], 'cursor should be 2 + cursor_byte')
306 + render.hide()
307 + end)
308 +end)
310 +describe('render.show with input prompt - multi-line (virt_lines)', function()
311 + before_each(function()
312 + package.loaded['bugabinga.cmdline.state'] = nil
313 + package.loaded['bugabinga.cmdline.format'] = nil
314 + package.loaded['bugabinga.cmdline.position'] = nil
315 + package.loaded['bugabinga.cmdline.cursor'] = nil
316 + package.loaded['bugabinga.cmdline.highlight'] = nil
317 + package.loaded['bugabinga.cmdline.render'] = nil
318 + end)
320 + after_each(function()
321 + local ok, render = pcall(require, 'bugabinga.cmdline.render')
322 + if ok then
323 + render.hide()
324 + end
325 + local ok2, pos = pcall(require, 'bugabinga.cmdline.position')
326 + if ok2 then
327 + pos.set_cmdline_anchor(nil)
328 + end
329 + end)
331 + it('multi-line prompt sets virt_lines in buffer', function()
332 + local state = require 'bugabinga.cmdline.state'
333 + local render = require 'bugabinga.cmdline.render'
334 + local cmdline_state = state.new({
335 + { {}, 'input' },
336 + }, 5, ':', 'Line 1\nLine 2\nLine 3', 0, 1)
337 + render.show(cmdline_state)
338 + local pos = require('bugabinga.cmdline.position')
339 + local anchor = pos.get_cmdline_anchor()
340 + local buf = anchor.buf
341 + local prompt_ns = vim.api.nvim_create_namespace('bugabinga.cmdline.prompt')
342 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, prompt_ns, 0, -1, { details = true })
343 + assert.is_true(#extmarks > 0, 'should have virt_lines extmark for multi-line prompt')
344 + local mark = extmarks[1]
345 + local opts = mark[4]
346 + assert.is_not_nil(opts.virt_lines, 'extmark should have virt_lines')
347 + assert.is_true(opts.virt_lines_above, 'virt_lines should be above')
348 + render.hide()
349 + end)
351 + it('multi-line prompt window height is prompt_chunks + 1', function()
352 + local state = require 'bugabinga.cmdline.state'
353 + local render = require 'bugabinga.cmdline.render'
354 + local cmdline_state = state.new({
355 + { {}, 'x' },
356 + }, 1, ':', 'Line 1\nLine 2', 0, 1)
357 + render.show(cmdline_state)
358 + local pos = require('bugabinga.cmdline.position')
359 + local anchor = pos.get_cmdline_anchor()
360 + local height = vim.api.nvim_win_get_config(anchor.win).height
361 + assert.equals(2 + 1, height, 'height should be 2 (prompt lines) + 1 (input line)')
362 + render.hide()
363 + end)
365 + it('multi-line prompt splits on newlines', function()
366 + local state = require 'bugabinga.cmdline.state'
367 + local render = require 'bugabinga.cmdline.render'
368 + local cmdline_state = state.new({
369 + { {}, 'x' },
370 + }, 1, ':', 'first line\nsecond line', 0, 1)
371 + render.show(cmdline_state)
372 + local pos = require('bugabinga.cmdline.position')
373 + local anchor = pos.get_cmdline_anchor()
374 + local buf = anchor.buf
375 + local prompt_ns = vim.api.nvim_create_namespace('bugabinga.cmdline.prompt')
376 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, prompt_ns, 0, -1, { details = true })
377 + local opts = extmarks[1][4]
378 + assert.equals(2, #opts.virt_lines, 'should have 2 virt_lines for 2 prompt lines')
379 + render.hide()
380 + end)
382 + it('double newline creates blank virt_line', function()
383 + local state = require 'bugabinga.cmdline.state'
384 + local render = require 'bugabinga.cmdline.render'
385 + local cmdline_state = state.new({
386 + { {}, 'x' },
387 + }, 1, ':', 'a\n\nb', 0, 1)
388 + render.show(cmdline_state)
389 + local pos = require('bugabinga.cmdline.position')
390 + local anchor = pos.get_cmdline_anchor()
391 + local buf = anchor.buf
392 + local prompt_ns = vim.api.nvim_create_namespace('bugabinga.cmdline.prompt')
393 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, prompt_ns, 0, -1, { details = true })
394 + local opts = extmarks[1][4]
395 + assert.equals(3, #opts.virt_lines, 'should have 3 virt_lines: a, blank, b')
396 + render.hide()
397 + end)
398 +end)
400 +describe('render.hide clears prompt state', function()
401 + before_each(function()
402 + package.loaded['bugabinga.cmdline.state'] = nil
403 + package.loaded['bugabinga.cmdline.format'] = nil
404 + package.loaded['bugabinga.cmdline.position'] = nil
405 + package.loaded['bugabinga.cmdline.cursor'] = nil
406 + package.loaded['bugabinga.cmdline.highlight'] = nil
407 + package.loaded['bugabinga.cmdline.render'] = nil
408 + end)
410 + after_each(function()
411 + local ok, render = pcall(require, 'bugabinga.cmdline.render')
412 + if ok then
413 + render.hide()
414 + end
415 + local ok2, pos = pcall(require, 'bugabinga.cmdline.position')
416 + if ok2 then
417 + pos.set_cmdline_anchor(nil)
418 + end
419 + end)
421 + it('hide after input clears prompt namespace', function()
422 + local state = require 'bugabinga.cmdline.state'
423 + local render = require 'bugabinga.cmdline.render'
424 + local cmdline_state = state.new({
425 + { {}, 'x' },
426 + }, 1, ':', 'Line 1\nLine 2', 0, 1)
427 + render.show(cmdline_state)
428 + local pos = require('bugabinga.cmdline.position')
429 + local anchor = pos.get_cmdline_anchor()
430 + local buf = anchor.buf
431 + render.hide()
432 + local prompt_ns = vim.api.nvim_create_namespace('bugabinga.cmdline.prompt')
433 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, prompt_ns, 0, -1, {})
434 + assert.equals(0, #extmarks, 'prompt namespace should be empty after hide')
435 + end)