Skip to content

Commit 8c1f4ea

Browse files
committed
some correction to load libraries/resources with same name. fixes #9
1 parent 8983d42 commit 8c1f4ea

File tree

4 files changed

+39
-48
lines changed

4 files changed

+39
-48
lines changed

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ def import_test_library(
923923
) -> Union[Any, Tuple[Any, str]]:
924924

925925
with OutputCapturer(library_import=True):
926-
importer = Importer("test library")
926+
importer = Importer("test library", LOGGER)
927927
return importer.import_class_or_module(name, return_source=True)
928928

929929
def get_test_library(

robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ async def _import(value: Import) -> Optional[LibraryEntry]:
10281028
]
10291029

10301030
if not allready_imported_resources and entry.library_doc.source != self.source:
1031-
self._resources[entry.alias or entry.name or entry.import_name] = entry
1031+
self._resources[entry.import_name] = entry
10321032
try:
10331033
await self._import_imports(
10341034
entry.imports,
@@ -1060,7 +1060,7 @@ async def _import(value: Import) -> Optional[LibraryEntry]:
10601060
)
10611061
)
10621062
elif allready_imported_resources and allready_imported_resources[0].library_doc.source:
1063-
self._resources[entry.alias or entry.name or entry.import_name] = entry
1063+
self._resources[entry.import_name] = entry
10641064

10651065
self._diagnostics.append(
10661066
Diagnostic(
@@ -1130,7 +1130,8 @@ async def _import(value: Import) -> Optional[LibraryEntry]:
11301130
)
11311131
)
11321132

1133-
self._libraries[entry.alias or entry.name or entry.import_name] = entry
1133+
if (entry.alias or entry.name or entry.import_name) not in self._libraries:
1134+
self._libraries[entry.alias or entry.name or entry.import_name] = entry
11341135
# TODO Variables
11351136

