treesitter: add node:id()

This commit is contained in:
Björn Linse
2020-10-30 10:51:41 +01:00
parent 037ffd54dc
commit 03c478ae53
4 changed files with 37 additions and 8 deletions

View File

@@ -136,6 +136,15 @@ tsnode:has_error() *tsnode:has_error()*
tsnode:sexpr() *tsnode:sexpr()*
Get an S-expression representing the node as a string.
tsnode:id() *tsnode:id()*
Get an unique identier for the node inside its own tree.
No guarantees are made about this identifer's internal representation,
except for being a primitive lua type with value equality (so not a table).
Presently it is a (non-printable) string.
NB: the id is not guaranteed to be unique for nodes from different trees.
tsnode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
*tsnode:descendant_for_range()*
Get the smallest node within this node that spans the given range of

View File

@@ -157,7 +157,7 @@ local function on_line_impl(self, buf, line)
a.nvim_buf_set_extmark(buf, ns, start_row, start_col,
{ end_line = end_row, end_col = end_col,
hl_group = hl,
ephemeral = true
ephemeral = true,
})
end
if start_row > line then

View File

@@ -58,6 +58,7 @@ static struct luaL_Reg node_meta[] = {
{ "__tostring", node_tostring },
{ "__eq", node_eq },
{ "__len", node_child_count },
{ "id", node_id },
{ "range", node_range },
{ "start", node_start },
{ "end_", node_end },
@@ -621,6 +622,17 @@ static int node_eq(lua_State *L)
return 1;
}
static int node_id(lua_State *L)
{
TSNode node;
if (!node_check(L, 1, &node)) {
return 0;
}
lua_pushlstring(L, (const char *)&node.id, sizeof node.id);
return 1;
}
static int node_range(lua_State *L)
{
TSNode node;

View File

@@ -80,13 +80,6 @@ describe('treesitter API with C parser', function()
eq({1,2,1,12}, exec_lua("return {descendant:range()}"))
eq("(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))", exec_lua("return descendant:sexpr()"))
eq(true, exec_lua("return child == child"))
-- separate lua object, but represents same node
eq(true, exec_lua("return child == root:child(0)"))
eq(false, exec_lua("return child == descendant2"))
eq(false, exec_lua("return child == nil"))
eq(false, exec_lua("return child == tree"))
feed("2G7|ay")
exec_lua([[
tree2 = parser:parse()
@@ -98,6 +91,21 @@ describe('treesitter API with C parser', function()
eq("<node declaration>", exec_lua("return tostring(descendant2)"))
eq({1,2,1,13}, exec_lua("return {descendant2:range()}"))
eq(true, exec_lua("return child == child"))
-- separate lua object, but represents same node
eq(true, exec_lua("return child == root:child(0)"))
eq(false, exec_lua("return child == descendant2"))
eq(false, exec_lua("return child == nil"))
eq(false, exec_lua("return child == tree"))
eq("string", exec_lua("return type(child:id())"))
eq(true, exec_lua("return child:id() == child:id()"))
-- separate lua object, but represents same node
eq(true, exec_lua("return child:id() == root:child(0):id()"))
eq(false, exec_lua("return child:id() == descendant2:id()"))
eq(false, exec_lua("return child:id() == nil"))
eq(false, exec_lua("return child:id() == tree"))
-- orginal tree did not change
eq({1,2,1,12}, exec_lua("return {descendant:range()}"))