|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from sqlmesh.core.linter.rule import Position, Range |
| 4 | +from sqlmesh.utils.pydantic import PydanticModel |
| 5 | +import typing as t |
| 6 | + |
| 7 | + |
| 8 | +class TokenPositionDetails(PydanticModel): |
| 9 | + """ |
| 10 | + Details about a token's position in the source code in the structure provided by SQLGlot. |
| 11 | +
|
| 12 | + Attributes: |
| 13 | + line (int): The line that the token ends on. |
| 14 | + col (int): The column that the token ends on. |
| 15 | + start (int): The start index of the token. |
| 16 | + end (int): The ending index of the token. |
| 17 | + """ |
| 18 | + |
| 19 | + line: int |
| 20 | + col: int |
| 21 | + start: int |
| 22 | + end: int |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def from_meta(meta: t.Dict[str, int]) -> "TokenPositionDetails": |
| 26 | + return TokenPositionDetails( |
| 27 | + line=meta["line"], |
| 28 | + col=meta["col"], |
| 29 | + start=meta["start"], |
| 30 | + end=meta["end"], |
| 31 | + ) |
| 32 | + |
| 33 | + def to_range(self, read_file: t.Optional[t.List[str]]) -> Range: |
| 34 | + """ |
| 35 | + Convert a TokenPositionDetails object to a Range object. |
| 36 | +
|
| 37 | + In the circumstances where the token's start and end positions are the same, |
| 38 | + there is no need for a read_file parameter, as the range can be derived from the token's |
| 39 | + line and column. This is an optimization to avoid unnecessary file reads and should |
| 40 | + only be used when the token represents a single character or position in the file. |
| 41 | +
|
| 42 | + If the token's start and end positions are different, the read_file parameter is required. |
| 43 | +
|
| 44 | + :param read_file: List of lines from the file. Optional |
| 45 | + :return: A Range object representing the token's position |
| 46 | + """ |
| 47 | + if self.start == self.end: |
| 48 | + # If the start and end positions are the same, we can create a range directly |
| 49 | + return Range( |
| 50 | + start=Position(line=self.line - 1, character=self.col - 1), |
| 51 | + end=Position(line=self.line - 1, character=self.col), |
| 52 | + ) |
| 53 | + |
| 54 | + if read_file is None: |
| 55 | + raise ValueError("read_file must be provided when start and end positions differ.") |
| 56 | + |
| 57 | + # Convert from 1-indexed to 0-indexed for line only |
| 58 | + end_line_0 = self.line - 1 |
| 59 | + end_col_0 = self.col |
| 60 | + |
| 61 | + # Find the start line and column by counting backwards from the end position |
| 62 | + start_pos = self.start |
| 63 | + end_pos = self.end |
| 64 | + |
| 65 | + # Initialize with the end position |
| 66 | + start_line_0 = end_line_0 |
| 67 | + start_col_0 = end_col_0 - (end_pos - start_pos + 1) |
| 68 | + |
| 69 | + # If start_col_0 is negative, we need to go back to previous lines |
| 70 | + while start_col_0 < 0 and start_line_0 > 0: |
| 71 | + start_line_0 -= 1 |
| 72 | + start_col_0 += len(read_file[start_line_0]) |
| 73 | + # Account for newline character |
| 74 | + if start_col_0 >= 0: |
| 75 | + break |
| 76 | + start_col_0 += 1 # For the newline character |
| 77 | + |
| 78 | + # Ensure we don't have negative values |
| 79 | + start_col_0 = max(0, start_col_0) |
| 80 | + return Range( |
| 81 | + start=Position(line=start_line_0, character=start_col_0), |
| 82 | + end=Position(line=end_line_0, character=end_col_0), |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def read_range_from_file(file: Path, text_range: Range) -> str: |
| 87 | + """ |
| 88 | + Read the file and return the content within the specified range. |
| 89 | +
|
| 90 | + Args: |
| 91 | + file: Path to the file to read |
| 92 | + text_range: The range of text to extract |
| 93 | +
|
| 94 | + Returns: |
| 95 | + The content within the specified range |
| 96 | + """ |
| 97 | + with file.open("r", encoding="utf-8") as f: |
| 98 | + lines = f.readlines() |
| 99 | + |
| 100 | + # Ensure the range is within bounds |
| 101 | + start_line = max(0, text_range.start.line) |
| 102 | + end_line = min(len(lines), text_range.end.line + 1) |
| 103 | + |
| 104 | + if start_line >= end_line: |
| 105 | + return "" |
| 106 | + |
| 107 | + # Extract the relevant portions of each line |
| 108 | + result = [] |
| 109 | + for i in range(start_line, end_line): |
| 110 | + line = lines[i] |
| 111 | + start_char = text_range.start.character if i == text_range.start.line else 0 |
| 112 | + end_char = text_range.end.character if i == text_range.end.line else len(line) |
| 113 | + result.append(line[start_char:end_char]) |
| 114 | + |
| 115 | + return "".join(result) |
0 commit comments