Skip to content

Commit ad7b4c2

Browse files
committed
start implementing references codelens
1 parent 538ce9d commit ad7b4c2

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

robotcode/language_server/common/lsp_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,7 @@ class _CodeLensParams(Model):
16331633
text_document: TextDocumentIdentifier
16341634

16351635

1636+
@dataclass(repr=False)
16361637
class CodeLensParams(WorkDoneProgressParams, PartialResultParams, _CodeLensParams):
16371638
pass
16381639

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from __future__ import annotations
2+
3+
import ast
4+
from typing import TYPE_CHECKING, Any, List, Optional, cast
5+
6+
from ....utils.async_tools import run_coroutine_in_thread
7+
from ....utils.logging import LoggingDescriptor
8+
from ...common.language import language_id
9+
from ...common.lsp_types import CodeLens, Command
10+
from ...common.text_document import TextDocument
11+
from ..utils.ast import range_from_token
12+
13+
if TYPE_CHECKING:
14+
from ..protocol import RobotLanguageServerProtocol
15+
16+
from .protocol_part import RobotLanguageServerProtocolPart
17+
18+
19+
class RobotCodeLensProtocolPart(RobotLanguageServerProtocolPart):
20+
_logger = LoggingDescriptor()
21+
22+
def __init__(self, parent: RobotLanguageServerProtocol) -> None:
23+
super().__init__(parent)
24+
25+
# parent.code_lens.collect.add(self.collect)
26+
27+
@language_id("robotframework")
28+
async def collect(self, sender: Any, document: TextDocument) -> Optional[List[CodeLens]]:
29+
30+
from ..utils.async_ast import AsyncVisitor
31+
32+
class Visitor(AsyncVisitor):
33+
def __init__(self, parent: RobotCodeLensProtocolPart) -> None:
34+
super().__init__()
35+
self.parent = parent
36+
37+
self.result: List[CodeLens] = []
38+
39+
async def visit(self, node: ast.AST) -> None:
40+
await super().visit(node)
41+
42+
@classmethod
43+
async def find_from(cls, model: ast.AST, parent: RobotCodeLensProtocolPart) -> Optional[List[CodeLens]]:
44+
45+
finder = cls(parent)
46+
47+
await finder.visit(model)
48+
49+
return finder.result if finder.result else None
50+
51+
async def visit_Section(self, node: ast.AST) -> None: # noqa: N802
52+
from robot.parsing.model.blocks import KeywordSection
53+
54+
if isinstance(node, KeywordSection):
55+
await self.generic_visit(node)
56+
57+
async def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
58+
from robot.parsing.lexer.tokens import Token as RobotToken
59+
from robot.parsing.model.blocks import Keyword
60+
61+
keyword = cast(Keyword, node)
62+
63+
if keyword.header:
64+
name_token = keyword.header.get_token(RobotToken.KEYWORD_NAME)
65+
if name_token is None:
66+
return
67+
68+
r = range_from_token(name_token)
69+
self.result.append(
70+
CodeLens(
71+
r,
72+
Command(
73+
"references",
74+
"robotcode.action.findReferences",
75+
[str(document.uri), {"lineNumber": r.start.line, "column": r.start.character}],
76+
),
77+
)
78+
)
79+
80+
async def run() -> Optional[List[CodeLens]]:
81+
return await Visitor.find_from(await self.parent.documents_cache.get_model(document), self)
82+
83+
return await run_coroutine_in_thread(run)

robotcode/language_server/robotframework/parts/folding_range.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ast
44
from typing import TYPE_CHECKING, Any, List, Optional, cast
55

6-
from ....utils.async_tools import check_canceled, run_coroutine_in_thread
6+
from ....utils.async_tools import run_coroutine_in_thread
77
from ....utils.logging import LoggingDescriptor
88
from ...common.language import language_id
99
from ...common.lsp_types import FoldingRange
@@ -47,7 +47,6 @@ def __init__(self, parent: RobotFoldingRangeProtocolPart) -> None:
4747
self.result: List[FoldingRange] = []
4848

4949
async def visit(self, node: ast.AST) -> None:
50-
await check_canceled()
5150
await super().visit(node)
5251

5352
@classmethod

robotcode/language_server/robotframework/protocol.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121
from ..common.parts.document_symbols import symbol_information_label
2222
from ..common.protocol import LanguageServerProtocol
23+
from .parts.codelens import RobotCodeLensProtocolPart
2324
from .parts.completion import RobotCompletionProtocolPart
2425
from .parts.diagnostics import RobotDiagnosticsProtocolPart
2526
from .parts.discovering import DiscoveringProtocolPart
@@ -88,6 +89,7 @@ class RobotLanguageServerProtocol(LanguageServerProtocol):
8889
robot_discovering = ProtocolPartDescriptor(DiscoveringProtocolPart)
8990
robot_semantic_tokens = ProtocolPartDescriptor(RobotSemanticTokenProtocolPart)
9091
robot_references = ProtocolPartDescriptor(RobotReferencesProtocolPart)
92+
robot_codelens = ProtocolPartDescriptor(RobotCodeLensProtocolPart)
9193
robot_workspace = ProtocolPartDescriptor(RobotWorkspaceProtocolPart)
9294

9395
name = "RobotCode"

0 commit comments

Comments
 (0)