Skip to content

Commit 6427dac

Browse files
committed
references for arguments also finds named arguments
1 parent 99a2dc3 commit 6427dac

File tree

50 files changed

+359
-303
lines changed

Some content is hidden

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

50 files changed

+359
-303
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to the "robotcode" extension will be documented in this file
88

99
- Information about possible circular imports
1010
- if one resource file imports another resource file and vice versa an information message is shown in source code and problems list
11+
- References for arguments also finds named arguments
1112

1213
## 0.11.16
1314

robotcode/language_server/robotframework/diagnostics/entities.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from dataclasses import dataclass, field
22
from enum import Enum
3-
from typing import Any, Optional, Tuple
3+
from typing import TYPE_CHECKING, Any, Optional, Tuple
44

55
from ...common.lsp_types import Position, Range
66
from ..utils.ast_utils import Token
77

8+
if TYPE_CHECKING:
9+
from .library_doc import KeywordDoc
10+
811

912
@dataclass
1013
class SourceEntity:
@@ -195,6 +198,7 @@ def __hash__(self) -> int:
195198
@dataclass
196199
class ArgumentDefinition(VariableDefinition):
197200
type: VariableDefinitionType = VariableDefinitionType.ARGUMENT
201+
keyword_doc: Optional["KeywordDoc"] = None
198202

199203
def __hash__(self) -> int:
200204
return hash((type(self), self.name, self.type))

robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,17 @@ async def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
143143

144144

145145
class BlockVariableVisitor(AsyncVisitor):
146-
def __init__(self, source: str, position: Optional[Position] = None, in_args: bool = True) -> None:
146+
def __init__(
147+
self, namespace: Namespace, source: str, position: Optional[Position] = None, in_args: bool = True
148+
) -> None:
147149
super().__init__()
148-
150+
self.namespace = namespace
149151
self.source = source
150152
self.position = position
151153
self.in_args = in_args
152154

153155
self._results: Dict[str, VariableDefinition] = {}
156+
self.current_kw_doc: Optional[KeywordDoc] = None
154157

155158
async def get(self, model: ast.AST) -> List[VariableDefinition]:
156159

@@ -164,15 +167,26 @@ async def visit(self, node: ast.AST) -> None:
164167
if self.position is None or self.position >= range_from_node(node).start:
165168
return await super().visit(node)
166169

170+
async def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
171+
try:
172+
await self.generic_visit(node)
173+
finally:
174+
self.current_kw_doc = None
175+
167176
async def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
168177
from robot.parsing.lexer.tokens import Token as RobotToken
169178
from robot.parsing.model.statements import KeywordName
170179
from robot.variables.search import VariableSearcher
171180

181+
from ..parts.model_helper import ModelHelperMixin
182+
172183
n = cast(KeywordName, node)
173184
name_token = cast(Token, n.get_token(RobotToken.KEYWORD_NAME))
174185

175186
if name_token is not None and name_token.value:
187+
keyword = await ModelHelperMixin.get_keyword_definition_at_token(self.namespace, name_token)
188+
self.current_kw_doc = keyword
189+
176190
for variable_token in filter(
177191
lambda e: e.type == RobotToken.VARIABLE,
178192
tokenize_variables(name_token, identifiers="$", ignore_errors=True),
@@ -192,6 +206,7 @@ async def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
192206
end_line_no=variable_token.lineno,
193207
end_col_offset=variable_token.end_col_offset,
194208
source=self.source,
209+
keyword_doc=self.current_kw_doc,
195210
)
196211

197212
def get_variable_token(self, token: Token) -> Optional[Token]:
@@ -240,6 +255,7 @@ async def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
240255
end_line_no=argument.lineno,
241256
end_col_offset=argument.end_col_offset,
242257
source=self.source,
258+
keyword_doc=self.current_kw_doc,
243259
)
244260