11361137
async def _import_default_libraries(self) -> None:
@@ -1310,7 +1311,7 @@ async def _yield_owner_and_kw_names(self, full_name: str) -> AsyncIterator[Tuple
13101311
async def _get_explicit_keyword(self, name: str) -> Optional[KeywordDoc]:
13111312
found: List[Tuple[LibraryEntry, KeywordDoc]] = []
13121313
async for owner_name, kw_name in self._yield_owner_and_kw_names(name):
1313-
found.extend(await self._find_keywords(owner_name, kw_name))
1314+
found.extend(await self.find_keywords(owner_name, kw_name))
13141315
if len(found) > 1:
13151316
self.diagnostics.append(
13161317
DiagnosticsEntry(
@@ -1323,13 +1324,13 @@ async def _get_explicit_keyword(self, name: str) -> Optional[KeywordDoc]:
13231324

13241325
return found[0][1] if found else None
13251326

1326-
async def _find_keywords(self, owner_name: str, name: str) -> Sequence[Tuple[LibraryEntry, KeywordDoc]]:
1327+
async def find_keywords(self, owner_name: str, name: str) -> Sequence[Tuple[LibraryEntry, KeywordDoc]]:
13271328
from robot.utils.match import eq
13281329

13291330
return [
13301331
(v, v.library_doc.keywords[name])
1331-
async for k, v in async_chain(self.namespace._libraries.items(), self.namespace._resources.items())
1332-
if eq(k, owner_name) and name in v.library_doc.keywords
1332+
async for v in async_chain(self.namespace._libraries.values(), self.namespace._resources.values())
1333+
if eq(v.alias or v.name, owner_name) and name in v.library_doc.keywords
13331334
]
13341335

13351336
def _create_multiple_keywords_found_message(

robotcode/language_server/robotframework/parts/goto.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,23 @@ async def definition_LibraryImport( # noqa: N802
245245
if namespace is None:
246246
return None
247247

248-
libdocs = [
249-
entry.library_doc
250-
for entry in (await namespace.get_libraries()).values()
251-
if entry.import_name == library_node.name
252-
and entry.args == library_node.args
253-
and entry.alias == library_node.alias
254-
]
248+
try:
249+
libdoc = await namespace.imports_manager.get_libdoc_for_library_import(
250+
library_node.name, library_node.args, str(document.uri.to_path().parent)
251+
)
255252

256-
if len(libdocs) == 1:
257-
result = libdocs[0]
258-
python_source = result.source_or_origin
253+
python_source = libdoc.source_or_origin
259254
if python_source is not None:
260255
return [
261256
LocationLink(
262257
origin_selection_range=range_from_token_or_node(library_node, name_token),
263258
target_uri=str(Uri.from_path(python_source)),
264-
target_range=result.range,
265-
target_selection_range=result.range,
259+
target_range=libdoc.range,
260+
target_selection_range=libdoc.range,
266261
)
267262
]
263+
except BaseException:
264+
pass
268265
return None
269266

270267
async def definition_ResourceImport( # noqa: N802
@@ -285,22 +282,21 @@ async def definition_ResourceImport( # noqa: N802
285282
if namespace is None:
286283
return None
287284

288-
libdocs = [
289-
entry.library_doc
290-
for entry in (await namespace.get_resources()).values()
291-
if entry.import_name == resource_node.name
292-
]
285+
try:
286+
libdoc = await namespace.imports_manager.get_libdoc_for_resource_import(
287+
resource_node.name, str(document.uri.to_path().parent)
288+
)
293289

294-
if len(libdocs) == 1:
295-
result = libdocs[0]
296-
python_source = result.source_or_origin
290+
python_source = libdoc.source_or_origin
297291
if python_source is not None:
298292
return [
299293
LocationLink(
300294
origin_selection_range=range_from_token_or_node(resource_node, name_token),
301295
target_uri=str(Uri.from_path(python_source)),
302-
target_range=result.range,
303-
target_selection_range=result.range,
296+
target_range=libdoc.range,
297+
target_selection_range=libdoc.range,
304298
)
305299
]
300+
except BaseException:
301+
pass
306302
return None

robotcode/language_server/robotframework/parts/hover.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,10 @@ async def hover_LibraryImport( # noqa: N802
213213
if namespace is None:
214214
return None
215215

216-
libdocs = [
217-
entry.library_doc
218-
for entry in (await namespace.get_libraries()).values()
219-
if entry.import_name == library_node.name
220-
and entry.args == library_node.args
221-
and entry.alias == library_node.alias
222-
]
223-
224-
if len(libdocs) == 1:
225-
libdoc = libdocs[0]
216+
try:
217+
libdoc = await namespace.imports_manager.get_libdoc_for_library_import(
218+
library_node.name, library_node.args, str(document.uri.to_path().parent)
219+
)
226220

227221
return Hover(
228222
contents=MarkupContent(
@@ -231,6 +225,8 @@ async def hover_LibraryImport( # noqa: N802
231225
),
232226
range=range_from_token_or_node(library_node, name_token),
233227
)
228+
except BaseException:
229+
pass
234230
return None
235231

236232
async def hover_ResourceImport( # noqa: N802
@@ -251,19 +247,17 @@ async def hover_ResourceImport( # noqa: N802
251247
if namespace is None:
252248
return None
253249

254-
libdocs = [
255-
entry.library_doc
256-
for entry in (await namespace.get_resources()).values()
257-
if entry.import_name == resource_node.name
258-
]
259-
260-
if len(libdocs) == 1:
261-
libdoc = libdocs[0]
250+
try:
251+
libdoc = await namespace.imports_manager.get_libdoc_for_resource_import(
252+
resource_node.name, str(document.uri.to_path().parent)
253+
)
262254
return Hover(
263255
contents=MarkupContent(
264256
kind=MarkupKind.MARKDOWN,
265257
value=libdoc.to_markdown(),
266258
),
267259
range=range_from_token_or_node(resource_node, name_token),
268260
)
261+
except BaseException:
262+
pass
269263
return None

0 commit comments

Comments
 (0)