-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathanalyzer.py
More file actions
180 lines (137 loc) · 5.08 KB
/
analyzer.py
File metadata and controls
180 lines (137 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from pathlib import Path
from typing import Optional
from tree_sitter import Language, Node, Parser, Point, QueryCursor
from api.entities.entity import Entity
from api.entities.file import File
from abc import ABC, abstractmethod
from multilspy import SyncLanguageServer
class AbstractAnalyzer(ABC):
def __init__(self, language: Language) -> None:
self.language = language
self.parser = Parser(language)
def _captures(self, pattern: str, node: Node) -> dict:
"""Run a tree-sitter query and return captures dict."""
query = self.language.query(pattern)
cursor = QueryCursor(query)
return cursor.captures(node)
def find_parent(self, node: Node, parent_types: list) -> Node:
while node and node.type not in parent_types:
node = node.parent
return node
@abstractmethod
def is_dependency(self, file_path: str) -> bool:
"""
Check if the file is a dependency.
Args:
file_path (str): The file path.
Returns:
bool: True if the file is a dependency, False otherwise.
"""
pass
@abstractmethod
def resolve_path(self, file_path: str, path: Path) -> str:
"""
Resolve the path of the file.
Args:
file_path (str): The file path.
path (Path): The path to the folder.
Returns:
str: The resolved path.
"""
pass
def resolve(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, node: Node) -> list[tuple[File, Node]]:
try:
locations = lsp.request_definition(str(file_path), node.start_point.row, node.start_point.column)
return [(files[Path(self.resolve_path(location['absolutePath'], path))], files[Path(self.resolve_path(location['absolutePath'], path))].tree.root_node.descendant_for_point_range(Point(location['range']['start']['line'], location['range']['start']['character']), Point(location['range']['end']['line'], location['range']['end']['character']))) for location in locations if location and Path(self.resolve_path(location['absolutePath'], path)) in files]
except Exception:
return []
@abstractmethod
def add_dependencies(self, path: Path, files: list[Path]):
"""
Add dependencies to the files.
Args:
path (Path): The path to the folder.
files (dict[Path, File]): The files.
"""
pass
@abstractmethod
def get_entity_label(self, node: Node) -> str:
"""
Get the entity label from the node.
Args:
node (Node): The node.
Returns:
str: The entity label.
"""
pass
@abstractmethod
def get_entity_name(self, node: Node) -> str:
"""
Get the entity name from the node.
Args:
node (Node): The node.
Returns:
str: The entity name.
"""
pass
@abstractmethod
def get_entity_docstring(self, node: Node) -> Optional[str]:
"""
Get the entity docstring from the node.
Args:
node (Node): The node.
Returns:
Optional[str]: The entity docstring.
"""
pass
@abstractmethod
def get_entity_types(self) -> list[str]:
"""
Get the top level entity types for the language.
Returns:
list[str]: The list of top level entity types.
"""
pass
@abstractmethod
def add_symbols(self, entity: Entity) -> None:
"""
Add symbols to the entity.
Args:
entity (Entity): The entity to add symbols to.
"""
pass
@abstractmethod
def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, key: str, symbol: Node) -> list[Entity]:
"""
Resolve a symbol to entities.
Args:
lsp (SyncLanguageServer): The language server.
path (Path): The path to the file.
key (str): The symbol key.
symbol (Node): The symbol node.
Returns:
list[Entity]: The resolved entities.
"""
pass
@abstractmethod
def add_file_imports(self, file: File) -> None:
"""
Add import statements to the file.
Args:
file (File): The file to add imports to.
"""
pass
@abstractmethod
def resolve_import(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, import_node: Node) -> list[Entity]:
"""
Resolve an import statement to entities.
Args:
files (dict[Path, File]): All files in the project.
lsp (SyncLanguageServer): The language server.
file_path (Path): The path to the file containing the import.
path (Path): The path to the project root.
import_node (Node): The import statement node.
Returns:
list[Entity]: List of resolved entities.
"""
pass