Fix edge case in insert_text_at_position when inserting in line beyond actual content

(insertion coordinates were not adjusted to true end coordinates)
This commit is contained in:
Dominik Jain authored and Dominik Jain committed 2026-07-15 19:29:16 +02:00
1 parent d4e99dc003
commit c80d76abe9
2 files changed
+20 -1

No files matched your search

+4 -1
View File
@@ -263,9 +263,12 @@ class TextUtils:
num_lines_in_text = text_stepper.line + 1
max_line = num_lines_in_text - 1
if line == max_line + 1 and col == 0: # trying to insert at new line after full text
# insert at end, adding missing newline
# insert at end, adding missing newline and adjusting insertion position
# to the actual end coordinates of the text
change_index = len(text)
text_to_be_inserted = "\n" + text_to_be_inserted
line = text_stepper.line
col = text_stepper.col
else:
raise
new_text = text[:change_index] + text_to_be_inserted + text[change_index:]
+16
View File
@@ -32,3 +32,19 @@ class TestTextUtils:
assert TextUtils.get_index_from_line_col(self.TEXT, 0, 1) == 1
assert TextUtils.get_index_from_line_col(self.TEXT, 1, 1) == 3 + 1 + 1
assert TextUtils.get_index_from_line_col(self.TEXT, 2, 1) == 3 + 1 + 3 + 2 + 1
def test_insert_text_at_index(self):
insertion = "XXX"
new_text, l, c = TextUtils.insert_text_at_position(self.TEXT, 0, 1, insertion)
assert (l, c) == (0, 1 + len(insertion))
assert new_text.startswith("0XXX12")
def test_insert_text_in_next_line_beyond_content(self):
"""
Test inserting text at a line index 1 beyond the actual number of lines.
This case is specifically handled as an edge case in the implementation.
"""
insertion = "XXX"
new_text, l, c = TextUtils.insert_text_at_position(self.TEXT, 4, 0, insertion)
assert (l, c) == (4, len(insertion))
assert new_text == self.TEXT + "\n" + insertion