Skip to content

Commit a173b72

Browse files
committed
fix: Fix bug in RangeSpec when search term offset exceeds match count
1 parent 010667a commit a173b72

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _update_command(self, cmd: UpdateCommand):
119119
# action.insertclause.insert_position=FUNCTION(score)
120120
# target.as_marker = FUNCTION(roll) (the one to delete)
121121
search_range = RangeSpec.EMPTY
122-
move_src_range = restrict_search_range(action, target, identifier_finder)
122+
move_src_range = restrict_search_range(action, target, identifier_finder, lines)
123123
case _:
124124
move_src_range = None
125125
# Set range_spec to cover the identifier

src/text_manipulation/range_spec.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def from_line_marker(
187187
matches['partial'].append((i, reference_indent))
188188

189189
offset = search_term.offset or 0
190+
max_match_count = max([len(m) for m in matches.values()])
190191
for match_type in MATCH_TYPES:
191192
match_type_count = len(matches[match_type])
192193
if search_term.offset is None and match_type_count > 1:
@@ -201,9 +202,10 @@ def from_line_marker(
201202
# `LINE '{search_term.value.strip()}' OFFSET {match_type_count - 1}`"
202203
# ' (See `offset_clause` in `<grammar.js>` for details on OFFSET)'
203204
)
204-
if match_type_count and (search_term.offset or 0) >= match_type_count:
205+
206+
if match_type_count and offset >= max_match_count:
205207
raise ValueError(
206-
f"There are only {match_type_count} lines matching `{search_term.value}`, "
208+
f"There are only {max_match_count} lines matching `{search_term.value}`, "
207209
f"but 'OFFSET' was set to {search_term.offset} (you can skip at most {match_type_count-1} of those)"
208210
)
209211

0 commit comments

Comments
 (0)