aboutsummaryrefslogtreecommitdiff
path: root/test/functional/treesitter/parser_spec.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-08-07 18:22:36 +0100
committerGitHub <noreply@github.com>2023-08-07 18:22:36 +0100
commit0211f889b9538f7df5fbcb06273d273fb071efff (patch)
treeb50f0ab1471d60b21eca8e31361745f4ef2a5a69 /test/functional/treesitter/parser_spec.lua
parentce792db5b806b41d5d687e3b09da9ab2b6be0307 (diff)
downloadrneovim-0211f889b9538f7df5fbcb06273d273fb071efff.tar.gz
rneovim-0211f889b9538f7df5fbcb06273d273fb071efff.tar.bz2
rneovim-0211f889b9538f7df5fbcb06273d273fb071efff.zip
fix(treesitter): make sure injections don't return empty ranges (#24595)
When an injection has not set include children, make sure not to add the injection if no ranges are determined. This could happen when there is an injection with a child that has the same range as itself. e.g. consider this Makefile snippet ```make foo: $(VAR) ``` Line 2 has an injection for bash and a make variable reference. If include-children isn't set (default), then there is no range on line 2 to inject since the variable reference needs to be excluded. This caused the language tree to return an empty range, which the parser now interprets to mean the full buffer. This caused makefiles to have completely broken highlighting.
Diffstat (limited to 'test/functional/treesitter/parser_spec.lua')
-rw-r--r--test/functional/treesitter/parser_spec.lua34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua
index f0144e6f6d..da84f435c9 100644
--- a/test/functional/treesitter/parser_spec.lua
+++ b/test/functional/treesitter/parser_spec.lua
@@ -982,4 +982,38 @@ int x = INT_MAX;
eq(rb, r)
end)
+
+ it("does not produce empty injection ranges (#23409)", function()
+ insert [[
+ Examples: >lua
+ local a = {}
+<
+ ]]
+
+ -- This is not a valid injection since (code) has children and include-children is not set
+ exec_lua [[
+ parser1 = require('vim.treesitter.languagetree').new(0, "vimdoc", {
+ injections = {
+ vimdoc = "((codeblock (language) @injection.language (code) @injection.content))"
+ }
+ })
+ parser1:parse()
+ ]]
+
+ eq(0, exec_lua("return #vim.tbl_keys(parser1:children())"))
+
+ exec_lua [[
+ parser2 = require('vim.treesitter.languagetree').new(0, "vimdoc", {
+ injections = {
+ vimdoc = "((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))"
+ }
+ })
+ parser2:parse()
+ ]]
+
+ eq(1, exec_lua("return #vim.tbl_keys(parser2:children())"))
+ eq( { { { 1, 0, 21, 2, 0, 42 } } }, exec_lua("return parser2:children().lua:included_regions()"))
+
+ end)
+
end)