Skip to content

Commit ac518f0

Browse files
committed
correct completion and signature help for library imports with "WITH NAME"
1 parent e8b0273 commit ac518f0

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

robotcode/language_server/common/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def zero() -> "Range":
856856
),
857857
)
858858

859-
def extended(self, start_line: int, start_character: int, end_line: int, end_character: int) -> Range:
859+
def extend(self, start_line: int = 0, start_character: int = 0, end_line: int = 0, end_character: int = 0) -> Range:
860860
return Range(
861861
start=Position(line=self.start.line + start_line, character=self.start.character + start_character),
862862
end=Position(line=self.end.line + end_line, character=self.end.character + end_character),

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,7 @@ def get_test_library(
886886

887887
# skip antigravity easter egg
888888
# see https://python-history.blogspot.com/2010/06/import-antigravity.html
889-
if import_name.lower() in ["antigravity"] or import_name.lower().endswith(
890-
("lib/antigravity.py", f"lib{os.sep}antigravity.py")
891-
):
889+
if import_name.lower() in ["antigravity"] or import_name.lower().endswith("antigravity.py"):
892890
raise IgnoreEasterEggLibraryWarning(f"Ignoring import for python easter egg '{import_name}'.")
893891

894892
errors: List[Error] = []

robotcode/language_server/robotframework/parts/completion.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,9 @@ async def complete_LibraryImport( # noqa: N802
868868
return []
869869
import_token_index = import_node.tokens.index(import_token)
870870

871-
async def complete_import() -> Union[List[CompletionItem], CompletionList, None]:
871+
async def complete_import() -> Optional[List[CompletionItem]]:
872872
if self.document is None:
873-
return []
873+
return None
874874

875875
if len(import_node.tokens) > import_token_index + 2:
876876
name_token = import_node.tokens[import_token_index + 2]
@@ -887,6 +887,9 @@ async def complete_import() -> Union[List[CompletionItem], CompletionList, None]
887887

888888
if not position.is_in_range(r) and r.end != position:
889889
return None
890+
else:
891+
return None
892+
890893
else:
891894
return None
892895

@@ -954,9 +957,19 @@ async def complete_import() -> Union[List[CompletionItem], CompletionList, None]
954957
for e in list
955958
]
956959

957-
async def complete_arguments() -> Union[List[CompletionItem], CompletionList, None]:
960+
async def complete_arguments() -> Optional[List[CompletionItem]]:
958961
if self.document is None:
959-
return []
962+
return None
963+
964+
if (
965+
import_node.name is None
966+
or position <= range_from_token(import_node.get_token(RobotToken.NAME)).extend(end_character=1).end
967+
):
968+
return None
969+
970+
with_name_token = next((v for v in import_node.tokens if v.value == "WITH NAME"), None)
971+
if with_name_token is not None and position >= range_from_token(with_name_token).start:
972+
return None
960973

961974
if context is None or context.trigger_kind != CompletionTriggerKind.INVOKED:
962975
return []
@@ -1063,9 +1076,31 @@ async def complete_arguments() -> Union[List[CompletionItem], CompletionList, No
10631076

10641077
return None
10651078

1066-
result = await complete_import()
1067-
if not result:
1068-
result = await complete_arguments()
1079+
async def complete_with_name() -> Optional[List[CompletionItem]]:
1080+
if self.document is None:
1081+
return None
1082+
1083+
if context is None or context.trigger_kind != CompletionTriggerKind.INVOKED:
1084+
return []
1085+
1086+
if import_node.name and not any(v for v in import_node.tokens if v.value == "WITH NAME"):
1087+
name_token = import_node.get_token(RobotToken.NAME)
1088+
if position >= range_from_token(name_token).extend(end_character=2).end:
1089+
return [
1090+
CompletionItem(
1091+
label="WITH NAME",
1092+
kind=CompletionItemKind.TEXT,
1093+
# detail=e.detail,
1094+
sort_text="03_WITH NAME",
1095+
insert_text_format=InsertTextFormat.PLAINTEXT,
1096+
)
1097+
]
1098+
return []
1099+
1100+
result = await complete_import() or []
1101+
result.extend(await complete_arguments() or [])
1102+
result.extend(await complete_with_name() or [])
1103+
10691104
return result
10701105

10711106
async def complete_ResourceImport( # noqa: N802

robotcode/language_server/robotframework/parts/signature_help.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,14 @@ async def signature_help_LibraryImport( # noqa: N802
215215

216216
library_node = cast(LibraryImport, node)
217217

218-
if library_node.name is None:
218+
if (
219+
library_node.name is None
220+
or position <= range_from_token(library_node.get_token(RobotToken.NAME)).extend(end_character=1).end
221+
):
222+
return None
223+
224+
with_name_token = next((v for v in library_node.tokens if v.value == "WITH NAME"), None)
225+
if with_name_token is not None and position >= range_from_token(with_name_token).start:
219226
return None
220227

221228
imports_manager = await self.parent.documents_cache.get_imports_manager(document)

0 commit comments

Comments
 (0)