repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(cmdline): add treesitter syntax highlighting with nu shell support

a2e5448ca2ae42336473b7755d75ae1a49d5bce9

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (6)

neovim/lua/bugabinga/cmdline/PLAN.md

Modified
34 34 ### 5. Syntax highlighting in cmdline
36 -- [ ] Add `lang` field to format table entries (`vim`, `lua`, `bash`, `regex`)
37 -- [ ] After writing buffer text, apply treesitter highlights to command region
38 -- [ ] Fallback to vim syntax if no treesitter parser
36 +- [x] Add `lang` field to format table entries (`vim`, `lua`, `bash`, `regex`)
37 +- [x] After writing buffer text, apply treesitter highlights to command region
38 +- [x] Fallback to vim syntax if no treesitter parser
39 39 - Files: `render.lua`, new `highlight.lua` or inline
41 41 ### 6. Popupmenu match highlighting

neovim/lua/bugabinga/cmdline/format.lua

Modified
5 5 return {
6 6 detect = function(firstc, content, prompt)
7 7 if prompt and prompt ~= '' then
8 - return { type = 'input', icon = icon.prompt_prefix }
8 + return { type = 'input', icon = icon.prompt_prefix, lang = nil }
10 10 if firstc == '/' then
11 - return { type = 'search_down', icon = icon.search }
11 + return { type = 'search_down', icon = icon.search, lang = 'regex' }
13 13 if firstc == '?' then
14 - return { type = 'search_up', icon = icon.search_up }
14 + return { type = 'search_up', icon = icon.search_up, lang = 'regex' }
16 16 if firstc == '!' or (firstc == ':' and content and content:match '^!') then
17 - return { type = 'shell', icon = icon.shell }
17 + return { type = 'shell', icon = icon.shell, lang = 'bash' }
19 19 if firstc == ':' and content then
20 20 for _, pat in ipairs(HELP_PATTERNS) do
21 21 if content:match(pat) then
22 - return { type = 'help', icon = icon.help }
22 + return { type = 'help', icon = icon.help, lang = nil }
25 25 if content:match '^lua%s*' or content:match '^=' then
26 - return { type = 'lua', icon = icon.vim }
26 + return { type = 'lua', icon = icon.vim, lang = 'lua' }
28 - return { type = 'cmdline', icon = icon.command }
28 + return { type = 'cmdline', icon = icon.command, lang = 'vim' }
30 - return { type = 'cmdline', icon = icon.command }
30 + return { type = 'cmdline', icon = icon.command, lang = 'vim' }
31 31 end,

neovim/lua/bugabinga/cmdline/highlight.lua

Added
new file mode 100644
1 +local M = {}
3 +local function resolve_lang(lang)
4 + if lang ~= 'bash' then
5 + return lang
6 + end
7 + local shell = vim.o.shell
8 + if shell and (shell:match '^nu$' or shell:match '/nu$') then
9 + return 'nu'
10 + end
11 + return 'bash'
12 +end
14 +function M.apply(buf, ns, text, lang, row, col_offset)
15 + lang = resolve_lang(lang)
16 + if not lang or lang == '' then
17 + return
18 + end
19 + if vim.api.nvim_buf_line_count(buf) < 1 then
20 + return
21 + end
22 + vim.api.nvim_buf_clear_namespace(buf, ns, row, row + 1)
23 + local parser_ok, parser = pcall(vim.treesitter.get_string_parser, text, lang)
24 + if not parser_ok or not parser then
25 + return
26 + end
27 + local parse_ok, trees = pcall(function()
28 + return parser:parse()
29 + end)
30 + if not parse_ok or not trees or #trees == 0 then
31 + return
32 + end
33 + local tree = trees[1]
34 + local query_ok, query = pcall(vim.treesitter.query.get, lang, 'highlights')
35 + if not query_ok or not query then
36 + return
37 + end
38 + for id, node in query:iter_captures(tree:root(), text) do
39 + local start_row, start_col, end_row, end_col = node:range()
40 + if start_row == end_row and start_col < end_col then
41 + local hl_group = '@' .. query.captures[id]
42 + vim.api.nvim_buf_set_extmark(buf, ns, row, col_offset + start_col, {
43 + end_col = col_offset + end_col,
44 + hl_group = hl_group,
45 + priority = 100,
46 + })
47 + end
48 + end
49 +end
51 +return M

neovim/lua/bugabinga/cmdline/render.lua

Modified
1 1 local format = require 'bugabinga.cmdline.format'
2 2 local position = require 'bugabinga.cmdline.position'
3 3 local cursor = require 'bugabinga.cmdline.cursor'
4 +local highlight = require 'bugabinga.cmdline.highlight'
5 6 local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline'
7 +local hl_ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.hl'
7 9 local state = {
8 10 buffer = -1,
105 107 virt_text = { { fmt.icon, icon_hl } },
106 108 virt_text_pos = 'overlay',
110 + highlight.apply(buf, hl_ns, text, fmt.lang, 0, 2)
108 111 local cursor_byte = cmdline_state:cursor_byte()
109 112 local cursor_col = 2 + cursor_byte
110 113 local anchor = position.get_cmdline_anchor()

neovim/lua/bugabinga/treesitter.lua

Modified
21 21 local treesitter = puk 'nvim-treesitter'
22 22 treesitter.setup {}
23 +treesitter.install { 'bash', 'nu' }
24 25 auto 'look_for_treesitter_parser' {
25 26 description = 'Try loading a treesitter parser, if available.',

neovim/tests/bugabinga/cmdline/highlight_spec.lua

Added
new file mode 100644
1 +describe('highlight.apply', function()
2 + local highlight
4 + before_each(function()
5 + package.loaded['bugabinga.cmdline.highlight'] = nil
6 + highlight = require('bugabinga.cmdline.highlight')
7 + end)
9 + after_each(function()
10 + for _, buf in ipairs(vim.api.nvim_list_bufs()) do
11 + vim.api.nvim_buf_delete(buf, { force = true })
12 + end
13 + end)
15 + local function make_buf_with_line()
16 + local buf = vim.api.nvim_create_buf(false, true)
17 + vim.api.nvim_buf_set_lines(buf, 0, -1, false, { ' ' })
18 + return buf
19 + end
21 + describe('apply', function()
22 + it('applies treesitter highlights for lua lang', function()
23 + local buf = make_buf_with_line()
24 + local ns = vim.api.nvim_create_namespace 'test'
25 + local text = 'local x = 10'
26 + local row = 0
27 + local col_offset = 2
28 + highlight.apply(buf, ns, text, 'lua', row, col_offset)
29 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
30 + assert.is_true(#extmarks > 0)
31 + end)
33 + it('applies treesitter highlights for vim lang', function()
34 + local buf = make_buf_with_line()
35 + local ns = vim.api.nvim_create_namespace 'test'
36 + local text = ':function Foo() end'
37 + local row = 0
38 + local col_offset = 2
39 + highlight.apply(buf, ns, text, 'vim', row, col_offset)
40 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
41 + assert.is_true(#extmarks > 0)
42 + end)
44 + it('applies treesitter highlights for bash lang if parser available', function()
45 + local buf = make_buf_with_line()
46 + local ns = vim.api.nvim_create_namespace 'test'
47 + local text = 'ls -la'
48 + local row = 0
49 + local col_offset = 2
50 + highlight.apply(buf, ns, text, 'bash', row, col_offset)
51 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
52 + if #extmarks == 0 then
53 + vim.notify('bash parser not available, skipping', vim.log.levels.WARN)
54 + end
55 + assert.is_true(#extmarks >= 0)
56 + end)
58 + it('does not crash when lang is nil', function()
59 + local buf = make_buf_with_line()
60 + local ns = vim.api.nvim_create_namespace 'test'
61 + local text = 'hello world'
62 + local row = 0
63 + local col_offset = 2
64 + local ok = pcall(highlight.apply, buf, ns, text, nil, row, col_offset)
65 + assert.is_true(ok)
66 + end)
68 + it('does not crash when lang has no parser', function()
69 + local buf = make_buf_with_line()
70 + local ns = vim.api.nvim_create_namespace 'test'
71 + local text = 'hello world'
72 + local row = 0
73 + local col_offset = 2
74 + local ok = pcall(highlight.apply, buf, ns, text, 'nonexistent_lang_xyz', row, col_offset)
75 + assert.is_true(ok)
76 + end)
78 + it('applies highlights with correct col_offset applied', function()
79 + local buf = make_buf_with_line()
80 + local ns = vim.api.nvim_create_namespace 'test'
81 + local text = 'print("hi")'
82 + local row = 0
83 + local col_offset = 5
84 + highlight.apply(buf, ns, text, 'lua', row, col_offset)
85 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
86 + assert.is_true(#extmarks > 0)
87 + for _, extmark in ipairs(extmarks) do
88 + local _, mark_col = extmark[2], extmark[3]
89 + assert.is_true(mark_col >= col_offset)
90 + end
91 + end)
93 + it('clears namespace before applying new highlights', function()
94 + local buf = make_buf_with_line()
95 + local ns = vim.api.nvim_create_namespace 'test'
96 + local text = 'local x = 10'
97 + local row = 0
98 + local col_offset = 2
99 + highlight.apply(buf, ns, text, 'lua', row, col_offset)
100 + local extmarks_first = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
101 + local first_count = #extmarks_first
102 + highlight.apply(buf, ns, text, 'lua', row, col_offset)
103 + local extmarks_second = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
104 + assert.equals(first_count, #extmarks_second)
105 + end)
107 + it('handles nil lang gracefully', function()
108 + local buf = make_buf_with_line()
109 + local ns = vim.api.nvim_create_namespace 'test'
110 + local text = 'local x = 10'
111 + local row = 0
112 + local col_offset = 2
113 + local ok = pcall(highlight.apply, buf, ns, text, nil, row, col_offset)
114 + assert.is_true(ok)
115 + end)
116 + end)
117 +end)