|
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 | + def resolve(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, node: Node) -> list[tuple[File, Node]]: |
| 21 | + try: |
| 22 | + return [(files[Path(location['absolutePath'])], files[Path(location['absolutePath'])].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 lsp.request_definition(str(path), node.start_point.row, node.start_point.column) if location and Path(location['absolutePath']) in files] |
| 23 | + except Exception as e: |
| 24 | + return [] |
| 25 | + |
| 26 | + @abstractmethod |
| 27 | + def get_entity_label(self, node: Node) -> str: |
| 28 | + """ |
| 29 | + Get the entity label from the node. |
| 30 | +
|
| 31 | + Args: |
| 32 | + node (Node): The node. |
| 33 | + |
| 34 | + Returns: |
| 35 | + str: The entity label. |
| 36 | + """ |
| 37 | + pass |
| 38 | + |
| 39 | + @abstractmethod |
| 40 | + def get_entity_name(self, node: Node) -> str: |
| 41 | + """ |
| 42 | + Get the entity name from the node. |
| 43 | +
|
| 44 | + Args: |
| 45 | + node (Node): The node. |
| 46 | + |
| 47 | + Returns: |
| 48 | + str: The entity name. |
| 49 | + """ |
| 50 | + pass |
| 51 | + |
| 52 | + @abstractmethod |
| 53 | + def get_entity_docstring(self, node: Node) -> Optional[str]: |
| 54 | + """ |
| 55 | + Get the entity docstring from the node. |
| 56 | +
|
| 57 | + Args: |
| 58 | + node (Node): The node. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + Optional[str]: The entity docstring. |
| 62 | + """ |
| 63 | + pass |
| 64 | + |
6 | 65 | @abstractmethod
|
7 |
| - def first_pass(self, path: Path, f: io.TextIOWrapper) -> None: |
| 66 | + def get_entity_types(self) -> list[str]: |
8 | 67 | """
|
9 |
| - Perform the first pass of analysis on the given file. |
| 68 | + Get the top level entity types for the language. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + list[str]: The list of top level entity types. |
| 72 | + """ |
| 73 | + |
| 74 | + pass |
| 75 | + |
| 76 | + @abstractmethod |
| 77 | + def add_symbols(self, entity: Entity) -> None: |
| 78 | + """ |
| 79 | + Add symbols to the entity. |
10 | 80 |
|
11 | 81 | Args:
|
12 |
| - path (Path): The path to the file being processed. |
13 |
| - f (io.TextIOWrapper): The file object. |
| 82 | + entity (Entity): The entity to add symbols to. |
14 | 83 | """
|
15 | 84 |
|
16 | 85 | pass
|
17 | 86 |
|
18 | 87 | @abstractmethod
|
19 |
| - def second_pass(self, path: Path, f: io.TextIOWrapper) -> None: |
| 88 | + def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, key: str, symbol: Node) -> Entity: |
20 | 89 | """
|
21 |
| - Perform a second pass analysis on the given source file. |
| 90 | + Resolve a symbol to an entity. |
22 | 91 |
|
23 | 92 | Args:
|
| 93 | + lsp (SyncLanguageServer): The language server. |
24 | 94 | path (Path): The path to the file.
|
25 |
| - f (io.TextIOWrapper): The file handle of the file to be processed. |
| 95 | + key (str): The symbol key. |
| 96 | + symbol (Node): The symbol node. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Entity: The entity. |
26 | 100 | """
|
27 | 101 |
|
28 | 102 | pass
|
|
0 commit comments