From da4e8dc5b04a82c6dd483f6c5345a81d8b375bec Mon Sep 17 00:00:00 2001 From: Luuk van Baal Date: Sun, 23 Jun 2024 13:50:21 +0200 Subject: 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. --- test/functional/treesitter/highlight_spec.lua | 63 +++++++++++++++------------ 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index 69984b3233..05c0cdc01e 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -1008,39 +1008,48 @@ describe('treesitter highlighting (markdown)', function() before_each(function() screen = Screen.new(40, 6) screen:attach() - screen:set_default_attr_ids { - [1] = { foreground = Screen.colors.Blue1 }, - [2] = { bold = true, foreground = Screen.colors.Blue1 }, - [3] = { bold = true, foreground = Screen.colors.Brown }, - [4] = { foreground = Screen.colors.Cyan4 }, - [5] = { foreground = Screen.colors.Magenta1 }, - } + exec_lua([[ + vim.bo.filetype = 'markdown' + vim.treesitter.start() + ]]) end) it('supports hyperlinks', function() local url = 'https://example.com' insert(string.format('[This link text](%s) is a hyperlink.', url)) - exec_lua([[ - vim.bo.filetype = 'markdown' - vim.treesitter.start() - ]]) + screen:add_extra_attr_ids({ + [100] = { foreground = Screen.colors.DarkCyan, url = 'https://example.com' }, + }) + screen:expect({ + grid = [[ + {25:[}{100:This link text}{25:](}{28:https://example.com}{25:)} is| + a hyperlink^. | + {1:~ }|*3 + | + ]], + }) + end) - screen:expect { + it('works with spellchecked and smoothscrolled topline', function() + insert([[ +- $f(0)=\sum_{k=1}^{\infty}\frac{2}{\pi^{2}k^{2}}+\lim_{w \to 0}x$. + +```c +printf('Hello World!'); +``` + ]]) + command('set spell smoothscroll') + feed('gg') + screen:add_extra_attr_ids({ [100] = { undercurl = true, special = Screen.colors.Red } }) + screen:expect({ grid = [[ - {4:[}{6:This link text}{4:](}{7:https://example.com}{4:)} is| - a hyperlink^. | - {2:~ }|*3 - | - ]], - attr_ids = { - [1] = { foreground = Screen.colors.Blue1 }, - [2] = { bold = true, foreground = Screen.colors.Blue1 }, - [3] = { bold = true, foreground = Screen.colors.Brown }, - [4] = { foreground = Screen.colors.Cyan4 }, - [5] = { foreground = Screen.colors.Magenta }, - [6] = { foreground = Screen.colors.Cyan4, url = url }, - [7] = { underline = true, foreground = Screen.colors.SlateBlue }, - }, - } + {1:<<<}k^{2}}+\{100:lim}_{w \to 0}x$^. | + | + {18:```}{15:c} | + {25:printf}{16:(}{26:'Hello World!'}{16:);} | + {18:```} | + | + ]], + }) end) end) -- cgit From d413038b4fd71b7a335b6653aa64d2cb6daeac7b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 3 Jul 2024 07:40:42 +0800 Subject: fix(treesitter): ensure syntaxset augroup exists (#29542) Problem: Error when calling vim.treesitter.start() and vim.treesitter.stop() in init.lua. Solution: Ensure syntaxset augroup exists after loading synload.vim. --- test/functional/treesitter/highlight_spec.lua | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index 05c0cdc01e..6eadb3110f 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -10,8 +10,6 @@ local command = n.command local api = n.api local eq = t.eq -before_each(clear) - local hl_query_c = [[ (ERROR) @error @@ -120,6 +118,7 @@ describe('treesitter highlighting (C)', function() local screen before_each(function() + clear() screen = Screen.new(65, 18) screen:attach() screen:set_default_attr_ids { @@ -800,6 +799,7 @@ describe('treesitter highlighting (lua)', function() local screen before_each(function() + clear() screen = Screen.new(65, 18) screen:attach() screen:set_default_attr_ids { @@ -838,6 +838,7 @@ describe('treesitter highlighting (help)', function() local screen before_each(function() + clear() screen = Screen.new(40, 6) screen:attach() screen:set_default_attr_ids { @@ -939,6 +940,7 @@ describe('treesitter highlighting (nested injections)', function() local screen before_each(function() + clear() screen = Screen.new(80, 7) screen:attach() screen:set_default_attr_ids { @@ -1006,6 +1008,7 @@ describe('treesitter highlighting (markdown)', function() local screen before_each(function() + clear() screen = Screen.new(40, 6) screen:attach() exec_lua([[ @@ -1053,3 +1056,19 @@ printf('Hello World!'); }) end) end) + +it('starting and stopping treesitter highlight in init.lua works #29541', function() + t.write_file( + 'Xinit.lua', + [[ + vim.bo.ft = 'c' + vim.treesitter.start() + vim.treesitter.stop() + ]] + ) + finally(function() + os.remove('Xinit.lua') + end) + clear({ args = { '-u', 'Xinit.lua' } }) + eq('', api.nvim_get_vvar('errmsg')) +end) -- cgit From 599fc7cee4441cf3beacd47f388aa36194091422 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 3 Jul 2024 08:40:55 +0800 Subject: test: starting and stopping treesitter highlight (#29546) --- test/functional/treesitter/highlight_spec.lua | 143 +++++++++++++++++--------- 1 file changed, 97 insertions(+), 46 deletions(-) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index 6eadb3110f..3774ff8d54 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -8,6 +8,7 @@ local exec_lua = n.exec_lua local feed = n.feed local command = n.command local api = n.api +local fn = n.fn local eq = t.eq local hl_query_c = [[ @@ -63,6 +64,46 @@ static int nlua_schedule(lua_State *const lstate) return 0; }]] +local hl_grid_legacy_c = [[ + {2:^/// Schedule Lua callback on main loop's event queue} | + {3:static} {3:int} nlua_schedule(lua_State *{3:const} lstate) | + { | + {4:if} (lua_type(lstate, {5:1}) != LUA_TFUNCTION | + || lstate != lstate) { | + lua_pushliteral(lstate, {5:"vim.schedule: expected function"}); | + {4:return} lua_error(lstate); | + } | + | + LuaRef cb = nlua_ref(lstate, {5:1}); | + | + multiqueue_put(main_loop.events, nlua_schedule_event, | + {5:1}, ({3:void} *)({3:ptrdiff_t})cb); | + {4:return} {5:0}; | + } | + {1:~ }|*2 + | +]] + +local hl_grid_ts_c = [[ + {2:^/// Schedule Lua callback on main loop's event queue} | + {3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) | + { | + {4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} | + || {6:lstate} != {6:lstate}) { | + {11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); | + {4:return} {11:lua_error}(lstate); | + } | + | + {7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); | + | + multiqueue_put(main_loop.events, {11:nlua_schedule_event}, | + {5:1}, ({3:void} *)({3:ptrdiff_t})cb); | + {4:return} {5:0}; | + } | + {1:~ }|*2 + | +]] + local test_text_c = [[ void ui_refresh(void) { @@ -140,11 +181,39 @@ describe('treesitter highlighting (C)', function() command [[ hi link @warning WarningMsg ]] end) + it('starting and stopping treesitter highlight works', function() + command('setfiletype c | syntax on') + fn.setreg('r', hl_text_c) + feed('irgg') + -- legacy syntax highlighting is used by default + screen:expect(hl_grid_legacy_c) + + exec_lua([[ + vim.treesitter.query.set('c', 'highlights', hl_query) + vim.treesitter.start() + ]]) + -- treesitter highlighting is used + screen:expect(hl_grid_ts_c) + + exec_lua('vim.treesitter.stop()') + -- legacy syntax highlighting is used + screen:expect(hl_grid_legacy_c) + + exec_lua('vim.treesitter.start()') + -- treesitter highlighting is used + screen:expect(hl_grid_ts_c) + + exec_lua('vim.treesitter.stop()') + -- legacy syntax highlighting is used + screen:expect(hl_grid_legacy_c) + end) + it('is updated with edits', function() insert(hl_text_c) + feed('gg') screen:expect { grid = [[ - /// Schedule Lua callback on main loop's event queue | + ^/// Schedule Lua callback on main loop's event queue | static int nlua_schedule(lua_State *const lstate) | { | if (lua_type(lstate, 1) != LUA_TFUNCTION | @@ -158,7 +227,7 @@ describe('treesitter highlighting (C)', function() multiqueue_put(main_loop.events, nlua_schedule_event, | 1, (void *)(ptrdiff_t)cb); | return 0; | - ^} | + } | {1:~ }|*2 | ]], @@ -169,27 +238,7 @@ describe('treesitter highlighting (C)', function() local highlighter = vim.treesitter.highlighter test_hl = highlighter.new(parser, {queries = {c = hl_query}}) ]] - screen:expect { - grid = [[ - {2:/// Schedule Lua callback on main loop's event queue} | - {3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) | - { | - {4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} | - || {6:lstate} != {6:lstate}) { | - {11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); | - {4:return} {11:lua_error}(lstate); | - } | - | - {7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); | - | - multiqueue_put(main_loop.events, {11:nlua_schedule_event}, | - {5:1}, ({3:void} *)({3:ptrdiff_t})cb); | - {4:return} {5:0}; | - ^} | - {1:~ }|*2 - | - ]], - } + screen:expect(hl_grid_ts_c) feed('5Gocdd') @@ -525,40 +574,21 @@ describe('treesitter highlighting (C)', function() it('supports highlighting with custom highlight groups', function() insert(hl_text_c) + feed('gg') exec_lua [[ local parser = vim.treesitter.get_parser(0, "c") test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}}) ]] - screen:expect { - grid = [[ - {2:/// Schedule Lua callback on main loop's event queue} | - {3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) | - { | - {4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} | - || {6:lstate} != {6:lstate}) { | - {11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); | - {4:return} {11:lua_error}(lstate); | - } | - | - {7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); | - | - multiqueue_put(main_loop.events, {11:nlua_schedule_event}, | - {5:1}, ({3:void} *)({3:ptrdiff_t})cb); | - {4:return} {5:0}; | - ^} | - {1:~ }|*2 - | - ]], - } + screen:expect(hl_grid_ts_c) -- This will change ONLY the literal strings to look like comments -- The only literal string is the "vim.schedule: expected function" in this test. exec_lua [[vim.cmd("highlight link @string.nonexistent_specializer comment")]] screen:expect { grid = [[ - {2:/// Schedule Lua callback on main loop's event queue} | + {2:^/// Schedule Lua callback on main loop's event queue} | {3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) | { | {4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} | @@ -572,7 +602,7 @@ describe('treesitter highlighting (C)', function() multiqueue_put(main_loop.events, {11:nlua_schedule_event}, | {5:1}, ({3:void} *)({3:ptrdiff_t})cb); | {4:return} {5:0}; | - ^} | + } | {1:~ }|*2 | ]], @@ -1071,4 +1101,25 @@ it('starting and stopping treesitter highlight in init.lua works #29541', functi end) clear({ args = { '-u', 'Xinit.lua' } }) eq('', api.nvim_get_vvar('errmsg')) + + local screen = Screen.new(65, 18) + screen:attach() + screen:set_default_attr_ids { + [1] = { bold = true, foreground = Screen.colors.Blue1 }, + [2] = { foreground = Screen.colors.Blue1 }, + [3] = { bold = true, foreground = Screen.colors.SeaGreen4 }, + [4] = { bold = true, foreground = Screen.colors.Brown }, + [5] = { foreground = Screen.colors.Magenta }, + [6] = { foreground = Screen.colors.Red }, + [7] = { bold = true, foreground = Screen.colors.SlateBlue }, + [8] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red }, + [9] = { foreground = Screen.colors.Magenta, background = Screen.colors.Red }, + [10] = { foreground = Screen.colors.Red, background = Screen.colors.Red }, + [11] = { foreground = Screen.colors.Cyan4 }, + } + + fn.setreg('r', hl_text_c) + feed('irgg') + -- legacy syntax highlighting is used + screen:expect(hl_grid_legacy_c) end) -- cgit From 862338255da6dc08ae4f4f78db0034c81e3cdf38 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Wed, 24 Jul 2024 15:06:01 +0200 Subject: fix(runtime): sync bundled treesitter queries --- test/functional/treesitter/highlight_spec.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index 3774ff8d54..b0ac180738 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -1052,10 +1052,15 @@ describe('treesitter highlighting (markdown)', function() insert(string.format('[This link text](%s) is a hyperlink.', url)) screen:add_extra_attr_ids({ [100] = { foreground = Screen.colors.DarkCyan, url = 'https://example.com' }, + [101] = { + foreground = Screen.colors.SlateBlue, + url = 'https://example.com', + underline = true, + }, }) screen:expect({ grid = [[ - {25:[}{100:This link text}{25:](}{28:https://example.com}{25:)} is| + {25:[}{100:This link text}{25:](}{101:https://example.com}{25:)} is| a hyperlink^. | {1:~ }|*3 | -- cgit From 7d24c4d6b0413cd5af8d0579f0a9a694db7f775e Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 29 Jul 2024 11:20:15 +0100 Subject: test: allow exec_lua to handle functions Problem: Tests have lots of exec_lua calls which input blocks of code provided as unformatted strings. Solution: Teach exec_lua how to handle functions. --- test/functional/treesitter/highlight_spec.lua | 175 ++++++++++++++------------ 1 file changed, 96 insertions(+), 79 deletions(-) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index b0ac180738..da001e2ab1 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -156,7 +156,7 @@ local injection_grid_expected_c = [[ ]] describe('treesitter highlighting (C)', function() - local screen + local screen --- @type test.functional.ui.screen before_each(function() clear() @@ -176,7 +176,6 @@ describe('treesitter highlighting (C)', function() [11] = { foreground = Screen.colors.Cyan4 }, } - exec_lua([[ hl_query = ... ]], hl_query_c) command [[ hi link @error ErrorMsg ]] command [[ hi link @warning WarningMsg ]] end) @@ -188,22 +187,28 @@ describe('treesitter highlighting (C)', function() -- legacy syntax highlighting is used by default screen:expect(hl_grid_legacy_c) - exec_lua([[ + exec_lua(function(hl_query) vim.treesitter.query.set('c', 'highlights', hl_query) vim.treesitter.start() - ]]) + end, hl_query_c) -- treesitter highlighting is used screen:expect(hl_grid_ts_c) - exec_lua('vim.treesitter.stop()') + exec_lua(function() + vim.treesitter.stop() + end) -- legacy syntax highlighting is used screen:expect(hl_grid_legacy_c) - exec_lua('vim.treesitter.start()') + exec_lua(function() + vim.treesitter.start() + end) -- treesitter highlighting is used screen:expect(hl_grid_ts_c) - exec_lua('vim.treesitter.stop()') + exec_lua(function() + vim.treesitter.stop() + end) -- legacy syntax highlighting is used screen:expect(hl_grid_legacy_c) end) @@ -233,11 +238,11 @@ describe('treesitter highlighting (C)', function() ]], } - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = hl_query}}) - ]] + highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect(hl_grid_ts_c) feed('5Gocdd') @@ -364,10 +369,10 @@ describe('treesitter highlighting (C)', function() it('is updated with :sort', function() insert(test_text_c) - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}}) - ]] + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect { grid = [[ {3:int} width = {5:INT_MAX}, height = {5:INT_MAX}; | @@ -470,19 +475,19 @@ describe('treesitter highlighting (C)', function() ]], } - exec_lua [[ - parser = vim.treesitter.get_parser(0, "c") - query = vim.treesitter.query.parse("c", "(declaration) @decl") + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') + local query = vim.treesitter.query.parse('c', '(declaration) @decl') local nodes = {} for _, node in query:iter_captures(parser:parse()[1]:root(), 0, 0, 19) do table.insert(nodes, node) end - parser:set_included_regions({nodes}) + parser:set_included_regions({ nodes }) - local hl = vim.treesitter.highlighter.new(parser, {queries = {c = "(identifier) @type"}}) - ]] + vim.treesitter.highlighter.new(parser, { queries = { c = '(identifier) @type' } }) + end) screen:expect { grid = [[ @@ -513,13 +518,15 @@ describe('treesitter highlighting (C)', function() screen:expect { grid = injection_grid_c } - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c", { - injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'} + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c', { + injections = { + c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))', + }, }) local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = hl_query}}) - ]] + highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect { grid = injection_grid_expected_c } end) @@ -529,14 +536,16 @@ describe('treesitter highlighting (C)', function() screen:expect { grid = injection_grid_c } - exec_lua [[ - vim.treesitter.language.register("c", "foo") - local parser = vim.treesitter.get_parser(0, "c", { - injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "foo")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "foo"))'} + exec_lua(function(hl_query) + vim.treesitter.language.register('c', 'foo') + local parser = vim.treesitter.get_parser(0, 'c', { + injections = { + c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "foo")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "foo"))', + }, }) local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = hl_query}}) - ]] + highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect { grid = injection_grid_expected_c } end) @@ -550,13 +559,14 @@ describe('treesitter highlighting (C)', function() } ]]) - exec_lua [[ - local injection_query = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' - vim.treesitter.query.set("c", "highlights", hl_query) - vim.treesitter.query.set("c", "injections", injection_query) + exec_lua(function(hl_query) + local injection_query = + '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' + vim.treesitter.query.set('c', 'highlights', hl_query) + vim.treesitter.query.set('c', 'injections', injection_query) - vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, "c")) - ]] + vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) + end, hl_query_c) screen:expect { grid = [[ @@ -576,10 +586,10 @@ describe('treesitter highlighting (C)', function() insert(hl_text_c) feed('gg') - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}}) - ]] + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect(hl_grid_ts_c) @@ -619,10 +629,14 @@ describe('treesitter highlighting (C)', function() } ]]) - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query..'\n((translation_unit) @constant (#set! "priority" 101))\n'}}) - ]] + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { + queries = { + c = hl_query .. '\n((translation_unit) @constant (#set! "priority" 101))\n', + }, + }) + end, hl_query_c) -- expect everything to have Constant highlight screen:expect { grid = [[ @@ -669,11 +683,14 @@ describe('treesitter highlighting (C)', function() hi link @foo.bar Type hi link @foo String ]] - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c", {}) - local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = "(primitive_type) @foo.bar (string_literal) @foo"}}) - ]] + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c', {}) + local highlighter = vim.treesitter.highlighter + highlighter.new( + parser, + { queries = { c = '(primitive_type) @foo.bar (string_literal) @foo' } } + ) + end) screen:expect { grid = [[ @@ -701,10 +718,12 @@ describe('treesitter highlighting (C)', function() insert(hl_text_c) -- conceal can be empty or a single cchar. - exec_lua [=[ + exec_lua(function() vim.opt.cole = 2 - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = [[ + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { + queries = { + c = [[ ("static" @keyword (#set! conceal "R")) @@ -717,8 +736,10 @@ describe('treesitter highlighting (C)', function() arguments: (argument_list) @arguments) (#eq? @function "multiqueue_put") (#set! @function conceal "V")) - ]]}}) - ]=] + ]], + }, + }) + end) screen:expect { grid = [[ @@ -775,11 +796,11 @@ describe('treesitter highlighting (C)', function() int z = 6; ]]) - exec_lua([[ + exec_lua(function() local query = '((declaration)+ @string)' vim.treesitter.query.set('c', 'highlights', query) vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) - ]]) + end) screen:expect { grid = [[ @@ -805,14 +826,10 @@ describe('treesitter highlighting (C)', function() declarator: (pointer_declarator) @variable.parameter) ]] - exec_lua( - [[ - local query = ... - vim.treesitter.query.set('c', 'highlights', query) + exec_lua(function(query_str) + vim.treesitter.query.set('c', 'highlights', query_str) vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) - ]], - query - ) + end, query) screen:expect { grid = [[ @@ -847,10 +864,10 @@ describe('treesitter highlighting (lua)', function() ffi.cdef("int (*fun)(int, char *);") ]] - exec_lua [[ + exec_lua(function() vim.bo.filetype = 'lua' vim.treesitter.start() - ]] + end) screen:expect { grid = [[ @@ -888,10 +905,10 @@ describe('treesitter highlighting (help)', function() < ]] - exec_lua [[ + exec_lua(function() vim.bo.filetype = 'help' vim.treesitter.start() - ]] + end) screen:expect { grid = [[ @@ -943,15 +960,15 @@ describe('treesitter highlighting (help)', function() ]] ]=]) - exec_lua [[ - parser = vim.treesitter.get_parser(0, "lua", { + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'lua', { injections = { - lua = '(string content: (_) @injection.content (#set! injection.language lua))' - } + lua = '(string content: (_) @injection.content (#set! injection.language lua))', + }, }) vim.treesitter.highlighter.new(parser) - ]] + end) screen:expect { grid = [=[ @@ -967,7 +984,7 @@ describe('treesitter highlighting (help)', function() end) describe('treesitter highlighting (nested injections)', function() - local screen + local screen --- @type test.functional.ui.screen before_each(function() clear() @@ -996,11 +1013,11 @@ vim.cmd([[ ]]) ]=] - exec_lua [[ + exec_lua(function() vim.opt.scrolloff = 0 vim.bo.filetype = 'lua' vim.treesitter.start() - ]] + end) -- invalidate the language tree feed('ggi--[[04x') @@ -1041,10 +1058,10 @@ describe('treesitter highlighting (markdown)', function() clear() screen = Screen.new(40, 6) screen:attach() - exec_lua([[ + exec_lua(function() vim.bo.filetype = 'markdown' vim.treesitter.start() - ]]) + end) end) it('supports hyperlinks', function() -- cgit From e5c174421df3872df0dd3a676609d1e74dfef6a9 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 11 Aug 2024 09:27:48 +0100 Subject: test: support upvalues in exec_lua --- test/functional/treesitter/highlight_spec.lua | 54 +++++++++++++-------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index da001e2ab1..50f5734230 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -187,10 +187,10 @@ describe('treesitter highlighting (C)', function() -- legacy syntax highlighting is used by default screen:expect(hl_grid_legacy_c) - exec_lua(function(hl_query) - vim.treesitter.query.set('c', 'highlights', hl_query) + exec_lua(function() + vim.treesitter.query.set('c', 'highlights', hl_query_c) vim.treesitter.start() - end, hl_query_c) + end) -- treesitter highlighting is used screen:expect(hl_grid_ts_c) @@ -238,11 +238,11 @@ describe('treesitter highlighting (C)', function() ]], } - exec_lua(function(hl_query) + exec_lua(function() local parser = vim.treesitter.get_parser(0, 'c') local highlighter = vim.treesitter.highlighter - highlighter.new(parser, { queries = { c = hl_query } }) - end, hl_query_c) + highlighter.new(parser, { queries = { c = hl_query_c } }) + end) screen:expect(hl_grid_ts_c) feed('5Gocdd') @@ -369,10 +369,10 @@ describe('treesitter highlighting (C)', function() it('is updated with :sort', function() insert(test_text_c) - exec_lua(function(hl_query) + exec_lua(function() local parser = vim.treesitter.get_parser(0, 'c') - vim.treesitter.highlighter.new(parser, { queries = { c = hl_query } }) - end, hl_query_c) + vim.treesitter.highlighter.new(parser, { queries = { c = hl_query_c } }) + end) screen:expect { grid = [[ {3:int} width = {5:INT_MAX}, height = {5:INT_MAX}; | @@ -518,15 +518,15 @@ describe('treesitter highlighting (C)', function() screen:expect { grid = injection_grid_c } - exec_lua(function(hl_query) + exec_lua(function() local parser = vim.treesitter.get_parser(0, 'c', { injections = { c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))', }, }) local highlighter = vim.treesitter.highlighter - highlighter.new(parser, { queries = { c = hl_query } }) - end, hl_query_c) + highlighter.new(parser, { queries = { c = hl_query_c } }) + end) screen:expect { grid = injection_grid_expected_c } end) @@ -536,7 +536,7 @@ describe('treesitter highlighting (C)', function() screen:expect { grid = injection_grid_c } - exec_lua(function(hl_query) + exec_lua(function() vim.treesitter.language.register('c', 'foo') local parser = vim.treesitter.get_parser(0, 'c', { injections = { @@ -544,8 +544,8 @@ describe('treesitter highlighting (C)', function() }, }) local highlighter = vim.treesitter.highlighter - highlighter.new(parser, { queries = { c = hl_query } }) - end, hl_query_c) + highlighter.new(parser, { queries = { c = hl_query_c } }) + end) screen:expect { grid = injection_grid_expected_c } end) @@ -559,14 +559,14 @@ describe('treesitter highlighting (C)', function() } ]]) - exec_lua(function(hl_query) + exec_lua(function() local injection_query = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' - vim.treesitter.query.set('c', 'highlights', hl_query) + vim.treesitter.query.set('c', 'highlights', hl_query_c) vim.treesitter.query.set('c', 'injections', injection_query) vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) - end, hl_query_c) + end) screen:expect { grid = [[ @@ -586,10 +586,10 @@ describe('treesitter highlighting (C)', function() insert(hl_text_c) feed('gg') - exec_lua(function(hl_query) + exec_lua(function() local parser = vim.treesitter.get_parser(0, 'c') - vim.treesitter.highlighter.new(parser, { queries = { c = hl_query } }) - end, hl_query_c) + vim.treesitter.highlighter.new(parser, { queries = { c = hl_query_c } }) + end) screen:expect(hl_grid_ts_c) @@ -629,14 +629,14 @@ describe('treesitter highlighting (C)', function() } ]]) - exec_lua(function(hl_query) + exec_lua(function() local parser = vim.treesitter.get_parser(0, 'c') vim.treesitter.highlighter.new(parser, { queries = { - c = hl_query .. '\n((translation_unit) @constant (#set! "priority" 101))\n', + c = hl_query_c .. '\n((translation_unit) @constant (#set! "priority" 101))\n', }, }) - end, hl_query_c) + end) -- expect everything to have Constant highlight screen:expect { grid = [[ @@ -826,10 +826,10 @@ describe('treesitter highlighting (C)', function() declarator: (pointer_declarator) @variable.parameter) ]] - exec_lua(function(query_str) - vim.treesitter.query.set('c', 'highlights', query_str) + exec_lua(function() + vim.treesitter.query.set('c', 'highlights', query) vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) - end, query) + end) screen:expect { grid = [[ -- cgit From 09d76afe84dd5b895e102dcd8df8ce6271bebfef Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 27 Sep 2024 08:53:30 -0700 Subject: feat(defaults): pretty :help headings #30544 Problem: Headings in :help do not stand out visually. Solution: Define a non-standard `@markup.heading.1.delimiter` group and special-case it in `highlight_group.c`. FUTURE: This is a cheap workaround until we have #25718 which will enable: - fully driven by `vimdoc/highlights.scm` instead of using highlight tricks (`guibg=bg guifg=bg guisp=fg`) - better support of "cterm" ('notermguicolors') --- test/functional/treesitter/highlight_spec.lua | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/functional/treesitter/highlight_spec.lua') diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index 50f5734230..b5a6cb5c17 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -894,9 +894,45 @@ describe('treesitter highlighting (help)', function() [3] = { bold = true, foreground = Screen.colors.Brown }, [4] = { foreground = Screen.colors.Cyan4 }, [5] = { foreground = Screen.colors.Magenta1 }, + title = { bold = true, foreground = Screen.colors.Magenta1 }, + h1_delim = { nocombine = true, underdouble = true }, + h2_delim = { nocombine = true, underline = true }, } end) + it('defaults in vimdoc/highlights.scm', function() + -- Avoid regressions when syncing upstream vimdoc queries. + + insert [[ + ============================================================================== + NVIM DOCUMENTATION + + ------------------------------------------------------------------------------ + ABOUT NVIM *tag-1* *tag-2* + + |news| News + |nvim| NVim + ]] + + feed('gg') + exec_lua(function() + vim.wo.wrap = false + vim.bo.filetype = 'help' + vim.treesitter.start() + end) + + screen:expect({ + grid = [[ + {h1_delim:^========================================}| + {title:NVIM DOCUMENTATION} | + | + {h2_delim:----------------------------------------}| + {title:ABOUT NVIM} | + | + ]], + }) + end) + it('correctly redraws added/removed injections', function() insert [[ >ruby -- cgit