Skip to content

Commit

Permalink
feat: support get root
Browse files Browse the repository at this point in the history
part of #389
  • Loading branch information
HerringtonDarkholme committed Oct 23, 2023
1 parent 40028b0 commit f1cf76e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/pyo3/ast_grep_pyo3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class SgRoot:
class SgNode:
def range(self) -> Range: pass
def find(self, config = None, **kwargs) -> SgNode: ...
def get_match(self, meta_var: str) -> Optional[SgNode]: ...
def get_match(self, meta_var: str) -> Optional[SgNode]: ...
def text(self) -> str: ...
4 changes: 3 additions & 1 deletion crates/pyo3/src/py_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ impl SgNode {
}

/*---------- Tree Traversal ----------*/
// TODO get_root
fn get_root(&self) -> Py<SgRoot> {
self.root.clone()
}

#[pyo3(signature = (config=None, **kwargs))]
fn find(&self, config: Option<&PyDict>, kwargs: Option<&PyDict>) -> Option<Self> {
Expand Down
12 changes: 11 additions & 1 deletion crates/pyo3/tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ def test_get_match():
assert a.text() == 'a'
rng = a.range()
assert rng.start.line == 1
assert rng.start.column == 6
assert rng.start.column == 6

def test_get_multi_match():
pass

def test_get_root():
node = root.find(pattern = 'let a = $A')
assert node is not None
root2 = node.get_root()
assert root2.filename() == 'anonymous'
# assert root2 == root

0 comments on commit f1cf76e

Please sign in to comment.