|
| 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) |
0 commit comments