Skip to content

Commit 4291f7a

Browse files
committed
Improve handling of completion of argument definitions
1 parent ac0ac27 commit 4291f7a

File tree

168 files changed

+1952
-1646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+1952
-1646
lines changed

CHANGELOG.md

Lines changed: 422 additions & 420 deletions
Large diffs are not rendered by default.

robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ async def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
212212
from robot.parsing.lexer.tokens import Token as RobotToken
213213
from robot.parsing.model.statements import Arguments
214214

215+
args: List[str] = []
215216
n = cast(Arguments, node)
216217
arguments = n.get_tokens(RobotToken.ARGUMENT)
217218
for argument_token in (cast(RobotToken, e) for e in arguments):
@@ -225,17 +226,19 @@ async def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
225226
and self.position in range_from_token(argument_token)
226227
and self.position > range_from_token(argument).end
227228
):
228-
continue
229-
230-
self._results[argument.value] = ArgumentDefinition(
231-
name=argument.value,
232-
name_token=strip_variable_token(argument),
233-
line_no=argument.lineno,
234-
col_offset=argument.col_offset,
235-
end_line_no=argument.lineno,
236-
end_col_offset=argument.end_col_offset,
237-
source=self.source,
238-
)
229+
break
230+
231+
if argument.value not in args:
232+
args.append(argument.value)
233+
self._results[argument.value] = ArgumentDefinition(
234+
name=argument.value,
235+
name_token=strip_variable_token(argument),
236+
line_no=argument.lineno,
237+
col_offset=argument.col_offset,
238+
end_line_no=argument.lineno,
239+
end_col_offset=argument.end_col_offset,
240+
source=self.source,
241+
)
239242

240243
except VariableError:
241244
pass

robotcode/language_server/robotframework/parts/completion.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
get_nodes_at_position,
5555
get_tokens_at_position,
5656
range_from_token,
57+
tokenize_variables,
5758
whitespace_at_begin_of_token,
5859
whitespace_from_begin_of_token,
5960
)
@@ -410,7 +411,7 @@ async def create_variables_completion_items(
410411
),
411412
filter_text=s.name[2:-1] if range is not None else None,
412413
)
413-
for s in (await self.namespace.get_variable_matchers(nodes, position)).values()
414+
for s in (await self.namespace.get_variable_matchers(list(reversed(nodes)), position)).values()
414415
if s.name is not None and (s.name_token is None or not position.is_in_range(range_from_token(s.name_token)))
415416
]
416417

@@ -649,6 +650,21 @@ def enumerate_indexes(s: str, c: str) -> Iterator[int]:
649650

650651
return result
651652

