|
1 | 1 | import re |
2 | | -from typing import List, Optional, Tuple, cast |
| 2 | +from typing import Optional, Tuple, cast |
3 | 3 |
|
4 | 4 | from .ast import Location |
5 | 5 | from .location import SourceLocation, get_location |
@@ -30,24 +30,40 @@ def print_source_location(source: Source, source_location: SourceLocation) -> st |
30 | 30 |
|
31 | 31 | column_offset = first_line_column_offset if source_location.line == 1 else 0 |
32 | 32 | column_num = source_location.column + column_offset |
| 33 | + location_str = f"{source.name}:{line_num}:{column_num}\n" |
33 | 34 |
|
34 | 35 | lines = _re_newline.split(body) # works a bit different from splitlines() |
35 | | - len_lines = len(lines) |
36 | | - |
37 | | - def get_line(index: int) -> Optional[str]: |
38 | | - return lines[index] if 0 <= index < len_lines else None |
39 | | - |
40 | | - return f"{source.name}:{line_num}:{column_num}\n" + print_prefixed_lines( |
41 | | - [ |
42 | | - (f"{line_num - 1}", get_line(line_index - 1)), |
43 | | - (f"{line_num}", get_line(line_index)), |
44 | | - ("", " " * (column_num - 1) + "^"), |
45 | | - (f"{line_num + 1}", get_line(line_index + 1)), |
46 | | - ] |
| 36 | + location_line = lines[line_index] |
| 37 | + |
| 38 | + # Special case for minified documents |
| 39 | + if len(location_line) > 120: |
| 40 | + subline_index, subline_column_num = divmod(column_num, 80) |
| 41 | + sublines = [location_line[i : i + 80] for i in range(0, len(location_line), 80)] |
| 42 | + |
| 43 | + return location_str + print_prefixed_lines( |
| 44 | + (str(line_num), sublines[0]), |
| 45 | + *[("", subline) for subline in sublines[1 : subline_index + 1]], |
| 46 | + (" ", " " * (subline_column_num - 1) + "^"), |
| 47 | + ( |
| 48 | + "", |
| 49 | + sublines[subline_index + 1] |
| 50 | + if subline_index < len(sublines) - 1 |
| 51 | + else None, |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + return location_str + print_prefixed_lines( |
| 56 | + (f"{line_num - 1}", lines[line_index - 1] if line_index > 0 else None), |
| 57 | + (f"{line_num}", location_line), |
| 58 | + ("", " " * (column_num - 1) + "^"), |
| 59 | + ( |
| 60 | + f"{line_num + 1}", |
| 61 | + lines[line_index + 1] if line_index < len(lines) - 1 else None, |
| 62 | + ), |
47 | 63 | ) |
48 | 64 |
|
49 | 65 |
|
50 | | -def print_prefixed_lines(lines: List[Tuple[str, Optional[str]]]) -> str: |
| 66 | +def print_prefixed_lines(*lines: Tuple[str, Optional[str]]) -> str: |
51 | 67 | """Print lines specified like this: ("prefix", "string")""" |
52 | 68 | existing_lines = [ |
53 | 69 | cast(Tuple[str, str], line) for line in lines if line[1] is not None |
|
0 commit comments