From 0211f889b9538f7df5fbcb06273d273fb071efff Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 7 Aug 2023 18:22:36 +0100 Subject: 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. --- runtime/lua/vim/treesitter/languagetree.lua | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua index 0d4a1a54dd..4b2628609a 100644 --- a/runtime/lua/vim/treesitter/languagetree.lua +++ b/runtime/lua/vim/treesitter/languagetree.lua @@ -110,6 +110,10 @@ function LanguageTree.new(source, lang, opts) ---@type LanguageTreeOpts opts = opts or {} + if source == 0 then + source = vim.api.nvim_get_current_buf() + end + local injections = opts.injections or {} local self = setmetatable({ _source = source, @@ -561,11 +565,13 @@ end ---@param node TSNode ---@param source string|integer ---@param metadata TSMetadata +---@param include_children boolean ---@return Range6[] local function get_node_ranges(node, source, metadata, include_children) local range = vim.treesitter.get_range(node, source, metadata) + local child_count = node:named_child_count() - if include_children then + if include_children or child_count == 0 then return { range } end @@ -573,7 +579,8 @@ local function get_node_ranges(node, source, metadata, include_children) local srow, scol, sbyte, erow, ecol, ebyte = Range.unpack6(range) - for i = 0, node:named_child_count() - 1 do + -- We are excluding children so we need to mask out their ranges + for i = 0, child_count - 1 do local child = node:named_child(i) local c_srow, c_scol, c_sbyte, c_erow, c_ecol, c_ebyte = child:range(true) if c_srow > srow or c_scol > scol then @@ -604,7 +611,10 @@ end ---@param combined boolean ---@param ranges Range6[] local function add_injection(t, tree_index, pattern, lang, combined, ranges) - assert(type(lang) == 'string') + if #ranges == 0 then + -- Make sure not to add an empty range set as this is interpreted to mean the whole buffer. + return + end -- Each tree index should be isolated from the other nodes. if not t[tree_index] then -- cgit