Skip to content

Commit b9cd85a

Browse files
committed
feat(langserver): highlight namespace references
1 parent 4ba77ab commit b9cd85a

File tree

1,014 files changed

+23881
-22715
lines changed

Some content is hidden

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

1,014 files changed

+23881
-22715
lines changed

packages/core/src/robotcode/core/lsp/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,6 +3985,9 @@ def extend(
39853985
),
39863986
)
39873987

3988+
def __bool__(self) -> bool:
3989+
return self.start != self.end
3990+
39883991
def __contains__(self, x: object) -> bool:
39893992
if isinstance(x, (Position, Range)):
39903993
return x.is_in_range(self)

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/entities.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ def range(self) -> Range:
7171
class LibraryImport(Import):
7272
args: Tuple[str, ...] = ()
7373
alias: Optional[str] = None
74+
alias_token: Optional[Token] = None
75+
76+
@property
77+
def alias_range(self) -> Range:
78+
return Range(
79+
start=Position(
80+
line=self.alias_token.lineno - 1 if self.alias_token is not None else -1,
81+
character=self.alias_token.col_offset if self.alias_token is not None else -1,
82+
),
83+
end=Position(
84+
line=self.alias_token.lineno - 1 if self.alias_token is not None else -1,
85+
character=self.alias_token.end_col_offset if self.alias_token is not None else -1,
86+
),
87+
)
7488

7589
@single_call
7690
def __hash__(self) -> int:
@@ -301,6 +315,7 @@ class LibraryEntry:
301315
alias: Optional[str] = None
302316
import_range: Range = field(default_factory=lambda: Range.zero())
303317
import_source: Optional[str] = None
318+
alias_range: Range = field(default_factory=lambda: Range.zero())
304319

305320
def __str__(self) -> str:
306321
result = self.import_name

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ def visit_LibraryImport(self, node: ast.AST) -> None: # noqa: N802
417417
n = cast(RobotLibraryImport, node)
418418
name = cast(RobotToken, n.get_token(RobotToken.NAME))
419419

420+
separator = n.get_token(RobotToken.WITH_NAME)
421+
alias_token = n.get_tokens(RobotToken.NAME)[-1] if separator else None
422+
420423
last_data_token = cast(
421424
RobotToken, next(v for v in reversed(n.tokens) if v.type not in RobotToken.NON_DATA_TOKENS)
422425
)
@@ -427,6 +430,7 @@ def visit_LibraryImport(self, node: ast.AST) -> None: # noqa: N802
427430
name_token=name if name is not None else None,
428431
args=n.args,
429432
alias=n.alias,
433+
alias_token=alias_token,
430434
line_no=node.lineno,
431435
col_offset=node.col_offset,
432436
end_line_no=last_data_token.lineno
@@ -1025,6 +1029,7 @@ async def _import(
10251029
)
10261030
result.import_range = value.range
10271031
result.import_source = value.source
1032+
result.alias_range = value.alias_range
10281033

10291034
self._import_entries[value] = result
10301035

packages/language_server/src/robotcode/language_server/robotframework/parts/document_highlight.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, List, Optional
3+
from typing import TYPE_CHECKING, Any, List, Optional, cast
44

55
from robotcode.core.logging import LoggingDescriptor
6-
from robotcode.core.lsp.types import DocumentHighlight, DocumentHighlightKind, Position
6+
from robotcode.core.lsp.types import DocumentHighlight, DocumentHighlightKind, Position, Range
77
from robotcode.language_server.common.decorators import language_id
88
from robotcode.language_server.common.text_document import TextDocument
99

@@ -60,4 +60,40 @@ async def collect(
6060
*(DocumentHighlight(e.range, DocumentHighlightKind.TEXT) for e in kw_refs),
6161
]
6262

63+
all_namespace_refs = await namespace.get_namespace_references()
64+
if all_namespace_refs:
65+
for ns, ns_refs in all_namespace_refs.items():
66+
found_range = (
67+
ns.import_range
68+
if ns.import_source == namespace.source
69+
and (position.is_in_range(ns.alias_range, False) or position.is_in_range(ns.import_range, False))
70+
else cast(
71+
Optional[Range], next((r.range for r in ns_refs if position.is_in_range(r.range, False)), None)
72+
)
73+
)
74+
75+
if found_range is not None:
76+
return [
77+
*(
78+
[
79+
DocumentHighlight(
80+
ns.import_range,
81+
DocumentHighlightKind.TEXT,
82+
)
83+
]
84+
if ns.import_source == namespace.source and ns.import_range and not ns.alias_range
85+
else []
86+
),
87+
*(
88+
[
89+
DocumentHighlight(
90+
ns.alias_range,
91+
DocumentHighlightKind.TEXT,
92+
)
93+
]
94+
if ns.import_source == namespace.source and ns.alias_range
95+
else []
96+
),
97+
*(DocumentHighlight(e.range, DocumentHighlightKind.TEXT) for e in ns_refs),
98+
]
6399
return None

packages/language_server/src/robotcode/language_server/robotframework/parts/hover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def _hover_default(self, nodes: List[ast.AST], document: TextDocument, pos
140140
for ns, ns_refs in all_namespace_refs.items():
141141
found_range = (
142142
ns.import_range
143-
if kw.source == namespace.source and position.is_in_range(ns.import_range, False)
143+
if ns.import_source == namespace.source and position.is_in_range(ns.import_range, False)
144144
else cast(
145145
Optional[Range], next((r.range for r in ns_refs if position.is_in_range(r.range, False)), None)
146146
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
data: !GeneratedTestData
2+
character: 29
3+
line: 6
4+
name: namespace reference with resource
5+
result:
6+
- !DocumentHighlight
7+
kind: !DocumentHighlightKind 'TEXT'
8+
range:
9+
end:
10+
character: 61
11+
line: 6
12+
start:
13+
character: 16
14+
line: 6
15+
- !DocumentHighlight
16+
kind: !DocumentHighlightKind 'TEXT'
17+
range:
18+
end:
19+
character: 17
20+
line: 70
21+
start:
22+
character: 4
23+
line: 70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
data: !GeneratedTestData
2+
character: 40
3+
line: 6
4+
name: namespace reference with resource
5+
result:
6+
- !DocumentHighlight
7+
kind: !DocumentHighlightKind 'TEXT'
8+
range:
9+
end:
10+
character: 61
11+
line: 6
12+
start:
13+
character: 16
14+
line: 6
15+
- !DocumentHighlight
16+
kind: !DocumentHighlightKind 'TEXT'
17+
range:
18+
end:
19+
character: 17
20+
line: 70
21+
start:
22+
character: 4
23+
line: 70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
data: !GeneratedTestData
2+
character: 51
3+
line: 6
4+
name: namespace reference with resource
5+
result:
6+
- !DocumentHighlight
7+
kind: !DocumentHighlightKind 'TEXT'
8+
range:
9+
end:
10+
character: 61
11+
line: 6
12+
start:
13+
character: 16
14+
line: 6
15+
- !DocumentHighlight
16+
kind: !DocumentHighlightKind 'TEXT'
17+
range:
18+
end:
19+
character: 17
20+
line: 70
21+
start:
22+
character: 4
23+
line: 70

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_highlight.test[document_highlight.robot-009-036-Variable_in_library_params].out

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_highlight.test[document_highlight.robot-009-041-Variable_in_library_params].out

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)