Skip to content

Commit 8002d8d

Browse files
committed
fix bug
1 parent 93558d8 commit 8002d8d

File tree

4 files changed

+31
-71
lines changed

4 files changed

+31
-71
lines changed

api/analyzers/analyzer.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_entity_docstring(self, node: Node) -> Optional[str]:
6363
pass
6464

6565
@abstractmethod
66-
def get_top_level_entity_types(self) -> list[str]:
66+
def get_entity_types(self) -> list[str]:
6767
"""
6868
Get the top level entity types for the language.
6969
@@ -84,17 +84,6 @@ def add_symbols(self, entity: Entity) -> None:
8484

8585
pass
8686

87-
@abstractmethod
88-
def add_children(self, entity: Entity) -> None:
89-
"""
90-
Add children to the entity.
91-
92-
Args:
93-
entity (Entity): The entity to add children to.
94-
"""
95-
96-
pass
97-
9887
@abstractmethod
9988
def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, key: str, symbol: Node) -> Entity:
10089
"""

api/analyzers/java/analyzer.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,8 @@ def find_calls(self, method: Entity):
4747
for caller in captures['reference.call']:
4848
method.add_symbol("call", caller)
4949

50-
def find_methods(self, type: Entity):
51-
query = self.language.query("[(method_declaration) (constructor_declaration)] @definition.method")
52-
captures = query.captures(type.node)
53-
if 'definition.method' in captures:
54-
for method_dec in captures['definition.method']:
55-
method = Entity(method_dec)
56-
query = self.language.query("(formal_parameters (formal_parameter type: (_) @parameter))")
57-
captures = query.captures(method_dec)
58-
if 'parameter' in captures:
59-
for parameter in captures['parameter']:
60-
method.add_symbol("parameters", parameter)
61-
if method_dec.type == 'method_declaration':
62-
method.add_symbol("return_type", method_dec.child_by_field_name('type'))
63-
type.add_child(method)
64-
self.find_calls(method)
65-
66-
def get_top_level_entity_types(self) -> list[str]:
67-
return ['class_declaration', 'interface_declaration', 'enum_declaration']
50+
def get_entity_types(self) -> list[str]:
51+
return ['class_declaration', 'interface_declaration', 'enum_declaration', 'method_declaration', 'constructor_declaration']
6852

6953
def add_symbols(self, entity: Entity) -> None:
7054
if entity.node.type == 'class_declaration':
@@ -84,9 +68,8 @@ def add_symbols(self, entity: Entity) -> None:
8468
if 'type' in extends_captures:
8569
for interface in extends_captures['type']:
8670
entity.add_symbol("extend_interface", interface)
87-
88-
def add_children(self, entity: Entity) -> None:
89-
self.find_methods(entity)
71+
elif entity.node.type in ['method_declaration', 'constructor_declaration']:
72+
self.find_calls(entity)
9073

9174
def resolve_type(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, node: Node) -> list[Entity]:
9275
res = []

api/analyzers/python/analyzer.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,8 @@ def find_calls(self, method: Entity):
4141
if 'reference.call' in captures:
4242
for caller in captures['reference.call']:
4343
method.add_symbol("call", caller)
44-
45-
def find_methods(self, type: Entity):
46-
query = self.language.query("(function_definition) @definition.method")
47-
captures = query.captures(type.node)
48-
if 'definition.method' in captures:
49-
for method_dec in captures['definition.method']:
50-
method = Entity(method_dec)
51-
query = self.language.query("(typed_parameter type: (_) @parameter)")
52-
captures = query.captures(method_dec)
53-
if 'parameter' in captures:
54-
for parameter in captures['parameter']:
55-
method.add_symbol("parameters", parameter)
56-
return_type = method_dec.child_by_field_name('return_type')
57-
if return_type:
58-
method.add_symbol("return_type", return_type)
59-
type.add_child(method)
60-
self.find_calls(method)
6144

62-
def get_top_level_entity_types(self) -> list[str]:
45+
def get_entity_types(self) -> list[str]:
6346
return ['class_definition', 'function_definition']
6447