245261
except VariableError:
@@ -816,7 +832,10 @@ async def yield_variables(
816832
*[
817833
(
818834
await BlockVariableVisitor(
819-
self.source, position, isinstance(test_or_keyword_nodes[-1], Arguments) if nodes else False
835+
self,
836+
self.source,
837+
position,
838+
isinstance(test_or_keyword_nodes[-1], Arguments) if nodes else False,
820839
).get(test_or_keyword)
821840
)
822841
if test_or_keyword is not None

robotcode/language_server/robotframework/parts/references.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ async def find_variable_references(
225225
) -> List[Location]:
226226
return (
227227
await create_sub_task(self.find_variable_references_in_file(document, variable, include_declaration))
228-
if isinstance(variable, (ArgumentDefinition, LocalVariableDefinition))
228+
if isinstance(variable, (LocalVariableDefinition))
229229
else await self._find_references_in_workspace(
230230
document, self.find_variable_references_in_file, variable, include_declaration
231231
)
@@ -266,6 +266,8 @@ async def find_variable_references_in_file(
266266
) -> List[Location]:
267267
from robot.parsing.lexer.tokens import Token as RobotToken
268268
from robot.parsing.model.blocks import Block, Keyword, Section, TestCase
269+
from robot.parsing.model.statements import Fixture, KeywordCall
270+
from robot.utils.escaping import split_from_equals
269271

270272
namespace = await self.parent.documents_cache.get_namespace(doc)
271273
model = await self.parent.documents_cache.get_model(doc)
@@ -334,6 +336,26 @@ async def find_variable_references_in_file(
334336
if found_variable == variable:
335337
result.append(Location(str(doc.uri), range_from_token(sub_token)))
336338

339+
if (
340+
isinstance(variable, ArgumentDefinition)
341+
and isinstance(node, (KeywordCall, Fixture))
342+
and variable.name_token
343+
):
344+
kw_token = cast(
345+
Token,
346+
node.get_token(RobotToken.KEYWORD)
347+
if isinstance(node, KeywordCall)
348+
else node.get_token(RobotToken.NAME),
349+
)
350+
if kw_token and kw_token.value:
351+
kw = await namespace.find_keyword(kw_token.value)
352+
if kw is not None and kw == variable.keyword_doc:
353+
for arg in node.get_tokens(RobotToken.ARGUMENT):
354+
name, value = split_from_equals(arg.value)
355+
if value is not None and name == variable.name_token.value:
356+
name_token = RobotToken(RobotToken.ARGUMENT, name, arg.lineno, arg.col_offset)
357+
result.append(Location(str(doc.uri), range_from_token(name_token)))
358+
337359
return result
338360

339361
async def references_KeywordName( # noqa: N802

tests/robotcode/language_server/robotframework/parts/data/.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
},
1515
"robotcode.languageServer.args": [
16-
// "--debugpy",
16+
"--debugpy",
1717
// "--log",
1818
// "--log-level",
1919
// "TRACE",
@@ -30,5 +30,5 @@
3030
// "robotcode.debug.outputMessages": true
3131
//"python.analysis.diagnosticMode": "workspace"
3232
//"robotcode.robot.paths": ["./tests", "./tests1"]
33-
"robotcode.analysis.diagnosticMode": "workspace"
33+
"robotcode.analysis.diagnosticMode": "openFilesOnly"
3434
}

tests/robotcode/language_server/robotframework/parts/data/resources/firstresource.resource

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ some failing keyword
1313
Log hello from failing
1414
Fail ho
1515
Log end failing
16+
17+
18+
a keyword with args
19+
[Arguments] ${a} ${a long name}= ${a_short_name}=default
20+
No Operation

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fifth
6363
a keyword with params
6464
another keyword with params 1
6565
again a keyword with params 1
66+
a keyword with args a=2 a long name=99 a_short_name=342
6667

6768
*** Keywords ***
6869
a keyword with params

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ templated with embedded not defined
152152
environmentvars
153153
log ${%{TESTENV}.server.ip} port=${%{TESTENV}.server.port} # TODO
154154

155+
named arguments
156+
a keyword with loop aaa=hello
157+
158+
155159
*** Keywords ***
156160
do something
157161
Log hello from do something

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: 68
65+
line: 69
6666
start:
6767
character: 30
68-
line: 68
68+
line: 69
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 44
74-
line: 77
74+
line: 78
7575
start:
7676
character: 39
77-
line: 77
77+
line: 78

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: 68
65+
line: 69
6666
start:
6767
character: 30
68-
line: 68
68+
line: 69
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 44
74-
line: 77
74+
line: 78
7575
start:
7676
character: 39
77-
line: 77
77+
line: 78

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: 68
65+
line: 69
6666
start:
6767
character: 30
68-
line: 68
68+
line: 69
6969
- !DocumentHighlight
7070
kind: !DocumentHighlightKind 'TEXT'
7171
range:
7272
end:
7373
character: 44
74-
line: 77
74+
line: 78
7575
start:
7676
character: 39
77-
line: 77
77+
line: 78

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

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

0 commit comments

Comments
 (0)