1 +describe(
'highlight.apply',
function()
4 + before_each(
function()
5 + package.
loaded[
'bugabinga.cmdline.highlight']
= nil 6 + highlight
= require(
'bugabinga.cmdline.highlight')
9 + after_each(
function()
10 + for _, buf
in ipairs(vim.api.nvim_list_bufs())
do 11 + vim.api.nvim_buf_delete(buf, {
force =
true })
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, {
' ' })
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' 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)
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' 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)
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' 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)
55 + assert.is_true(
#extmarks
>= 0)
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' 63 + local col_offset
= 2 64 + local ok
= pcall(highlight.apply, buf, ns, text,
nil, row, col_offset)
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' 73 + local col_offset
= 2 74 + local ok
= pcall(highlight.apply, buf, ns, text,
'nonexistent_lang_xyz', row, col_offset)
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")' 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)
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' 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)
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' 112 + local col_offset
= 2 113 + local ok
= pcall(highlight.apply, buf, ns, text,
nil, row, col_offset)