Merge pull request #14418 from DerekStride/treesitter-set-directive

[RDY] tree-sitter ensure there is a nested table allocated for `#set!`
This commit is contained in:
Thomas Vigouroux 2021-05-03 21:41:39 +02:00 committed by GitHub
commit 3fc71ea228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -275,7 +275,11 @@ local directive_handlers = {
["set!"] = function(_, _, _, pred, metadata)
if #pred == 4 then
-- (#set! @capture "key" "value")
metadata[pred[2]][pred[3]] = pred[4]
local capture = pred[2]
if not metadata[capture] then
metadata[capture] = {}
end
metadata[capture][pred[3]] = pred[4]
else
-- (#set! "key" "value")
metadata[pred[2]] = pred[3]

View File

@ -599,6 +599,56 @@ int x = INT_MAX;
eq(result, "value")
end)
describe("when setting a key on a capture", function()
it("it should create the nested table", function()
insert([[
int x = 3;
]])
local result = exec_lua([[
local query = require("vim.treesitter.query")
local value
query = vim.treesitter.parse_query("c", '((number_literal) @number (#set! @number "key" "value"))')
parser = vim.treesitter.get_parser(0, "c")
for pattern, match, metadata in query:iter_matches(parser:parse()[1]:root(), 0) do
for _, nested_tbl in pairs(metadata) do
return nested_tbl.key
end
end
]])
eq(result, "value")
end)
it("it should not overwrite the nested table", function()
insert([[
int x = 3;
]])
local result = exec_lua([[
local query = require("vim.treesitter.query")
local result
query = vim.treesitter.parse_query("c", '((number_literal) @number (#set! @number "key" "value") (#set! @number "key2" "value2"))')
parser = vim.treesitter.get_parser(0, "c")
for pattern, match, metadata in query:iter_matches(parser:parse()[1]:root(), 0) do
for _, nested_tbl in pairs(metadata) do
return nested_tbl
end
end
]])
local expected = {
["key"] = "value",
["key2"] = "value2",
}
eq(expected, result)
end)
end)
end)
end)
end)