Merge pull request #24649 from neovim/backport-24595

Backport #24595 to release-0.9
This commit is contained in:
Christian Clason 2023-08-10 18:50:39 +02:00 committed by GitHub
commit 1cd08f242f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 3 deletions

View File

@ -99,6 +99,10 @@ function LanguageTree.new(source, lang, opts)
---@type LanguageTreeOpts ---@type LanguageTreeOpts
opts = opts or {} opts = opts or {}
if source == 0 then
source = vim.api.nvim_get_current_buf()
end
local injections = opts.injections or {} local injections = opts.injections or {}
local self = setmetatable({ local self = setmetatable({
_source = source, _source = source,
@ -517,11 +521,13 @@ end
---@param node TSNode ---@param node TSNode
---@param source string|integer ---@param source string|integer
---@param metadata TSMetadata ---@param metadata TSMetadata
---@param include_children boolean
---@return Range6[] ---@return Range6[]
local function get_node_ranges(node, source, metadata, include_children) local function get_node_ranges(node, source, metadata, include_children)
local range = vim.treesitter.get_range(node, source, metadata) 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 } return { range }
end end
@ -529,7 +535,8 @@ local function get_node_ranges(node, source, metadata, include_children)
local srow, scol, sbyte, erow, ecol, ebyte = Range.unpack6(range) 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 child = node:named_child(i)
local c_srow, c_scol, c_sbyte, c_erow, c_ecol, c_ebyte = child:range(true) 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 if c_srow > srow or c_scol > scol then
@ -561,7 +568,10 @@ end
---@param combined boolean ---@param combined boolean
---@param ranges Range6[] ---@param ranges Range6[]
local function add_injection(t, tree_index, pattern, lang, combined, ranges) 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. -- Each tree index should be isolated from the other nodes.
if not t[tree_index] then if not t[tree_index] then

View File

@ -992,4 +992,38 @@ int x = INT_MAX;
}, run_query()) }, run_query())
end) 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) end)