| 1 |
import re |
| 2 |
from typing import Optional, Tuple, cast |
| 3 |
|
| 4 |
from .ast import Location |
| 5 |
from .location import SourceLocation, get_location |
| 6 |
from .source import Source |
| 7 |
|
| 8 |
__all__ = ["print_location", "print_source_location"] |
| 9 |
|
| 10 |
|
| 11 |
def print_location(location: Location) -> str: |
| 12 |
"""Render a helpful description of the location in the GraphQL Source document.""" |
| 13 |
return print_source_location( |
| 14 |
location.source, get_location(location.source, location.start) |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
_re_newline = re.compile(r"\r\n|[\n\r]") |
| 19 |
|
| 20 |
|
| 21 |
def print_source_location(source: Source, source_location: SourceLocation) -> str: |
| 22 |
"""Render a helpful description of the location in the GraphQL Source document.""" |
| 23 |
first_line_column_offset = source.location_offset.column - 1 |
| 24 |
body = "".rjust(first_line_column_offset) + source.body |
| 25 |
|
| 26 |
line_index = source_location.line - 1 |
| 27 |
line_offset = source.location_offset.line - 1 |
| 28 |
line_num = source_location.line + line_offset |
| 29 |
|
| 30 |
column_offset = first_line_column_offset if source_location.line == 1 else 0 |
| 31 |
column_num = source_location.column + column_offset |
| 32 |
location_str = f"{source.name}:{line_num}:{column_num}\n" |
| 33 |
|
| 34 |
lines = _re_newline.split(body) # works a bit different from splitlines() |
| 35 |
location_line = lines[line_index] |
| 36 |
|
| 37 |
# Special case for minified documents |
| 38 |
if len(location_line) > 120: |
| 39 |
sub_line_index, sub_line_column_num = divmod(column_num, 80) |
| 40 |
sub_lines = [ |
| 41 |
location_line[i : i + 80] for i in range(0, len(location_line), 80) |
| 42 |
] |
| 43 |
|
| 44 |
return location_str + print_prefixed_lines( |
| 45 |
(f"{line_num} |", sub_lines[0]), |
| 46 |
*[("|", sub_line) for sub_line in sub_lines[1 : sub_line_index + 1]], |
| 47 |
("|", "^".rjust(sub_line_column_num)), |
| 48 |
( |
| 49 |
"|", |
| 50 |
( |
| 51 |
sub_lines[sub_line_index + 1] |
| 52 |
if sub_line_index < len(sub_lines) - 1 |
| 53 |
else None |
| 54 |
), |
| 55 |
), |
| 56 |
) |
| 57 |
|
| 58 |
return location_str + print_prefixed_lines( |
| 59 |
(f"{line_num - 1} |", lines[line_index - 1] if line_index > 0 else None), |
| 60 |
(f"{line_num} |", location_line), |
| 61 |
("|", "^".rjust(column_num)), |
| 62 |
( |
| 63 |
f"{line_num + 1} |", |
| 64 |
lines[line_index + 1] if line_index < len(lines) - 1 else None, |
| 65 |
), |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
def print_prefixed_lines(*lines: Tuple[str, Optional[str]]) -> str: |
| 70 |
"""Print lines specified like this: ("prefix", "string")""" |
| 71 |
existing_lines = [ |
| 72 |
cast(Tuple[str, str], line) for line in lines if line[1] is not None |
| 73 |
] |
| 74 |
pad_len = max(len(line[0]) for line in existing_lines) |
| 75 |
return "\n".join( |
| 76 |
prefix.rjust(pad_len) + (" " + line if line else "") |
| 77 |
for prefix, line in existing_lines |
| 78 |
) |