Skip to content

Commit 1d16a11

Browse files
committed
optimize completion of arguments
1 parent 920ac1c commit 1d16a11

File tree

1 file changed

+59
-81
lines changed

1 file changed

+59
-81
lines changed

robotcode/language_server/robotframework/parts/completion.py

Lines changed: 59 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,71 +1139,64 @@ async def complete_arguments() -> Optional[List[CompletionItem]]:
11391139
if token_at_position.type not in [RobotToken.ARGUMENT, RobotToken.EOL, RobotToken.SEPARATOR]:
11401140
return None
11411141

1142+
if (
1143+
token_at_position.type == RobotToken.EOL
1144+
and len(tokens_at_position) > 1
1145+
and tokens_at_position[-2].type == RobotToken.KEYWORD
1146+
):
1147+
return None
1148+
11421149
token_at_position_index = kw_node.tokens.index(token_at_position)
11431150

11441151
argument_token_index = token_at_position_index
11451152
while argument_token_index >= 0 and kw_node.tokens[argument_token_index].type != RobotToken.ARGUMENT:
11461153
argument_token_index -= 1
11471154

1148-
arguments = kw_node.get_tokens(RobotToken.ARGUMENT)
1149-
1155+
argument_token: Optional[RobotToken] = None
11501156
if argument_token_index >= 0:
11511157
argument_token = kw_node.tokens[argument_token_index]
1152-
if argument_token.type == RobotToken.ARGUMENT:
1153-
argument_index = arguments.index(argument_token)
1154-
else:
1155-
argument_index = 0
1156-
else:
1157-
argument_index = -1
1158-
1159-
if whitespace_at_begin_of_token(token_at_position) > 1:
1160-
completion_range = range_from_token(token_at_position)
11611158

1162-
ws_b = whitespace_from_begin_of_token(token_at_position)
1163-
completion_range.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
1164-
1165-
if position.is_in_range(completion_range) or completion_range.end == position:
1166-
argument_index += 1
1167-
1168-
if argument_index < 0:
1169-
return None
1170-
1171-
completion_range = range_from_token(token_at_position)
1159+
completion_range = range_from_token(argument_token or token_at_position)
1160+
completion_range.end = range_from_token(token_at_position).end
11721161
if (w := whitespace_at_begin_of_token(token_at_position)) > 0:
1173-
if w > 1 and completion_range.start.character + 1 < position.character:
1162+
if w > 1 and range_from_token(token_at_position).start.character + 1 < position.character:
11741163
completion_range.start = position
1175-
elif completion_range.start == position:
1176-
pass
1177-
else:
1164+
elif completion_range.start != position:
11781165
return None
11791166
else:
1180-
if token_at_position.type != RobotToken.ARGUMENT:
1181-
return None
1182-
1183-
if "=" in token_at_position.value:
1184-
equal_index = token_at_position.value.index("=")
1167+
if "=" in (argument_token or token_at_position).value:
1168+
equal_index = (argument_token or token_at_position).value.index("=")
11851169
if completion_range.start.character + equal_index < position.character:
11861170
return None
11871171
else:
11881172
completion_range.end.character = completion_range.start.character + equal_index + 1
1173+
else:
1174+
completion_range.end = position
11891175

1190-
libdocs = [
1191-
entry.library_doc
1192-
for entry in (await self.namespace.get_libraries()).values()
1193-
if entry.import_name == import_node.name
1194-
and entry.args == import_node.args
1195-
and entry.alias == import_node.alias
1196-
]
1176+
imports_manger = await self.parent.documents_cache.get_imports_manager(self.document)
1177+
1178+
try:
1179+
libdoc = await imports_manger.get_libdoc_for_library_import(
1180+
import_node.name, (), str(self.document.uri.to_path().parent)
1181+
)
1182+
if not list:
1183+
return None
1184+
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
1185+
raise
1186+
except BaseException as e:
1187+
self._logger.exception(e)
1188+
return None
11971189

1198-
if len(libdocs) == 1:
1199-
init = next((v for v in libdocs[0].inits.values()), None)
1190+
if libdoc is not None:
1191+
init = next((v for v in libdoc.inits.values()), None)
12001192

