mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-15 06:16:18 +00:00
TextUtils: Allow insertion in new line after full text, adding missing newline at end of file
This commit is contained in:
1 parent
654b949bde
commit
6b8b103ded
1 file changed
+17
-2
@@ -20,6 +20,10 @@ from multilspy.multilspy_logger import MultilspyLogger
|
|||||||
from multilspy.multilspy_types import UnifiedSymbolInformation
|
from multilspy.multilspy_types import UnifiedSymbolInformation
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidTextLocationError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TextUtils:
|
class TextUtils:
|
||||||
"""
|
"""
|
||||||
Utilities for text operations.
|
Utilities for text operations.
|
||||||
@@ -49,7 +53,8 @@ class TextUtils:
|
|||||||
"""
|
"""
|
||||||
idx = 0
|
idx = 0
|
||||||
while line > 0:
|
while line > 0:
|
||||||
assert idx < len(text), (idx, len(text), text)
|
if idx >= len(text):
|
||||||
|
raise InvalidTextLocationError()
|
||||||
if text[idx] == "\n":
|
if text[idx] == "\n":
|
||||||
line -= 1
|
line -= 1
|
||||||
idx += 1
|
idx += 1
|
||||||
@@ -88,7 +93,17 @@ class TextUtils:
|
|||||||
Inserts the given text at the given line and column.
|
Inserts the given text at the given line and column.
|
||||||
Returns the modified text and the new line and column.
|
Returns the modified text and the new line and column.
|
||||||
"""
|
"""
|
||||||
change_index = TextUtils.get_index_from_line_col(text, line, col)
|
try:
|
||||||
|
change_index = TextUtils.get_index_from_line_col(text, line, col)
|
||||||
|
except InvalidTextLocationError as e:
|
||||||
|
num_lines_in_text = text.count("\n") + 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
|
||||||
|
change_index = len(text)
|
||||||
|
text_to_be_inserted = "\n" + text_to_be_inserted
|
||||||
|
else:
|
||||||
|
raise
|
||||||
new_text = text[:change_index] + text_to_be_inserted + text[change_index:]
|
new_text = text[:change_index] + text_to_be_inserted + text[change_index:]
|
||||||
new_l, new_c = TextUtils._get_updated_position_from_line_and_column_and_edit(line, col, text_to_be_inserted)
|
new_l, new_c = TextUtils._get_updated_position_from_line_and_column_and_edit(line, col, text_to_be_inserted)
|
||||||
return new_text, new_l, new_c
|
return new_text, new_l, new_c
|
||||||
|
|||||||
Reference in new issue
Block a user