6548
def add_symbols(self, entity: Entity) -> None:
@@ -71,9 +54,8 @@ def add_symbols(self, entity: Entity) -> None:
7154
if 'base_class' in base_classes_captures:
7255
for base_class in base_classes_captures['base_class']:
7356
entity.add_symbol("base_class", base_class)
74-
75-
def add_children(self, entity: Entity) -> None:
76-
self.find_methods(entity)
57+
elif entity.node.type == 'function_definition':
58+
self.find_calls(entity)
7759

7860
def resolve_type(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, node: Node) -> list[Entity]:
7961
res = []
@@ -88,13 +70,8 @@ def resolve_method(self, files: dict[Path, File], lsp: SyncLanguageServer, path:
8870
method_dec = self.find_parent(resolved_node, ['function_definition', 'class_definition'])
8971
if not method_dec:
9072
continue
91-
if method_dec.type == 'class_definition':
92-
res.append(file.entities[method_dec])
93-
elif method_dec in file.entities:
73+
if method_dec in file.entities:
9474
res.append(file.entities[method_dec])
95-
else:
96-
type_dec = self.find_parent(method_dec, ['class_definition'])
97-
res.append(file.entities[type_dec].children[method_dec])
9875
return res
9976

10077
def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, key: str, symbol: Node) -> Entity:

api/analyzers/source_analyzer.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,34 @@ def supported_types(self) -> list[str]:
3939
"""
4040
return list(analyzers.keys())
4141

42-
def create_hierarchy(self, analyzer: AbstractAnalyzer, file: File):
43-
types = analyzer.get_top_level_entity_types()
42+
def create_entity_hierarchy(self, entity: Entity, file: File, analyzer: AbstractAnalyzer, graph: Graph):
43+
types = analyzer.get_entity_types()
44+
stack = list(entity.node.children)
45+
while stack:
46+
node = stack.pop()
47+
if node.type in types:
48+
child = Entity(node)
49+
child.id = graph.add_entity(analyzer.get_entity_label(node), analyzer.get_entity_name(node), analyzer.get_entity_docstring(node), str(file.path), node.start_point.row, node.end_point.row, {})
50+
analyzer.add_symbols(child)
51+
file.add_entity(entity)
52+
entity.add_child(child)
53+
graph.connect_entities("DEFINES", entity.id, child.id)
54+
self.create_entity_hierarchy(child, file, analyzer, graph)
55+
else:
56+
stack.extend(node.children)
57+
58+
def create_hierarchy(self, file: File, analyzer: AbstractAnalyzer, graph: Graph):
59+
types = analyzer.get_entity_types()
4460
stack = [file.tree.root_node]
4561
while stack:
4662
node = stack.pop()
4763
if node.type in types:
4864
entity = Entity(node)
65+
entity.id = graph.add_entity(analyzer.get_entity_label(node), analyzer.get_entity_name(node), analyzer.get_entity_docstring(node), str(file.path), node.start_point.row, node.end_point.row, {})
4966
analyzer.add_symbols(entity)
50-
analyzer.add_children(entity)
5167
file.add_entity(entity)
68+
graph.connect_entities("DEFINES", file.id, entity.id)
69+
self.create_entity_hierarchy(entity, file, analyzer, graph)
5270
else:
5371
stack.extend(node.children)
5472

@@ -85,15 +103,8 @@ def first_pass(self, path: Path, ignore: list[str], graph: Graph) -> None:
85103
self.files[file_path] = file
86104

87105
# Walk thought the AST
88-
self.create_hierarchy(analyzer, file)
89-
90106
graph.add_file(file)
91-
for node, entity in file.entities.items():
92-
entity.id = graph.add_entity(analyzer.get_entity_label(node), analyzer.get_entity_name(node), analyzer.get_entity_docstring(node), str(file_path), node.start_point.row, node.end_point.row, {})
93-
graph.connect_entities("DEFINES", file.id, entity.id)
94-
for child_node, child_entity in entity.children.items():
95-
child_entity.id = graph.add_entity(analyzer.get_entity_label(child_node), analyzer.get_entity_name(child_node), analyzer.get_entity_docstring(child_node), str(file_path), child_node.start_point.row, child_node.end_point.row, {"src": child_node.text.decode("utf-8")})
96-
graph.connect_entities("DEFINES", entity.id, child_entity.id)
107+
self.create_hierarchy(file, analyzer, graph)
97108

98109
def second_pass(self, graph: Graph, path: Path) -> None:
99110
"""

0 commit comments

Comments
 (0)