Skip to content

Commit 236f4ab

Browse files
test: add test for load dynamic lang in python
1 parent 8b660cf commit 236f4ab

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

crates/pyo3/ast_grep_py/__init__.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from typing import List, TypedDict, Literal, Dict, Union, Mapping
4-
from .ast_grep_py import SgNode, SgRoot, Pos, Range, Edit
4+
from .ast_grep_py import SgNode, SgRoot, Pos, Range, Edit, register_dynamic_language
55

66
Strictness = Union[Literal["cst"], Literal["smart"], Literal["ast"], Literal["relaxed"], Literal["signature"]]
77

@@ -61,9 +61,6 @@ class CustomLang(TypedDict):
6161
meta_var_char: Optional[str]
6262
expando_char: Optional[str]
6363

64-
def register_dynamic_language(langs: Dict[str, any]):
65-
pass
66-
6764
__all__ = [
6865
"Rule",
6966
"Config",

crates/pyo3/ast_grep_py/ast_grep_py.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, overload, Unpack
22

3-
from . import Rule, Config
3+
from . import Rule, Config, CustomLang
44

55
class Pos:
66
line: int
@@ -65,4 +65,6 @@ class SgNode:
6565

6666
# Edit
6767
def replace(self, new_text: str) -> Edit: ...
68-
def commit_edits(self, edits: List[Edit]) -> str: ...
68+
def commit_edits(self, edits: List[Edit]) -> str: ...
69+
70+
def register_dynamic_language(langs: Dict[str, CustomLang]): ...
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from ast_grep_py import SgRoot, Range, Pos, register_dynamic_language
2+
import platform
3+
4+
source = """
5+
{
6+
"test": 123
7+
}
8+
""".strip()
9+
10+
# at the moment only test darwin arm64
11+
def is_arm_mac():
12+
return platform.system() == 'Darwin' and platform.processor() == 'arm'
13+
14+
def register_lang():
15+
if not is_arm_mac():
16+
return
17+
register_dynamic_language({
18+
"myjson": {
19+
"library_path": "../../benches/fixtures/json-mac.so",
20+
"language_symbol": "tree_sitter_json",
21+
}
22+
})
23+
24+
register_lang()
25+
26+
def test_load_json_lang():
27+
if not is_arm_mac():
28+
return
29+
sg = SgRoot(source, "myjson")
30+
root = sg.root()
31+
node = root.find(pattern="123")
32+
assert node
33+
assert node.kind() == "number"
34+
node = root.find(pattern="456")
35+
assert not node

0 commit comments

Comments
 (0)