Skip to content

Commit 54809e6

Browse files
committed
correct find keywords in templates
1 parent 3d095d3 commit 54809e6

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ async def __remove_filewatcher(workspace: Workspace, entry: FileWatcherEntry) ->
248248

249249
def __del__(self) -> None:
250250
try:
251-
if self.file_watchers is not None:
251+
if asyncio.get_running_loop() and self.file_watchers is not None:
252252
for watcher in self.file_watchers:
253253
create_sub_task(
254254
_ResourcesEntry.__remove_filewatcher(self.parent.parent_protocol.workspace, watcher)

robotcode/language_server/robotframework/parts/references.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,16 @@ async def references_KeywordName( # noqa: N802
165165
if not name_token:
166166
return None
167167

168-
result = await namespace.find_keyword(name_token.value)
168+
keyword = await namespace.find_keyword(name_token.value)
169169

170-
if result is not None and result.source and not result.is_error_handler:
170+
if keyword is not None and keyword.source and not keyword.is_error_handler:
171171
return [
172172
*(
173-
[Location(uri=str(Uri.from_path(result.source)), range=range_from_token_or_node(node, name_token))]
173+
[Location(uri=str(Uri.from_path(keyword.source)), range=range_from_token_or_node(node, name_token))]
174174
if context.include_declaration
175175
else []
176176
),
177-
*await self._find_keyword_references(document, result),
177+
*await self._find_keyword_references(document, keyword),
178178
]
179179

180180
return None
@@ -190,24 +190,24 @@ async def references_KeywordCall( # noqa: N802
190190
return None
191191

192192
kw_node = cast(KeywordCall, node)
193-
result = await self.get_keyworddoc_and_token_from_position(
193+
keyword = await self.get_keyworddoc_and_token_from_position(
194194
kw_node.keyword,
195195
cast(Token, kw_node.get_token(RobotToken.KEYWORD)),
196196
[cast(Token, t) for t in kw_node.get_tokens(RobotToken.ARGUMENT)],
197197
namespace,
198198
position,
199199
)
200200

201-
if result is not None and result[0] is not None:
202-
source = result[0].source
201+
if keyword is not None and keyword[0] is not None:
202+
source = keyword[0].source
203203
if source is not None:
204204
return [
205205
*(
206-
[Location(uri=str(Uri.from_path(source)), range=result[0].range)]
206+
[Location(uri=str(Uri.from_path(source)), range=keyword[0].range)]
207207
if context.include_declaration
208208
else []
209209
),
210-
*await self._find_keyword_references(document, result[0]),
210+
*await self._find_keyword_references(document, keyword[0]),
211211
]
212212

213213
return None
@@ -223,24 +223,24 @@ async def references_Fixture( # noqa: N802
223223
return None
224224

225225
fixture_node = cast(Fixture, node)
226-
result = await self.get_keyworddoc_and_token_from_position(
226+
keyword = await self.get_keyworddoc_and_token_from_position(
227227
fixture_node.name,
228228
cast(Token, fixture_node.get_token(RobotToken.NAME)),
229229
[cast(Token, t) for t in fixture_node.get_tokens(RobotToken.ARGUMENT)],
230230
namespace,
231231
position,
232232
)
233233

234-
if result is not None and result[0] is not None:
235-
source = result[0].source
234+
if keyword is not None and keyword[0] is not None:
235+
source = keyword[0].source
236236
if source is not None:
237237
return [
238238
*(
239-
[Location(uri=str(Uri.from_path(source)), range=result[0].range)]
239+
[Location(uri=str(Uri.from_path(source)), range=keyword[0].range)]
240240
if context.include_declaration
241241
else []
242242
),
243-
*await self._find_keyword_references(document, result[0]),
243+
*await self._find_keyword_references(document, keyword[0]),
244244
]
245245

246246
return None
@@ -254,7 +254,7 @@ async def _references_Template_or_TestTemplate( # noqa: N802
254254
node = cast(Union[Template, TestTemplate], node)
255255
if node.value:
256256

257-
name_token = cast(RobotToken, node.get_token(RobotToken.NAME))
257+
name_token = cast(RobotToken, node.get_token(RobotToken.NAME, RobotToken.ARGUMENT))
258258
if name_token is None:
259259
return None
260260

@@ -263,20 +263,20 @@ async def _references_Template_or_TestTemplate( # noqa: N802
263263
if namespace is None:
264264
return None
265265

266-
result = await namespace.find_keyword(node.value)
267-
if result is not None and result.source is not None:
266+
keyword = await namespace.find_keyword(node.value)
267+
if keyword is not None and keyword.source is not None:
268268
return [
269269
*(
270270
[
271271
Location(
272-
uri=str(Uri.from_path(result.source)),
273-
range=range_from_token_or_node(node, name_token),
272+
uri=str(Uri.from_path(keyword.source)),
273+
range=keyword.range,
274274
)
275275
]
276276
if context.include_declaration
277277
else []
278278
),
279-
*await self._find_keyword_references(document, result),
279+
*await self._find_keyword_references(document, keyword),
280280
]
281281

282282
return None
@@ -318,7 +318,12 @@ async def _find_keyword_references_in_namespace(
318318
self, namespace: Namespace, kw_doc: KeywordDoc, cancel_token: CancelationToken
319319
) -> List[Location]:
320320
from robot.parsing.lexer.tokens import Token as RobotToken
321-
from robot.parsing.model.statements import Fixture, KeywordCall
321+
from robot.parsing.model.statements import (
322+
Fixture,
323+
KeywordCall,
324+
Template,
325+
TestTemplate,
326+
)
322327

323328
result: List[Location] = []
324329

@@ -344,6 +349,11 @@ async def _find_keyword_references_in_namespace(
344349
namespace, kw_doc, node, kw_token, arguments
345350
):
346351
result.append(location)
352+
elif isinstance(node, (Template, TestTemplate)):
353+
kw_token = node.get_token(RobotToken.NAME, RobotToken.ARGUMENT)
354+
355+
async for location in self.get_keyword_references_from_tokens(namespace, kw_doc, node, kw_token, []):
356+
result.append(location)
347357

348358
return result
349359

0 commit comments

Comments
 (0)