8000 Update treesitter analysis to use global declerations of parser and l… · codellm-devkit/python-sdk@fab8f84 · GitHub
[go: up one dir, main page]

Skip to content

Commit fab8f84

Browse files
committed
Update treesitter analysis to use global declerations of parser and language
Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
1 parent 5f669b5 commit fab8f84

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

cldk/analysis/commons/treesitter/treesitter_java.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
logger = logging.getLogger(__name__)
2828

29+
LANGUAGE: Language = Language(tsjava.language())
30+
PARSER: Parser = Parser(LANGUAGE)
31+
2932

3033
# pylint: disable=too-many-public-methods
3134
class TreesitterJava:
@@ -34,8 +37,7 @@ class TreesitterJava:
3437
"""
3538

3639
def __init__(self) -> None:
37-
self.language: Language = Language(tsjava.language())
38-
self.parser: Parser = Parser(self.language)
40+
pass
3941

4042
def method_is_not_in_class(self, method_name: str, class_body: str) -> bool:
4143
"""Check if a method is in a class.
@@ -78,7 +80,7 @@ def syntax_error(node):
7880

7981
return False
8082

81-
tree = self.parser.parse(bytes(code, "utf-8"))
83+
tree = PARSER.parse(bytes(code, "utf-8"))
8284
if tree is not None:
8385
return not syntax_error(tree.root_node)
8486
return False
@@ -92,7 +94,7 @@ def get_raw_ast(self, code: str) -> Tree:
9294
Returns:
9395
Tree: the raw AST
9496
"""
95-
return self.parser.parse(bytes(code, "utf-8"))
97+
return PARSER.parse(bytes(code, "utf-8"))
9698

9799
def get_all_imports(self, source_code: str) -> Set[str]:
98100
"""Get a list of all the imports in a class.
@@ -176,8 +178,8 @@ def frame_query_and_capture_output(self, query: str, code_to_process: str) -> Ca
176178
code_to_process : str
177179
The code to process.
178180
"""
179-
framed_query: Query = self.language.query(query)
180-
tree = self.parser.parse(bytes(code_to_process, "utf-8"))
181+
framed_query: Query = LANGUAGE.query(query)
182+
tree = PARSER.parse(bytes(code_to_process, "utf-8"))
181183
return Captures(framed_query.captures(tree.root_node))
182184

183185
def get_method_name_from_declaration(self, method_name_string: str) -> str:
@@ -424,7 +426,7 @@ def get_lexical_tokens(self, code: str, filter_by_node_type: List[str] | None =
424426
List of lexical tokens
425427
426428
"""
427-
tree = self.parser.parse(bytes(code, "utf-8"))
429+
tree = PARSER.parse(bytes(code, "utf-8"))
428430
root_node = tree.root_node
429431
lexical_tokens = []
430432

cldk/analysis/commons/treesitter/treesitter_python.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
from cldk.analysis.commons.treesitter.models import Captures
2929
from cldk.analysis.commons.treesitter.utils.treesitter_utils import TreeSitterUtils
3030

31+
LANGUAGE: Language = Language(tspython.language())
32+
PARSER: Parser = Parser(LANGUAGE)
33+
3134

3235
class TreesitterPython:
3336
"""
3437
Tree sitter for Python use cases.
3538
"""
3639

3740
def __init__(self) -> None:
38-
self.language: Language = Language(tspython.language())
39-
self.parser: Parser = Parser(self.language)
4041
self.utils: TreeSitterUtils = TreeSitterUtils()
4142

4243
def is_parsable(self, code: str) -> bool:
@@ -62,7 +63,7 @@ def syntax_error(node):
6263

6364
return False
6465

65-
tree = self.parser.parse(bytes(code, "utf-8"))
66+
tree = PARSER.parse(bytes(code, "utf-8"))
6667
if tree is not None:
6768
return not syntax_error(tree.root_node)
6869
return False
@@ -76,7 +77,7 @@ def get_raw_ast(self, code: str) -> Tree:
7677
Returns:
7778
Tree: the raw AST
7879
"""
79-
return self.parser.parse(bytes(code, "utf-8"))
80+
return PARSER.parse(bytes(code, "utf-8"))
8081

8182
def get_all_methods(self, module: str) -> List[PyMethod]:
8283
"""
@@ -163,8 +164,8 @@ def get_all_imports(self, module: str) -> List[str]:
163164
List[str]: List of imports
164165
"""
165166
import_list = []
166-
captures_from_import: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((import_from_statement) @imports))", module)
167-
captures_import: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((import_statement) @imports))", module)
167+
captures_from_import: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((import_from_statement) @imports))", module)
168+
captures_import: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((import_statement) @imports))", module)
168169
for capture in captures_import:
169170
import_list.append(capture.node.text.decode())
170171
for capture in captures_from_import:
@@ -188,8 +189,8 @@ def get_all_imports_details(self, module: str) -> List[PyImport]:
188189
List[PyImport]: List of imports
189190
"""
190191
import_list = []
191-
captures_from_import: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((import_from_statement) @imports))", module)
192-
captures_import: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((import_statement) @imports))", module)
192+
captures_from_import: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((import_from_statement) @imports))", module)
193+
captures_import: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((import_statement) @imports))", module)
193194
for capture in captures_import:
194195
imports = []
195196
for import_name in capture.node.children:
@@ -223,7 +224,7 @@ def get_all_classes(self, module: str) -> List[PyClass]:
223224
List[PyClass]: returns details of all classes in it
224225
"""
225226
classes: List[PyClass] = []
226-
all_class_details: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((class_definition) @class_name))", module)
227+
all_class_details: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((class_definition) @class_name))", module)
227228
for class_name in all_class_details:
228229
code_body = class_name.node.text.decode()
229230
class_full_signature = "" # TODO: what to fill here
@@ -331,7 +332,7 @@ def __get_function_details(self, node: Node, klass_name: str = "") -> PyMethod:
331332
is_constructor = False
332333
is_static = False
333334
call_sites: List[PyCallSite] = []
334-
call_nodes: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((call) @call_name))", node.text.decode())
335+
call_nodes: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((call) @call_name))", node.text.decode())
335336
for call_node in call_nodes:
336337
call_sites.append(self.__get_call_site_details(call_node.node))
337338
for function_detail in node.children:
@@ -390,9 +391,9 @@ def __get_function_details(self, node: Node, klass_name: str = "") -> PyMethod:
390391
return function
391392

392393
def __get_class_nodes(self, module: str) -> Captures:
393-
captures: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((class_definition) @class_name))", module)
394+
captures: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((class_definition) @class_name))", module)
394395
return captures
395396

396397
def __get_method_nodes(self, module: str) -> Captures:
397-
captures: Captures = self.utils.frame_query_and_capture_output(self.parser, self.language, "(((function_definition) @function_name))", module)
398+
captures: Captures = self.utils.frame_query_and_capture_output(PARSER, LANGUAGE, "(((function_definition) @function_name))", module)
398399
return captures

0 commit comments

Comments
 (0)
0