Skip to content

Commit 12af94d

Browse files
committed
fix(langserver): correct "Create keyword" quick fix to ignore empty lines when inserting text
1 parent 70c2350 commit 12af94d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

packages/language_server/src/robotcode/language_server/robotframework/parts/code_action_quick_fixes.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
[Arguments] ${args}
5050
# TODO: implement keyword "${name}".
5151
Fail Not Implemented
52-
5352
"""
5453
)
5554

@@ -58,7 +57,6 @@
5857
${name}
5958
# TODO: implement keyword "${name}".
6059
Fail Not Implemented
61-
6260
"""
6361
)
6462

@@ -265,13 +263,13 @@ async def _apply_create_keyword(self, document: TextDocument, insert_text: str)
265263
keyword_sections = find_keyword_sections(model)
266264
keyword_section = keyword_sections[-1] if keyword_sections else None
267265

268-
if keyword_section is not None:
269-
node_range = range_from_node(keyword_section)
266+
lines = document.get_lines()
270267

268+
if keyword_section is not None:
269+
node_range = range_from_node(keyword_section, skip_non_data=True, allow_comments=True)
271270
insert_pos = Position(node_range.end.line + 1, 0)
272271
insert_range = Range(insert_pos, insert_pos)
273-
274-
insert_text = f"\n{insert_text}"
272+
insert_text = f"\n\n{insert_text}"
275273
else:
276274
if namespace.languages is None or not namespace.languages.languages:
277275
keywords_text = "Keywords"
@@ -280,14 +278,27 @@ async def _apply_create_keyword(self, document: TextDocument, insert_text: str)
280278

281279
insert_text = f"\n\n*** {keywords_text} ***\n{insert_text}"
282280

283-
lines = document.get_lines()
284281
end_line = len(lines) - 1
285282
while end_line >= 0 and not lines[end_line].strip():
286283
end_line -= 1
287284
doc_pos = Position(end_line + 1, 0)
288285

289286
insert_range = Range(doc_pos, doc_pos)
290287

288+
if insert_range.start.line >= len(lines) and lines[-1].strip():
289+
doc_pos = Position(len(lines) - 1, len(lines[-1]))
290+
insert_range = Range(doc_pos, doc_pos)
291+
insert_text = "\n" + insert_text
292+
293+
if insert_range.start.line <= len(lines) and lines[insert_range.start.line].startswith("*"):
294+
insert_text = insert_text + "\n\n"
295+
if (
296+
insert_range.start.line + 1 <= len(lines)
297+
and not lines[insert_range.start.line].strip()
298+
and lines[insert_range.start.line + 1].startswith("*")
299+
):
300+
insert_text = insert_text + "\n"
301+
291302
we = WorkspaceEdit(
292303
document_changes=[
293304
TextDocumentEdit(

0 commit comments

Comments
 (0)