From c80d76abe9f3619ad18e7e83e937f03da0cb3a4b Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Wed, 15 Jul 2026 18:45:18 +0200 Subject: [PATCH] Fix edge case in insert_text_at_position when inserting in line beyond actual content (insertion coordinates were not adjusted to true end coordinates) --- src/solidlsp/ls_utils.py | 5 ++++- test/solidlsp/test_text_utils.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/solidlsp/ls_utils.py b/src/solidlsp/ls_utils.py index 0f8ef140..774d3741 100644 --- a/src/solidlsp/ls_utils.py +++ b/src/solidlsp/ls_utils.py @@ -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:] diff --git a/test/solidlsp/test_text_utils.py b/test/solidlsp/test_text_utils.py index 4204f7e9..062125c4 100644 --- a/test/solidlsp/test_text_utils.py +++ b/test/solidlsp/test_text_utils.py @@ -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