fix(treesitter): do not modify highlight state for _on_spell_nav

Problem:  Treesitter highlighter clears the already populated highlight
          state when performing spell checking while drawing a
          smoothscrolled topline.
Solution: Save and restore the highlight state in the highlighter's
          _on_spell_nav callback.
This commit is contained in:
Luuk van Baal
2024-06-23 13:50:21 +02:00
committed by Lewis Russell
parent be999e6a0e
commit da4e8dc5b0
2 changed files with 41 additions and 28 deletions

View File

@ -377,11 +377,15 @@ function TSHighlighter._on_spell_nav(_, _, buf, srow, _, erow, _)
return return
end end
-- Do not affect potentially populated highlight state. Here we just want a temporary
-- empty state so the C code can detect whether the region should be spell checked.
local highlight_states = self._highlight_states
self:prepare_highlight_states(srow, erow) self:prepare_highlight_states(srow, erow)
for row = srow, erow do for row = srow, erow do
on_line_impl(self, buf, row, true) on_line_impl(self, buf, row, true)
end end
self._highlight_states = highlight_states
end end
---@private ---@private

View File

@ -1008,39 +1008,48 @@ describe('treesitter highlighting (markdown)', function()
before_each(function() before_each(function()
screen = Screen.new(40, 6) screen = Screen.new(40, 6)
screen:attach() screen:attach()
screen:set_default_attr_ids { exec_lua([[
[1] = { foreground = Screen.colors.Blue1 }, vim.bo.filetype = 'markdown'
[2] = { bold = true, foreground = Screen.colors.Blue1 }, vim.treesitter.start()
[3] = { bold = true, foreground = Screen.colors.Brown }, ]])
[4] = { foreground = Screen.colors.Cyan4 },
[5] = { foreground = Screen.colors.Magenta1 },
}
end) end)
it('supports hyperlinks', function() it('supports hyperlinks', function()
local url = 'https://example.com' local url = 'https://example.com'
insert(string.format('[This link text](%s) is a hyperlink.', url)) insert(string.format('[This link text](%s) is a hyperlink.', url))
exec_lua([[ screen:add_extra_attr_ids({
vim.bo.filetype = 'markdown' [100] = { foreground = Screen.colors.DarkCyan, url = 'https://example.com' },
vim.treesitter.start() })
]]) screen:expect({
screen:expect {
grid = [[ grid = [[
{4:[}{6:This link text}{4:](}{7:https://example.com}{4:)} is| {25:[}{100:This link text}{25:](}{28:https://example.com}{25:)} is|
a hyperlink^. | a hyperlink^. |
{2:~ }|*3 {1:~ }|*3
| |
]], ]],
attr_ids = { })
[1] = { foreground = Screen.colors.Blue1 }, end)
[2] = { bold = true, foreground = Screen.colors.Blue1 },
[3] = { bold = true, foreground = Screen.colors.Brown }, it('works with spellchecked and smoothscrolled topline', function()
[4] = { foreground = Screen.colors.Cyan4 }, insert([[
[5] = { foreground = Screen.colors.Magenta }, - $f(0)=\sum_{k=1}^{\infty}\frac{2}{\pi^{2}k^{2}}+\lim_{w \to 0}x$.
[6] = { foreground = Screen.colors.Cyan4, url = url },
[7] = { underline = true, foreground = Screen.colors.SlateBlue }, ```c
}, printf('Hello World!');
} ```
]])
command('set spell smoothscroll')
feed('gg<C-E>')
screen:add_extra_attr_ids({ [100] = { undercurl = true, special = Screen.colors.Red } })
screen:expect({
grid = [[
{1:<<<}k^{2}}+\{100:lim}_{w \to 0}x$^. |
|
{18:```}{15:c} |
{25:printf}{16:(}{26:'Hello World!'}{16:);} |
{18:```} |
|
]],
})
end) end)
end) end)