mirror of
https://github.com/neovim/neovim
synced 2025-07-21 05:42:46 +00:00
feat(treesitter): allow get_node to return anonymous nodes
Adds a new field `include_anonymous` to the `get_node` options to allow anonymous nodes to be returned.
This commit is contained in:
committed by
Christian Clason
parent
bd3b6ec836
commit
1af55bfcf2
@ -167,6 +167,8 @@ TERMINAL
|
|||||||
TREESITTER
|
TREESITTER
|
||||||
|
|
||||||
• |LanguageTree:node_for_range()| gets anonymous and named nodes for a range
|
• |LanguageTree:node_for_range()| gets anonymous and named nodes for a range
|
||||||
|
• |vim.treesitter.get_node()| now takes an option `include_anonymous`, default
|
||||||
|
false, which allows it to return anonymous nodes as well as named nodes.
|
||||||
|
|
||||||
TUI
|
TUI
|
||||||
|
|
||||||
|
@ -773,6 +773,8 @@ get_node({opts}) *vim.treesitter.get_node()*
|
|||||||
filetype)
|
filetype)
|
||||||
• {ignore_injections} (`boolean?`) Ignore injected languages
|
• {ignore_injections} (`boolean?`) Ignore injected languages
|
||||||
(default true)
|
(default true)
|
||||||
|
• {include_anonymous} (`boolean?`) Include anonymous nodes
|
||||||
|
(default false)
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(`TSNode?`) Node at the given position
|
(`TSNode?`) Node at the given position
|
||||||
|
@ -342,6 +342,9 @@ end
|
|||||||
---
|
---
|
||||||
--- Ignore injected languages (default true)
|
--- Ignore injected languages (default true)
|
||||||
--- @field ignore_injections boolean?
|
--- @field ignore_injections boolean?
|
||||||
|
---
|
||||||
|
--- Include anonymous nodes (default false)
|
||||||
|
--- @field include_anonymous boolean?
|
||||||
|
|
||||||
--- Returns the smallest named node at the given position
|
--- Returns the smallest named node at the given position
|
||||||
---
|
---
|
||||||
@ -388,6 +391,9 @@ function M.get_node(opts)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if opts.include_anonymous then
|
||||||
|
return root_lang_tree:node_for_range(ts_range, opts)
|
||||||
|
end
|
||||||
return root_lang_tree:named_node_for_range(ts_range, opts)
|
return root_lang_tree:named_node_for_range(ts_range, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -55,6 +55,23 @@ describe('treesitter node API', function()
|
|||||||
eq('identifier', lua_eval('node:type()'))
|
eq('identifier', lua_eval('node:type()'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('get_node() with anonymous nodes included', function()
|
||||||
|
insert([[print('test')]])
|
||||||
|
|
||||||
|
exec_lua([[
|
||||||
|
parser = vim.treesitter.get_parser(0, 'lua')
|
||||||
|
tree = parser:parse()[1]
|
||||||
|
node = vim.treesitter.get_node({
|
||||||
|
bufnr = 0,
|
||||||
|
pos = { 0, 6 }, -- on the first apostrophe
|
||||||
|
include_anonymous = true,
|
||||||
|
})
|
||||||
|
]])
|
||||||
|
|
||||||
|
eq("'", lua_eval('node:type()'))
|
||||||
|
eq(false, lua_eval('node:named()'))
|
||||||
|
end)
|
||||||
|
|
||||||
it('can move between siblings', function()
|
it('can move between siblings', function()
|
||||||
insert([[
|
insert([[
|
||||||
int main(int x, int y, int z) {
|
int main(int x, int y, int z) {
|
||||||
|
Reference in New Issue
Block a user