-
-
Notifications
You must be signed in to change notification settings - Fork 37
Closed
Description
Is there an API similar to Node.text in py-tree-sitter here?
import tree_sitter_python as tspython
from tree_sitter import Language, Parser,Node
PY_LANGUAGE = Language(tspython.language())
parser = Parser(PY_LANGUAGE)
def get_function_name(node:Node) -> str|None:
if node.type == "function_definition":
children = node.named_children
for child in children:
if child.type == "identifier":
return child.text.decode("utf-8") # !!!THIS API
return None
if __name__ == '__main__':
tree = parser.parse(
bytes(
"""
def foo():
if bar:
baz()
""",
"utf8"
)
)
function_node = tree.root_node.children[0]
print(function_node.type)
print("function name: " ,get_function_name(function_node))output:
function_definition
function name: foo
The relevant code for this API in py-tree-sitter
https://github.com/tree-sitter/py-tree-sitter/blob/2a94c018de52dc5e107e12ea15bd02c1af67b44e/tree_sitter/binding/node.c#L550
Metadata
Metadata
Assignees
Labels
No labels