fix(treesitter)!: remove deprecated legacy injection format

This commit is contained in:
Christian Clason 2023-08-12 17:54:04 +02:00
parent 006152ff7a
commit fc0ee871de
4 changed files with 12 additions and 124 deletions

View File

@ -202,6 +202,8 @@ The following deprecated functions or APIs were removed.
• Vimball support is removed.
- :Vimuntar command removed.
• Support for legacy treesitter injection queries is removed.
==============================================================================
DEPRECATIONS *news-deprecations*

View File

@ -769,65 +769,6 @@ function LanguageTree:_get_injection(match, metadata)
return lang, combined, ranges
end
---@private
---@param match table<integer,TSNode>
---@param metadata TSMetadata
---@return string, boolean, Range6[]
function LanguageTree:_get_injection_deprecated(match, metadata)
local lang = nil ---@type string
local ranges = {} ---@type Range6[]
local combined = metadata.combined ~= nil
-- Directives can configure how injections are captured as well as actual node captures.
-- This allows more advanced processing for determining ranges and language resolution.
if metadata.content then
local content = metadata.content ---@type any
-- Allow for captured nodes to be used
if type(content) == 'number' then
content = { match[content]:range() }
end
if type(content) == 'table' and #content >= 4 then
vim.list_extend(ranges, content)
end
end
local mlang = metadata.language
if mlang ~= nil then
assert(type(mlang) == 'string')
lang = mlang
end
-- You can specify the content and language together
-- using a tag with the language, for example
-- @javascript
for id, node in pairs(match) do
local name = self._injection_query.captures[id]
-- Lang should override any other language tag
if name == 'language' and not lang then
lang = vim.treesitter.get_node_text(node, self._source, { metadata = metadata[id] })
elseif name == 'combined' then
combined = true
elseif name == 'content' and #ranges == 0 then
ranges[#ranges + 1] = vim.treesitter.get_range(node, self._source, metadata[id])
-- Ignore any tags that start with "_"
-- Allows for other tags to be used in matches
elseif string.sub(name, 1, 1) ~= '_' then
if not lang then
lang = name
end
if #ranges == 0 then
ranges[#ranges + 1] = vim.treesitter.get_range(node, self._source, metadata[id])
end
end
end
return lang, combined, ranges
end
--- Gets language injection points by language.
---
--- This is where most of the injection processing occurs.
@ -852,11 +793,11 @@ function LanguageTree:_get_injections()
self._injection_query:iter_matches(root_node, self._source, start_line, end_line + 1)
do
local lang, combined, ranges = self:_get_injection(match, metadata)
if not lang then
-- TODO(lewis6991): remove after 0.9 (#20434)
lang, combined, ranges = self:_get_injection_deprecated(match, metadata)
if lang then
add_injection(injections, index, pattern, lang, combined, ranges)
else
self:_log('match from injection query failed for pattern', pattern)
end
add_injection(injections, index, pattern, lang, combined, ranges)
end
end

View File

@ -442,7 +442,7 @@ describe('treesitter highlighting (C)', function()
exec_lua [[
local parser = vim.treesitter.get_parser(0, "c", {
injections = {c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @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}})
@ -480,7 +480,7 @@ describe('treesitter highlighting (C)', function()
]])
exec_lua [[
local injection_query = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"
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)

View File

@ -634,7 +634,7 @@ int x = INT_MAX;
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"}})
c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'}})
parser:parse(true)
]])
@ -667,7 +667,7 @@ int x = INT_MAX;
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = "(preproc_def (preproc_arg) @c @combined) (preproc_function_def value: (preproc_arg) @c @combined)"}})
c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined)) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined))'}})
parser:parse(true)
]])
@ -696,67 +696,12 @@ int x = INT_MAX;
end)
end)
describe("when providing parsing information through a directive", function()
it("should inject a language", function()
exec_lua([=[
vim.treesitter.query.add_directive("inject-clang!", function(match, _, _, pred, metadata)
metadata.language = "c"
metadata.combined = true
metadata.content = pred[2]
end)
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = "(preproc_def ((preproc_arg) @_c (#inject-clang! @_c)))" ..
"(preproc_function_def value: ((preproc_arg) @_a (#inject-clang! @_a)))"}})
parser:parse(true)
]=])
eq("table", exec_lua("return type(parser:children().c)"))
eq(2, exec_lua("return #parser:children().c:trees()"))
eq({
{0, 0, 7, 0}, -- root tree
{3, 14, 5, 18}, -- VALUE 123
-- VALUE1 123
-- VALUE2 123
{1, 26, 2, 66} -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
}, get_ranges())
helpers.feed('ggo<esc>')
eq("table", exec_lua("return type(parser:children().c)"))
eq(2, exec_lua("return #parser:children().c:trees()"))
eq({
{0, 0, 8, 0}, -- root tree
{4, 14, 6, 18}, -- VALUE 123
-- VALUE1 123
-- VALUE2 123
{2, 26, 3, 66} -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
}, get_ranges())
end)
it("should not inject bad languages", function()
exec_lua([=[
vim.treesitter.query.add_directive("inject-bad!", function(match, _, _, pred, metadata)
metadata.language = "{"
metadata.combined = true
metadata.content = pred[2]
end)
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = "(preproc_function_def value: ((preproc_arg) @_a (#inject-bad! @_a)))"}})
]=])
end)
end)
describe("when using the offset directive", function()
it("should shift the range by the directive amount", function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = "(preproc_def ((preproc_arg) @c (#offset! @c 0 2 0 -1))) (preproc_function_def value: (preproc_arg) @c)"}})
c = '(preproc_def ((preproc_arg) @injection.content (#set! injection.language "c") (#offset! @injection.content 0 2 0 -1))) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'}})
parser:parse(true)
]])
@ -797,7 +742,7 @@ int x = INT_MAX;
it("should return the correct language tree", function()
local result = exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = { c = "(preproc_def (preproc_arg) @c)"}})
injections = { c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))'}})
parser:parse(true)
local sub_tree = parser:language_for_range({1, 18, 1, 19})