Skip to content

Commit f38c836

Browse files
committed
Fixed bug in 'restrict_search_range'
1 parent f545579 commit f38c836

File tree

7 files changed

+4
-46
lines changed

7 files changed

+4
-46
lines changed

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -101,48 +101,6 @@ def _update_command(self, cmd: UpdateCommand):
101101
content = cmd.content or []
102102
file_path = os.path.join(self.root_path, target.file_path)
103103

104-
# Example 1:
105-
# UPDATE FILE "tmp.benchmarks/2024-10-04-22-59-58--CEDARScript-Gemini-small/bowling/bowling.py"
106-
# INSERT INSIDE FUNCTION "__init__" TOP
107-
# WITH CONTENT '''
108-
# @0:print("This line will be inserted at the top")
109-
# ''';
110-
# After parsing ->
111-
# UpdateCommand(
112-
# type='update',
113-
# target=SingleFileClause(file_path='tmp.benchmarks/2024-10-04-22-59-58--CEDARScript-Gemini-small/bowling
114-
# /bowling.py'),
115-
# action=InsertClause(insert_position=RelativeMarker(type=<MarkerType.FUNCTION: 'function'>,
116-
# value='__init__',
117-
# offset=None)),
118-
# content='\n @0:print("This line will be inserted at the top")\n '
119-
# )
120-
121-
# Example 2:
122-
# UPDATE FUNCTION
123-
# FROM FILE "tmp.benchmarks/2024-10-04-22-59-58--CEDARScript-Gemini-small/bowling/bowling.py"
124-
# WHERE NAME = "__init__"
125-
# REPLACE SEGMENT
126-
# STARTING AFTER LINE "def __init__(self):"
127-
# ENDING AFTER LINE "def __init__(self):"
128-
# WITH CONTENT '''
129-
# @0:print("This line will be inserted at the top")
130-
# ''';
131-
# After parsing ->
132-
# UpdateCommand(
133-
# type='update',
134-
# target=IdentifierFromFile(file_path='bowling.py',
135-
# where_clause=WhereClause(field='NAME', operator='=', value='__init__'),
136-
# identifier_type='FUNCTION', offset=None
137-
# ),
138-
# action=ReplaceClause(
139-
# region=Segment(
140-
# start=RelativeMarker(type=<MarkerType.LINE: 'line'>, value='def __init__(self):', offset=None),
141-
# end=RelativeMarker(type=<MarkerType.LINE: 'line'>, value='def __init__(self):', offset=None)
142-
# )),
143-
# content='\n @0:print("This line will be inserted at the top")\n '
144-
# )
145-
146104
src = read_file(file_path)
147105
lines = src.splitlines()
148106

@@ -348,7 +306,7 @@ def restrict_search_range(action, target, identifier_finder: IdentifierFinder) -
348306
case BodyOrWhole() | RelativePositionType():
349307
return identifier_boundaries.location_to_search_range(region)
350308
case Marker() as inner_marker:
351-
match identifier_finder(inner_marker):
309+
match identifier_finder(inner_marker, identifier_boundaries.whole):
352310
case IdentifierBoundaries() as inner_boundaries:
353311
return inner_boundaries.whole
354312
case RangeSpec() as inner_range_spec:

src/cedarscript_editor/tree_sitter_identifier_finder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
_log = logging.getLogger(__name__)
1313

14-
IdentifierFinder: TypeAlias = Callable[[Marker], IdentifierBoundaries | RangeSpec | None]
14+
IdentifierFinder: TypeAlias = Callable[[Marker | Segment, RangeSpec | None], IdentifierBoundaries | RangeSpec | None]
1515

1616

1717
def find_identifier(source_info: tuple[str, str | Sequence[str]], search_rage: RangeSpec = RangeSpec.EMPTY) -> IdentifierFinder:
@@ -37,7 +37,7 @@ def _select_finder(file_path: str, source: str, search_range: RangeSpec = RangeS
3737

3838
source = source.splitlines()
3939

40-
def find_by_marker(mos: Marker | Segment) -> IdentifierBoundaries | RangeSpec | None:
40+
def find_by_marker(mos: Marker | Segment, search_range: RangeSpec | None = None) -> IdentifierBoundaries | RangeSpec | None:
4141
match mos:
4242

4343
case Marker(MarkerType.LINE) | Segment():
File renamed without changes.
File renamed without changes.

tests/corpus/x.update.identifier.replace.line/chat.xml renamed to tests/corpus/update.identifier.replace.line/chat.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ UPDATE FUNCTION "fun2"
44
FROM FILE "1.py"
55
REPLACE LINE "def fun2(self, a, b, c):"
66
WITH CONTENT '''
7-
@0:def fun2(a, b, c)::
7+
µ@0:def fun2(a, b, c):
88
''';
99

1010
UPDATE FUNCTION "calculate"

tests/corpus/x.update.identifier.replace.line/expected.1.py renamed to tests/corpus/update.identifier.replace.line/expected.1.py

File renamed without changes.

tests/corpus/x.update.identifier.replace.line/expected.2.py renamed to tests/corpus/update.identifier.replace.line/expected.2.py

File renamed without changes.

0 commit comments

Comments
 (0)