Skip to content

Commit

Permalink
feat(tree): add language getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Aug 22, 2024
1 parent 2745c7d commit 9e3806b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/classes/tree_sitter.Tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ Tree
----------

.. autoattribute:: included_ranges
.. autoattribute:: language

.. versionadded:: 0.23.0
.. autoattribute:: root_node
1 change: 1 addition & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def read(byte_position, _):

self.assertEqual(snake_node.type, "string")
self.assertEqual(snake.decode("utf16"), "🐍")
self.assertIs(tree.language, self.javascript)


def test_parse_invalid_encoding(self):
Expand Down
3 changes: 3 additions & 0 deletions tree_sitter/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class Tree:
def root_node(self) -> Node: ...
@property
def included_ranges(self) -> list[Range]: ...
@property
def language(self) -> Language: ...
def root_node_with_offset(
self,
offset_bytes: int,
Expand All @@ -175,6 +177,7 @@ class Tree:
) -> None: ...
def walk(self) -> TreeCursor: ...
def changed_ranges(self, new_tree: Tree) -> list[Range]: ...
# TODO(0.24): add copy methods

@final
class TreeCursor:
Expand Down
1 change: 1 addition & 0 deletions tree_sitter/binding/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ PyObject *parser_parse(Parser *self, PyObject *args, PyObject *kwargs) {
return NULL;
}
tree->tree = new_tree;
tree->language = self->language;
tree->source = keep_text ? source_or_callback : Py_None;
Py_INCREF(tree->source);
return PyObject_Init((PyObject *)tree, state->tree_type);
Expand Down
5 changes: 5 additions & 0 deletions tree_sitter/binding/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ PyObject *tree_get_included_ranges(Tree *self, PyObject *Py_UNUSED(args)) {
return result;
}

PyObject *tree_get_language(Tree *self, PyObject *Py_UNUSED(args)) {
Py_INCREF(self->language);
return self->language;
}

PyDoc_STRVAR(tree_root_node_with_offset_doc,
"root_node_with_offset(self, offset_bytes, offset_extent, /)\n--\n\n"
"Get the root node of the syntax tree, but with its position shifted "
Expand Down
9 changes: 4 additions & 5 deletions tree_sitter/binding/tree_cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,16 @@ PyObject *tree_cursor_reset_to(TreeCursor *self, PyObject *args) {
Py_RETURN_NONE;
}

PyObject *tree_cursor_copy(PyObject *self, PyObject *Py_UNUSED(args)) {
PyObject *tree_cursor_copy(TreeCursor *self, PyObject *Py_UNUSED(args)) {
ModuleState *state = GET_MODULE_STATE(self);
TreeCursor *origin = (TreeCursor *)self;
TreeCursor *copied = PyObject_New(TreeCursor, state->tree_cursor_type);
if (copied == NULL) {
return NULL;
}

Py_INCREF(origin->tree);
copied->tree = origin->tree;
copied->cursor = ts_tree_cursor_copy(&origin->cursor);
Py_INCREF(self->tree);
copied->tree = self->tree;
copied->cursor = ts_tree_cursor_copy(&self->cursor);
return PyObject_Init((PyObject *)copied, state->tree_cursor_type);
}

Expand Down
1 change: 1 addition & 0 deletions tree_sitter/binding/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct {
PyObject_HEAD
TSTree *tree;
PyObject *source;
PyObject *language;
} Tree;

typedef struct {
Expand Down

0 comments on commit 9e3806b

Please sign in to comment.