Skip to content

Commit a2771fa

Browse files
committed
Minor improvements and fixes
1 parent 9f5f655 commit a2771fa

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

src/cedarscript_editor/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from ._version import __version__
22
import re
33
from .cedarscript_editor import CEDARScriptEditor
4-
from text_manipulation import IndentationInfo, IdentifierBoundaries, RangeSpec, read_file, write_file, bow_to_search_range
4+
from cedarscript_ast_parser import CEDARScriptASTParser
5+
from text_manipulation import (
6+
IndentationInfo, IdentifierBoundaries, RangeSpec, read_file, write_file,
7+
bow_to_search_range
8+
)
59

610
__all__ = [
711
"__version__", "find_commands", "CEDARScriptEditor", "IndentationInfo", "IdentifierBoundaries", "RangeSpec",
812
"read_file", "write_file", "bow_to_search_range"
913
]
1014

15+
1116
def find_commands(content: str):
1217
# Regex pattern to match CEDARScript blocks
1318
pattern = r'```CEDARScript\n(.*?)```'

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def _update_command(self, cmd: UpdateCommand):
121121
# content='\n @0:print("This line will be inserted at the top")\n '
122122
# )
123123

124-
125124
# Example 2:
126125
# UPDATE FUNCTION
127126
# FROM FILE "tmp.benchmarks/2024-10-04-22-59-58--CEDARScript-Gemini-small/bowling/bowling.py"
@@ -150,7 +149,7 @@ def _update_command(self, cmd: UpdateCommand):
150149
src = read_file(file_path)
151150
lines = src.splitlines()
152151

153-
source_info: tuple[str, str | Sequence[str]] = (file_path, src)
152+
source_info: tuple[LiteralString | str | bytes, str | Sequence[str]] = (file_path, src)
154153

155154
def identifier_resolver(m: Marker):
156155
return self.find_identifier(source_info, m)
@@ -239,7 +238,7 @@ def _apply_action(action: EditingAction, lines: Sequence[str], range_spec: Range
239238
def _rm_command(self, cmd: RmFileCommand):
240239
file_path = os.path.join(self.root_path, cmd.file_path)
241240

242-
def _delete_function(self, cmd): # TODO
241+
def _delete_function(self, cmd): # TODO
243242
file_path = os.path.join(self.root_path, cmd.file_path)
244243

245244
# def _create_command(self, cmd: CreateCommand):
@@ -285,7 +284,9 @@ def find_index_range_for_region(self,
285284
return index_range
286285

287286

288-
def find_marker_or_segment(action: EditingAction, lines: Sequence[str], search_range: RangeSpec) -> tuple[Marker, RangeSpec]:
287+
def find_marker_or_segment(
288+
action: EditingAction, lines: Sequence[str], search_range: RangeSpec
289+
) -> tuple[Marker, RangeSpec]:
289290
marker: Marker | Segment | None = None
290291
match action:
291292
case MarkerCompatible() as marker_compatible:

src/text_manipulation/indentation_kit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def apply_relative_indents(self, content: str | Sequence[str], context_indent_co
215215
AssertionError: If the calculated indentation level for any line is negative.
216216
"""
217217
# TODO Always send str?
218-
lines = [line.lstrip() for line in content.splitlines() if line.strip()] if isinstance(content, str) else content
218+
lines = [l.lstrip() for l in content.splitlines() if l.strip()] if isinstance(content, str) else content
219219

220220
context_indent_level = self.char_count_to_level(context_indent_count)
221221
for i in range(len(lines)):

src/text_manipulation/range_spec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def from_line_marker(
7575
search_marker.value: The line to search for.
7676
search_marker.offset: The number of matches to skip before returning a result.
7777
0 skips no match and returns the first match, 1 returns the second match, and so on.
78-
:param search_range: The index to start the search from. Defaults to 0. The index to end the search at (exclusive).
78+
:param search_range: The index to start the search from and to end the search at (exclusive).
7979
Defaults to (0, -1), which means search to the end of the list.
8080
8181
:returns:

src/text_manipulation/text_editor_kit.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from collections.abc import Sequence
22
from typing import Protocol, runtime_checkable
3+
from os import PathLike
34

45
from cedarscript_ast_parser import Marker, RelativeMarker, RelativePositionType, Segment, MarkerType, BodyOrWhole
56
from .range_spec import IdentifierBoundaries, RangeSpec
67

78

8-
def read_file(file_path: str) -> str:
9+
def read_file(file_path: str | PathLike) -> str:
910
with open(file_path, 'r') as file:
1011
return file.read()
1112

1213

13-
def write_file(file_path: str, lines: Sequence[str]):
14+
def write_file(file_path: str | PathLike, lines: Sequence[str]):
1415
with open(file_path, 'w') as file:
1516
file.writelines([line + '\n' for line in lines])
1617

@@ -90,8 +91,13 @@ def segment_to_search_range(
9091
start_index_for_end_marker = start_match_result.as_index
9192
if start_relpos.qualifier == RelativePositionType.AFTER:
9293
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+
)
95101
if end_match_result.as_index > -1:
96102
one_after_end = end_match_result.as_index + 1
97103
end_match_result = RangeSpec(one_after_end, one_after_end, end_match_result.indent)

0 commit comments

Comments
 (0)