feat(ts): add support for multiline nodes in get_node_text (#14999)

Based on https://github.com/neovim/neovim/pull/14445

This extends `vim.treesitter.query.get_node_text` to return the text
that spans a node's range even if start_row ~= end_row.
This commit is contained in:
Mathias Fußenegger
2021-12-12 12:05:39 +01:00
committed by GitHub
parent 3aff3d6349
commit 1f3c0593eb
2 changed files with 55 additions and 10 deletions

View File

@@ -227,6 +227,43 @@ void ui_refresh(void)
}, res)
end)
it('supports getting text of multiline node', function()
if pending_c_parser(pending) then return end
insert(test_text)
local res = exec_lua([[
local parser = vim.treesitter.get_parser(0, "c")
local tree = parser:parse()[1]
return vim.treesitter.get_node_text(tree:root(), 0)
]])
eq(test_text, res)
local res2 = exec_lua([[
local parser = vim.treesitter.get_parser(0, "c")
local root = parser:parse()[1]:root()
return vim.treesitter.get_node_text(root:child(0):child(0), 0)
]])
eq('void', res2)
end)
it('support getting text where start of node is past EOF', function()
local text = [[
def run
a = <<~E
end]]
insert(text)
local result = exec_lua([[
local fake_node = {}
function fake_node:start()
return 3, 0, 23
end
function fake_node:end_()
return 3, 0, 23
end
return vim.treesitter.get_node_text(fake_node, 0) == nil
]])
eq(true, result)
end)
it('can match special regex characters like \\ * + ( with `vim-match?`', function()
insert('char* astring = "\\n"; (1 + 1) * 2 != 2;')