Skip to content

Commit

Permalink
refactor(langserver): remove async code from inlay hints
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Jan 2, 2024
1 parent 2f25c78 commit 7535bfa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

from concurrent.futures import CancelledError
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union

Expand Down Expand Up @@ -27,7 +25,7 @@
class ImplementationProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
def __init__(self, parent: "LanguageServerProtocol") -> None:
super().__init__(parent)
self.link_support = False

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from asyncio import CancelledError
from concurrent.futures import CancelledError
from typing import TYPE_CHECKING, Any, Final, List, Optional

from robotcode.core.async_tools import async_tasking_event
from robotcode.core.concurrent import threaded
from robotcode.core.event import event
from robotcode.core.lsp.types import (
InlayHint,
InlayHintOptions,
Expand All @@ -28,12 +28,12 @@ class InlayHintProtocolPart(LanguageServerProtocolPart):
def __init__(self, parent: "LanguageServerProtocol") -> None:
super().__init__(parent)

@async_tasking_event
async def collect(sender, document: TextDocument, range: Range) -> Optional[List[InlayHint]]: # NOSONAR
@event
def collect(sender, document: TextDocument, range: Range) -> Optional[List[InlayHint]]: # NOSONAR
...

@async_tasking_event
async def resolve(sender, hint: InlayHint) -> Optional[InlayHint]: # NOSONAR
@event
def resolve(sender, hint: InlayHint) -> Optional[InlayHint]: # NOSONAR
...

def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
Expand All @@ -45,7 +45,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:

@rpc_method(name="textDocument/inlayHint", param_type=InlayHintParams)
@threaded
async def _text_document_inlay_hint(
def _text_document_inlay_hint(
self,
text_document: TextDocumentIdentifier,
range: Range,
Expand All @@ -58,7 +58,7 @@ async def _text_document_inlay_hint(
if document is None:
return None

for result in await self.collect(
for result in self.collect(
self,
document,
document.range_from_utf16(range),
Expand All @@ -72,8 +72,8 @@ async def _text_document_inlay_hint(
results.extend(result)

if results:
for result in results:
result.position = document.position_to_utf16(result.position)
for r in results:
r.position = document.position_to_utf16(r.position)
# TODO: resolve

return results
Expand All @@ -82,13 +82,13 @@ async def _text_document_inlay_hint(

@rpc_method(name="inlayHint/resolve", param_type=InlayHint)
@threaded
async def _inlay_hint_resolve(
def _inlay_hint_resolve(
self,
params: InlayHint,
*args: Any,
**kwargs: Any,
) -> Optional[InlayHint]:
for result in await self.resolve(self, params):
for result in self.resolve(self, params):
if isinstance(result, BaseException):
if not isinstance(result, CancelledError):
self._logger.exception(result, exc_info=result)
Expand All @@ -98,11 +98,11 @@ async def _inlay_hint_resolve(

return params

async def refresh(self) -> None:
def refresh(self) -> None:
if (
self.parent.client_capabilities is not None
and self.parent.client_capabilities.workspace is not None
and self.parent.client_capabilities.workspace.inlay_hint is not None
and self.parent.client_capabilities.workspace.inlay_hint.refresh_support
):
await self.parent.send_request_async("workspace/inlayHint/refresh")
self.parent.send_request("workspace/inlayHint/refresh").result()
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations

import ast
import asyncio
from typing import TYPE_CHECKING, Any, Awaitable, Callable, List, Optional, Type, cast
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Type, cast

from robot.parsing.lexer.tokens import Token
from robotcode.core.concurrent import check_current_thread_canceled
from robotcode.core.lsp.types import InlayHint, InlayHintKind, Range
from robotcode.core.utils.logging import LoggingDescriptor
from robotcode.robot.diagnostics.library_doc import KeywordArgumentKind, KeywordDoc, LibraryDoc
Expand All @@ -22,24 +20,24 @@
from .protocol_part import RobotLanguageServerProtocolPart

_HandlerMethod = Callable[
[TextDocument, Range, ast.AST, ast.AST, Namespace, InlayHintsConfig], Awaitable[Optional[List[InlayHint]]]
[TextDocument, Range, ast.AST, ast.AST, Namespace, InlayHintsConfig], Optional[List[InlayHint]]
]


class RobotInlayHintProtocolPart(RobotLanguageServerProtocolPart, ModelHelperMixin):
_logger = LoggingDescriptor()

def __init__(self, parent: RobotLanguageServerProtocol) -> None:
def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
super().__init__(parent)

parent.inlay_hint.collect.add(self.collect)

async def get_config(self, document: TextDocument) -> Optional[InlayHintsConfig]:
def get_config(self, document: TextDocument) -> Optional[InlayHintsConfig]:
folder = self.parent.workspace.get_workspace_folder(document.uri)
if folder is None:
return None

return await self.parent.workspace.get_configuration_async(InlayHintsConfig, folder.uri)
return self.parent.workspace.get_configuration(InlayHintsConfig, folder.uri)

def _find_method(self, cls: Type[Any]) -> Optional[_HandlerMethod]:
if cls is ast.AST:
Expand All @@ -58,8 +56,8 @@ def _find_method(self, cls: Type[Any]) -> Optional[_HandlerMethod]:

@language_id("robotframework")
@_logger.call
async def collect(self, sender: Any, document: TextDocument, range: Range) -> Optional[List[InlayHint]]:
config = await self.get_config(document)
def collect(self, sender: Any, document: TextDocument, range: Range) -> Optional[List[InlayHint]]:
config = self.get_config(document)
if config is None or not config.parameter_names and not config.namespaces:
return None

Expand All @@ -69,6 +67,8 @@ async def collect(self, sender: Any, document: TextDocument, range: Range) -> Op
result: List[InlayHint] = []

for node in iter_nodes(model):
check_current_thread_canceled()

node_range = range_from_node(node)
if node_range.end < range.start:
continue
Expand All @@ -78,13 +78,13 @@ async def collect(self, sender: Any, document: TextDocument, range: Range) -> Op

method = self._find_method(type(node))
if method is not None:
r = await method(document, range, node, model, namespace, config)
r = method(document, range, node, model, namespace, config)
if r is not None:
result.extend(r)

return result

async def _handle_keywordcall_fixture_template(
def _handle_keywordcall_fixture_template(
self,
keyword_token: Token,
arguments: List[Token],
Expand All @@ -107,9 +107,9 @@ async def _handle_keywordcall_fixture_template(
if kw_doc is None:
return None

return await self._get_inlay_hint(keyword_token, kw_doc, arguments, namespace, config)
return self._get_inlay_hint(keyword_token, kw_doc, arguments, namespace, config)

async def _get_inlay_hint(
def _get_inlay_hint(
self,
keyword_token: Optional[Token],
kw_doc: KeywordDoc,
Expand Down Expand Up @@ -197,7 +197,7 @@ async def _get_inlay_hint(

return result

async def handle_KeywordCall( # noqa: N802
def handle_KeywordCall( # noqa: N802
self,
document: TextDocument,
range: Range,
Expand All @@ -215,9 +215,9 @@ async def handle_KeywordCall( # noqa: N802
return None

arguments = keyword_call.get_tokens(RobotToken.ARGUMENT)
return await self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)
return self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)

async def handle_Fixture( # noqa: N802
def handle_Fixture( # noqa: N802
self,
document: TextDocument,
range: Range,
Expand All @@ -235,9 +235,9 @@ async def handle_Fixture( # noqa: N802
return None

arguments = fixture.get_tokens(RobotToken.ARGUMENT)
return await self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)
return self._handle_keywordcall_fixture_template(keyword_token, arguments, namespace, config)

async def handle_TestTemplate( # noqa: N802
def handle_TestTemplate( # noqa: N802
self,
document: TextDocument,
range: Range,
Expand All @@ -254,9 +254,9 @@ async def handle_TestTemplate( # noqa: N802
if keyword_token is None or not keyword_token.value:
return None

return await self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)
return self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)

async def handle_Template( # noqa: N802
def handle_Template( # noqa: N802
self,
document: TextDocument,
range: Range,
Expand All @@ -273,9 +273,9 @@ async def handle_Template( # noqa: N802
if keyword_token is None or not keyword_token.value:
return None

return await self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)
return self._handle_keywordcall_fixture_template(keyword_token, [], namespace, config)

async def handle_LibraryImport( # noqa: N802
def handle_LibraryImport( # noqa: N802
self,
document: TextDocument,
range: Range,
Expand Down Expand Up @@ -306,19 +306,20 @@ async def handle_LibraryImport( # noqa: N802
variables=namespace.get_resolvable_variables(),
)

except (asyncio.CancelledError, SystemExit, KeyboardInterrupt):
except (SystemExit, KeyboardInterrupt):
raise
except BaseException:
except BaseException as e:
self._logger.exception(e)
return None

arguments = library_node.get_tokens(RobotToken.ARGUMENT)

for kw_doc in lib_doc.inits:
return await self._get_inlay_hint(None, kw_doc, arguments, namespace, config)
return self._get_inlay_hint(None, kw_doc, arguments, namespace, config)

return None

async def handle_VariablesImport( # noqa: N802
def handle_VariablesImport( # noqa: N802
self,
document: TextDocument,
range: Range,
Expand Down Expand Up @@ -352,14 +353,15 @@ async def handle_VariablesImport( # noqa: N802
variables=namespace.get_resolvable_variables(),
)

except (asyncio.CancelledError, SystemExit, KeyboardInterrupt):
except (SystemExit, KeyboardInterrupt):
raise
except BaseException:
except BaseException as e:
self._logger.exception(e)
return None

arguments = library_node.get_tokens(RobotToken.ARGUMENT)

for kw_doc in lib_doc.inits:
return await self._get_inlay_hint(None, kw_doc, arguments, namespace, config)
return self._get_inlay_hint(None, kw_doc, arguments, namespace, config)

return None

0 comments on commit 7535bfa

Please sign in to comment.