|
1 | 1 | from collections.abc import Sequence |
2 | 2 | from typing import Protocol, runtime_checkable |
| 3 | +from os import PathLike |
3 | 4 |
|
4 | 5 | from cedarscript_ast_parser import Marker, RelativeMarker, RelativePositionType, Segment, MarkerType, BodyOrWhole |
5 | 6 | from .range_spec import IdentifierBoundaries, RangeSpec |
6 | 7 |
|
7 | 8 |
|
8 | | -def read_file(file_path: str) -> str: |
| 9 | +def read_file(file_path: str | PathLike) -> str: |
9 | 10 | with open(file_path, 'r') as file: |
10 | 11 | return file.read() |
11 | 12 |
|
12 | 13 |
|
13 | | -def write_file(file_path: str, lines: Sequence[str]): |
| 14 | +def write_file(file_path: str | PathLike, lines: Sequence[str]): |
14 | 15 | with open(file_path, 'w') as file: |
15 | 16 | file.writelines([line + '\n' for line in lines]) |
16 | 17 |
|
@@ -90,8 +91,13 @@ def segment_to_search_range( |
90 | 91 | start_index_for_end_marker = start_match_result.as_index |
91 | 92 | if start_relpos.qualifier == RelativePositionType.AFTER: |
92 | 93 | start_index_for_end_marker += -1 |
93 | | - end_match_result = RangeSpec.from_line_marker(lines, end_relpos, RangeSpec(start_index_for_end_marker, search_range.end, start_match_result.indent)) |
94 | | - assert end_match_result, f"Unable to find segment end `{end_relpos}` - Try: 1) using *exactly* the same characters from source; or 2) using a marker from below" |
| 94 | + end_match_result = RangeSpec.from_line_marker(lines, end_relpos, RangeSpec( |
| 95 | + start_index_for_end_marker, search_range.end, start_match_result.indent |
| 96 | + )) |
| 97 | + assert end_match_result, ( |
| 98 | + f"Unable to find segment end `{end_relpos}` - Try: " |
| 99 | + f"1) using *exactly* the same characters from source; or 2) using a marker from below" |
| 100 | + ) |
95 | 101 | if end_match_result.as_index > -1: |
96 | 102 | one_after_end = end_match_result.as_index + 1 |
97 | 103 | end_match_result = RangeSpec(one_after_end, one_after_end, end_match_result.indent) |
|
0 commit comments