|
1 | | -import io |
2 | 1 | from pathlib import Path |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from tree_sitter import Language, Node, Parser, Point |
| 5 | +from api.entities.entity import Entity |
| 6 | +from api.entities.file import File |
3 | 7 | from abc import ABC, abstractmethod |
| 8 | +from multilspy import SyncLanguageServer |
4 | 9 |
|
5 | 10 | class AbstractAnalyzer(ABC): |
| 11 | + def __init__(self, language: Language) -> None: |
| 12 | + self.language = language |
| 13 | + self.parser = Parser(language) |
| 14 | + |
| 15 | + def find_parent(self, node: Node, parent_types: list) -> Node: |
| 16 | + while node and node.type not in parent_types: |
| 17 | + node = node.parent |
| 18 | + return node |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def is_dependency(self, file_path: str) -> bool: |
| 22 | + """ |
| 23 | + Check if the file is a dependency. |
| 24 | +
|
| 25 | + Args: |
| 26 | + file_path (str): The file path. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + bool: True if the file is a dependency, False otherwise. |
| 30 | + """ |
| 31 | + |
| 32 | + pass |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def resolve_path(self, file_path: str, path: Path) -> str: |
| 36 | + """ |
| 37 | + Resolve the path of the file. |
| 38 | +
|
| 39 | + Args: |
| 40 | + file_path (str): The file path. |
| 41 | + path (Path): The path to the folder. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + str: The resolved path. |
| 45 | + """ |
| 46 | + |
| 47 | + pass |
| 48 | + |
| 49 | + def resolve(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, node: Node) -> list[tuple[File, Node]]: |
| 50 | + try: |
| 51 | + locations = lsp.request_definition(str(file_path), node.start_point.row, node.start_point.column) |
| 52 | + 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] |
| 53 | + except Exception as e: |
| 54 | + return [] |
| 55 | + |
6 | 56 | @abstractmethod |
7 | | - def first_pass(self, path: Path, f: io.TextIOWrapper) -> None: |
| 57 | + def add_dependencies(self, path: Path, files: list[Path]): |
8 | 58 | """ |
9 | | - Perform the first pass of analysis on the given file. |
| 59 | + Add dependencies to the files. |
10 | 60 |
|
11 | 61 | Args: |
12 | | - path (Path): The path to the file being processed. |
13 | | - f (io.TextIOWrapper): The file object. |
| 62 | + path (Path): The path to the folder. |
| 63 | + files (dict[Path, File]): The files. |
| 64 | + """ |
| 65 | + |
| 66 | + pass |
| 67 | + |
| 68 | + @abstractmethod |
| 69 | + def get_entity_label(self, node: Node) -> str: |
14 | 70 | """ |
| 71 | + Get the entity label from the node. |
15 | 72 |
|
| 73 | + Args: |
| 74 | + node (Node): The node. |
| 75 | + |
| 76 | + Returns: |
| 77 | + str: The entity label. |
| 78 | + """ |
16 | 79 | pass |
17 | 80 |
|
18 | 81 | @abstractmethod |
19 | | - def second_pass(self, path: Path, f: io.TextIOWrapper) -> None: |
| 82 | + def get_entity_name(self, node: Node) -> str: |
20 | 83 | """ |
21 | | - Perform a second pass analysis on the given source file. |
| 84 | + Get the entity name from the node. |
22 | 85 |
|
23 | 86 | Args: |
| 87 | + node (Node): The node. |
| 88 | + |
| 89 | + Returns: |
| 90 | + str: The entity name. |
| 91 | + """ |
| 92 | + pass |
| 93 | + |
| 94 | + @abstractmethod |
| 95 | + def get_entity_docstring(self, node: Node) -> Optional[str]: |
| 96 | + """ |
| 97 | + Get the entity docstring from the node. |
| 98 | +
|
| 99 | + Args: |
| 100 | + node (Node): The node. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + Optional[str]: The entity docstring. |
| 104 | + """ |
| 105 | + pass |
| 106 | + |
| 107 | + @abstractmethod |
| 108 | + def get_entity_types(self) -> list[str]: |
| 109 | + """ |
| 110 | + Get the top level entity types for the language. |
| 111 | +
|
| 112 | + Returns: |
| 113 | + list[str]: The list of top level entity types. |
| 114 | + """ |
| 115 | + |
| 116 | + pass |
| 117 | + |
| 118 | + @abstractmethod |
| 119 | + def add_symbols(self, entity: Entity) -> None: |
| 120 | + """ |
| 121 | + Add symbols to the entity. |
| 122 | +
|
| 123 | + Args: |
| 124 | + entity (Entity): The entity to add symbols to. |
| 125 | + """ |
| 126 | + |
| 127 | + pass |
| 128 | + |
| 129 | + @abstractmethod |
| 130 | + def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, key: str, symbol: Node) -> list[Entity]: |
| 131 | + """ |
| 132 | + Resolve a symbol to an entity. |
| 133 | +
|
| 134 | + Args: |
| 135 | + lsp (SyncLanguageServer): The language server. |
24 | 136 | path (Path): The path to the file. |
25 | | - f (io.TextIOWrapper): The file handle of the file to be processed. |
| 137 | + key (str): The symbol key. |
| 138 | + symbol (Node): The symbol node. |
| 139 | +
|
| 140 | + Returns: |
| 141 | + list[Entity]: The resolved entities. |
26 | 142 | """ |
27 | 143 |
|
28 | 144 | pass |
|
0 commit comments