12011193
if init:
12021194
return [
12031195
CompletionItem(
12041196
label=f"{e.name}=",
12051197
kind=CompletionItemKind.VARIABLE,
1206-
sort_text=f"010{i}_{e.name}=",
1198+
sort_text=f"010{i}_{e.name}",
1199+
filter_text=e.name,
12071200
insert_text_format=InsertTextFormat.PLAINTEXT,
12081201
text_edit=TextEdit(range=completion_range, new_text=f"{e.name}="),
12091202
data={
@@ -1237,7 +1230,7 @@ async def complete_with_name() -> Optional[List[CompletionItem]]:
12371230
return [
12381231
CompletionItem(
12391232
label="WITH NAME",
1240-
kind=CompletionItemKind.TEXT,
1233+
kind=CompletionItemKind.KEYWORD,
12411234
# detail=e.detail,
12421235
sort_text="03_WITH NAME",
12431236
insert_text_format=InsertTextFormat.PLAINTEXT,
@@ -1258,6 +1251,7 @@ async def complete_ResourceImport( # noqa: N802
12581251
position: Position,
12591252
context: Optional[CompletionContext],
12601253
) -> Union[List[CompletionItem], CompletionList, None]:
1254+
12611255
from robot.parsing.lexer.tokens import Token
12621256
from robot.parsing.model.statements import ResourceImport
12631257

@@ -1285,6 +1279,8 @@ async def complete_ResourceImport( # noqa: N802
12851279

12861280
if not position.is_in_range(r) and r.end != position:
12871281
return None
1282+
else:
1283+
return None
12881284
else:
12891285
return None
12901286

@@ -1368,6 +1364,10 @@ async def _complete_KeywordCall_or_Fixture( # noqa: N802
13681364

13691365
kw_node = cast(Statement, node)
13701366

1367+
keyword_token = kw_node.get_token(keyword_name_token_type)
1368+
if keyword_token is None:
1369+
return None
1370+
13711371
tokens_at_position = get_tokens_at_position(kw_node, position)
13721372

13731373
if not tokens_at_position:
@@ -1378,59 +1378,41 @@ async def _complete_KeywordCall_or_Fixture( # noqa: N802
13781378
if token_at_position.type not in [RobotToken.ARGUMENT, RobotToken.EOL, RobotToken.SEPARATOR]:
13791379
return None
13801380

1381+
if (
1382+
token_at_position.type == RobotToken.EOL
1383+
and len(tokens_at_position) > 1
1384+
and tokens_at_position[-2].type == RobotToken.KEYWORD
1385+
):
1386+
return None
1387+
13811388
token_at_position_index = kw_node.tokens.index(token_at_position)
13821389

13831390
argument_token_index = token_at_position_index
13841391
while argument_token_index >= 0 and kw_node.tokens[argument_token_index].type != RobotToken.ARGUMENT:
13851392
argument_token_index -= 1
13861393

1387-
arguments = kw_node.get_tokens(RobotToken.ARGUMENT)
1388-
1394+
argument_token: Optional[RobotToken] = None
13891395
if argument_token_index >= 0:
13901396
argument_token = kw_node.tokens[argument_token_index]
1391-
if argument_token.type == RobotToken.ARGUMENT:
1392-
argument_index = arguments.index(argument_token)
1393-
else:
1394-
argument_index = 0
1395-
else:
1396-
argument_index = -1
1397-
1398-
if whitespace_at_begin_of_token(token_at_position) > 1:
1399-
completion_range = range_from_token(token_at_position)
1400-
1401-
ws_b = whitespace_from_begin_of_token(token_at_position)
1402-
completion_range.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
1403-
1404-
if position.is_in_range(completion_range) or completion_range.end == position:
1405-
argument_index += 1
1406-
1407-
if argument_index < 0:
1408-
return None
14091397

14101398
result: Optional[Tuple[Optional[KeywordDoc], Token]]
14111399

1412-
keyword_token = kw_node.get_token(keyword_name_token_type)
1413-
if keyword_token is None:
1414-
return None
1415-
1416-
completion_range = range_from_token(token_at_position)
1400+
completion_range = range_from_token(argument_token or token_at_position)
1401+
completion_range.end = range_from_token(token_at_position).end
14171402
if (w := whitespace_at_begin_of_token(token_at_position)) > 0:
1418-
if w > 1 and completion_range.start.character + 1 < position.character:
1403+
if w > 1 and range_from_token(token_at_position).start.character + 1 < position.character:
14191404
completion_range.start = position
1420-
elif completion_range.start == position:
1421-
pass
1422-
else:
1405+
elif completion_range.start != position:
14231406
return None
14241407
else:
1425-
if token_at_position.type != RobotToken.ARGUMENT:
1426-
return None
1427-
1428-
if "=" in token_at_position.value:
1429-
equal_index = token_at_position.value.index("=")
1408+
if "=" in (argument_token or token_at_position).value:
1409+
equal_index = (argument_token or token_at_position).value.index("=")
14301410
if completion_range.start.character + equal_index < position.character:
14311411
return None
14321412
else:
14331413
completion_range.end.character = completion_range.start.character + equal_index + 1
1414+
else:
1415+
completion_range.end = position
14341416

14351417
result = await self.get_keyworddoc_and_token_from_position(
14361418
keyword_token.value,
@@ -1460,15 +1442,11 @@ async def _complete_KeywordCall_or_Fixture( # noqa: N802
14601442
CompletionItem(
14611443
label=f"{e.name}=",
14621444
kind=CompletionItemKind.VARIABLE,
1463-
# detail=e.detail,
1445+
detail="Argument",
1446+
filter_text=e.name,
14641447
sort_text=f"02{i}_{e.name}=",
14651448
insert_text_format=InsertTextFormat.PLAINTEXT,
14661449
text_edit=TextEdit(range=completion_range, new_text=f"{e.name}="),
1467-
data={
1468-
"document_uri": str(self.document.uri),
1469-
"type": "Argument",
1470-
"name": e,
1471-
},
14721450
)
14731451
for i, e in enumerate(result[0].args)
14741452
if e.kind

0 commit comments

Comments
 (0)