653+
def get_variable_token(self, token: Token) -> Optional[Token]:
654+
from robot.parsing.lexer.tokens import Token as RobotToken
655+
656+
return next(
657+
(
658+
v
659+
for v in itertools.dropwhile(
660+
lambda t: t.type in RobotToken.NON_DATA_TOKENS,
661+
tokenize_variables(token, ignore_errors=True),
662+
)
663+
if v.type == RobotToken.VARIABLE
664+
),
665+
None,
666+
)
667+
652668
async def complete_default(
653669
self,
654670
nodes_at_position: List[ast.AST],
@@ -683,15 +699,16 @@ async def complete_default(
683699
elif position.character == 0:
684700
return await self.create_section_completion_items(None)
685701

686-
if (
687-
len(nodes_at_position) > 1
688-
and isinstance(nodes_at_position[0], Statement)
689-
and not isinstance(nodes_at_position[0], Arguments)
690-
):
702+
if len(nodes_at_position) > 1 and isinstance(nodes_at_position[0], HasTokens):
691703
node = nodes_at_position[0]
692704
tokens_at_position = get_tokens_at_position(node, position)
693705
token_at_position = tokens_at_position[-1]
694706

707+
if isinstance(node, Arguments):
708+
arg = self.get_variable_token(token_at_position)
709+
if arg is not None and position <= range_from_token(arg).end:
710+
return None
711+
695712
token_at_position_index = tokens_at_position.index(token_at_position)
696713
while token_at_position.type in [RobotToken.EOL]:
697714
token_at_position_index -= 1

tests/robotcode/language_server/robotframework/parts/data/tests/document_highlight.robot

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ forth
5959
Should Be Equal ${result} ${LIB_ARG}
6060

6161

62+
fifth
63+
a keyword with params
64+
another keyword with params 1
65+
again a keyword with params 1
66+
6267
*** Keywords ***
6368
a keyword with params
6469
[Arguments] ${A VAR}=${A VAR}
@@ -80,11 +85,13 @@ another keyword with params
8085
# ^^^^^ argument usage
8186

8287
again a keyword with params
83-
[Arguments] ${a} ${b}=${a}
88+
[Arguments] ${a} ${b}=${a} ${c}=${b}
8489
# ^ an argument
8590
# ^ another argument
8691
# ^ argument usage in argument
8792
Log ${a}
8893
# ^ argument usage
8994
Log ${b}
95+
# ^ argument usage
96+
Log ${c}
9097
# ^ argument usage

tests/robotcode/language_server/robotframework/parts/data/tests/goto.robot

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ a templated Test
9292
hello
9393
world
9494

95+
third
96+
just another keyword with params
9597

9698
*** Keywords ***
9799
a simple keyword
@@ -119,13 +121,15 @@ another keyword with params
119121
# ^^^^^ argument usage
120122

121123
just another keyword with params
122-
[Arguments] ${tt} ${A VAR}=${tt} ${tt}=${A VAR}
124+
[Arguments] ${tt} ${A VAR1}=${tt} ${tt}=${A VAR1}
123125
# ^^ an argument
124-
# ^^^^^ another argument
125-
# ^^^^^ a default value
126+
# ^^^^^^ another argument
127+
# ^^ a default value
128+
# ^^ an overridden argument
129+
# ^^^^^ a default value from overriden argument
126130
Log ${tt}
127131
# ^^ argument usage
128-
Log ${A VAR}
132+
Log ${A VAR1}
129133
# ^^^^^ argument usage
130134

131135

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_018_002_simple_variable_.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ result:
6262
range:
6363
end:
6464
character: 35
65-
line: 63
65+
line: 68
6666
start:
6767
character: 30
68-
line: 63
68+
line: 68
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 44
74-
line: 72
74+
line: 77
7575
start:
7676
character: 39
77-
line: 72
77+
line: 77

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_018_004_simple_variable_.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ result:
6262
range:
6363
end:
6464
character: 35
65-
line: 63
65+
line: 68
6666
start:
6767
character: 30
68-
line: 63
68+
line: 68
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 44
74-
line: 72
74+
line: 77
7575
start:
7676
character: 39
77-
line: 72
77+
line: 77

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_018_006_simple_variable_.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ result:
6262
range:
6363
end:
6464
character: 35
65-
line: 63
65+
line: 68
6666
start:
6767
character: 30
68-
line: 63
68+
line: 68
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 44
74-
line: 72
74+
line: 77
7575
start:
7676
character: 39
77-
line: 72
77+
line: 77

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_030_004_simple_keyword_call_.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,61 @@ result:
2626
range:
2727
end:
2828
character: 7
29-
line: 66
29+
line: 71
3030
start:
3131
character: 4
32-
line: 66
32+
line: 71
3333
- !DocumentHighlight
3434
kind: !DocumentHighlightKind 'TEXT'
3535
range:
3636
end:
3737
character: 7
38-
line: 68
38+
line: 73
3939
start:
4040
character: 4
41-
line: 68
41+
line: 73
4242
- !DocumentHighlight
4343
kind: !DocumentHighlightKind 'TEXT'
4444
range:
4545
end:
4646
character: 7
47-
line: 76
47+
line: 81
4848
start:
4949
character: 4
50-
line: 76
50+
line: 81
5151
- !DocumentHighlight
5252
kind: !DocumentHighlightKind 'TEXT'
5353
range:
5454
end:
5555
character: 7
56-
line: 78
56+
line: 83
5757
start:
5858
character: 4
59-
line: 78
59+
line: 83
6060
- !DocumentHighlight
6161
kind: !DocumentHighlightKind 'TEXT'
6262
range:
6363
end:
6464
character: 7
65-
line: 86
65+
line: 91
6666
start:
6767
character: 4
68-
line: 86
68+
line: 91
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 7
74-
line: 88
74+
line: 93
7575
start:
7676
character: 4
77-
line: 88
77+
line: 93
78+
- !DocumentHighlight
79+
kind: !DocumentHighlightKind 'TEXT'
80+
range:
81+
end:
82+
character: 7
83+
line: 95
84+
start:
85+
character: 4
86+
line: 95

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_030_005_simple_keyword_call_.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,61 @@ result:
2626
range:
2727
end:
2828
character: 7
29-
line: 66
29+
line: 71
3030
start:
3131
character: 4
32-
line: 66
32+
line: 71
3333
- !DocumentHighlight
3434
kind: !DocumentHighlightKind 'TEXT'
3535
range:
3636
end:
3737
character: 7
38-
line: 68
38+
line: 73
3939
start:
4040
character: 4
41-
line: 68
41+
line: 73
4242
- !DocumentHighlight
4343
kind: !DocumentHighlightKind 'TEXT'
4444
range:
4545
end:
4646
character: 7
47-
line: 76
47+
line: 81
4848
start:
4949
character: 4
50-
line: 76
50+
line: 81
5151
- !DocumentHighlight
5252
kind: !DocumentHighlightKind 'TEXT'
5353
range:
5454
end:
5555
character: 7
56-
line: 78
56+
line: 83
5757
start:
5858
character: 4
59-
line: 78
59+
line: 83
6060
- !DocumentHighlight
6161
kind: !DocumentHighlightKind 'TEXT'
6262
range:
6363
end:
6464
character: 7
65-
line: 86
65+
line: 91
6666
start:
6767
character: 4
68-
line: 86
68+
line: 91
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 7
74-
line: 88
74+
line: 93
7575
start:
7676
character: 4
77-
line: 88
77+
line: 93
78+
- !DocumentHighlight
79+
kind: !DocumentHighlightKind 'TEXT'
80+
range:
81+
end:
82+
character: 7
83+
line: 95
84+
start:
85+
character: 4
86+
line: 95

0 commit comments

Comments
 (0)