|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import Optional, Tuple |
| 4 | + |
| 5 | +from ...common.lsp_types import Position, Range |
| 6 | +from ..utils.ast import Token |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class SourceEntity: |
| 11 | + line_no: int |
| 12 | + col_offset: int |
| 13 | + end_line_no: int |
| 14 | + end_col_offset: int |
| 15 | + source: str |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class Import(SourceEntity): |
| 20 | + name: Optional[str] |
| 21 | + name_token: Optional[Token] |
| 22 | + |
| 23 | + def range(self) -> Range: |
| 24 | + return Range( |
| 25 | + start=Position( |
| 26 | + line=self.name_token.lineno - 1 if self.name_token is not None else self.line_no - 1, |
| 27 | + character=self.name_token.col_offset if self.name_token is not None else self.col_offset, |
| 28 | + ), |
| 29 | + end=Position( |
| 30 | + line=self.name_token.lineno - 1 if self.name_token is not None else self.end_line_no - 1, |
| 31 | + character=self.name_token.end_col_offset if self.name_token is not None else self.end_col_offset, |
| 32 | + ), |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class LibraryImport(Import): |
| 38 | + args: Tuple[str, ...] = () |
| 39 | + alias: Optional[str] = None |
| 40 | + |
| 41 | + def __hash__(self) -> int: |
| 42 | + return hash( |
| 43 | + ( |
| 44 | + type(self), |
| 45 | + self.name, |
| 46 | + self.args, |
| 47 | + self.alias, |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@dataclass |
| 53 | +class ResourceImport(Import): |
| 54 | + def __hash__(self) -> int: |
| 55 | + return hash( |
| 56 | + ( |
| 57 | + type(self), |
| 58 | + self.name, |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@dataclass |
| 64 | +class VariablesImport(Import): |
| 65 | + args: Tuple[str, ...] = () |
| 66 | + |
| 67 | + def __hash__(self) -> int: |
| 68 | + return hash( |
| 69 | + ( |
| 70 | + type(self), |
| 71 | + self.name, |
| 72 | + self.args, |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +class VariableDefinitionType(Enum): |
| 78 | + VARIABLE = "variable" |
| 79 | + ARGUMENT = "argument" |
| 80 | + COMMAND_LINE_VARIABLE = "command line variable" |
| 81 | + BUILTIN_VARIABLE = "builtin variable" |
| 82 | + IMPORTED_VARIABLE = "imported variable" |
| 83 | + |
| 84 | + |
| 85 | +@dataclass |
| 86 | +class VariableDefinition(SourceEntity): |
| 87 | + name: Optional[str] |
| 88 | + name_token: Optional[Token] |
| 89 | + type: VariableDefinitionType = VariableDefinitionType.VARIABLE |
| 90 | + |
| 91 | + def __hash__(self) -> int: |
| 92 | + return hash((type(self), self.name, self.type)) |
| 93 | + |
| 94 | + def range(self) -> Range: |
| 95 | + return Range( |
| 96 | + start=Position( |
| 97 | + line=self.line_no - 1, |
| 98 | + character=self.col_offset, |
| 99 | + ), |
| 100 | + end=Position( |
| 101 | + line=self.end_line_no - 1, |
| 102 | + character=self.end_col_offset, |
| 103 | + ), |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +@dataclass |
| 108 | +class BuiltInVariableDefinition(VariableDefinition): |
| 109 | + type: VariableDefinitionType = VariableDefinitionType.BUILTIN_VARIABLE |
| 110 | + |
| 111 | + def __hash__(self) -> int: |
| 112 | + return hash((type(self), self.name, self.type)) |
| 113 | + |
| 114 | + |
| 115 | +@dataclass |
| 116 | +class CommandLineVariableDefinition(VariableDefinition): |
| 117 | + type: VariableDefinitionType = VariableDefinitionType.COMMAND_LINE_VARIABLE |
| 118 | + |
| 119 | + def __hash__(self) -> int: |
| 120 | + return hash((type(self), self.name, self.type)) |
| 121 | + |
| 122 | + |
| 123 | +@dataclass |
| 124 | +class ArgumentDefinition(VariableDefinition): |
| 125 | + type: VariableDefinitionType = VariableDefinitionType.ARGUMENT |
| 126 | + |
| 127 | + def __hash__(self) -> int: |
| 128 | + return hash((type(self), self.name, self.type)) |
| 129 | + |
| 130 | + |
| 131 | +@dataclass |
| 132 | +class ImportedVariableDefinition(VariableDefinition): |
| 133 | + type: VariableDefinitionType = VariableDefinitionType.IMPORTED_VARIABLE |
| 134 | + |
| 135 | + def __hash__(self) -> int: |
| 136 | + return hash((type(self), self.name, self.type)) |
0 commit comments