Skip to content

Commit 3adddd1

Browse files
committed
refactor(langserver): remove async code from semantic tokens
1 parent 15e409d commit 3adddd1

File tree

2 files changed

+71
-84
lines changed

2 files changed

+71
-84
lines changed

packages/language_server/src/robotcode/language_server/common/parts/semantic_tokens.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from __future__ import annotations
2-
3-
from asyncio import CancelledError
1+
from concurrent.futures import CancelledError
42
from enum import Enum
53
from typing import TYPE_CHECKING, Any, Final, List, Union
64

7-
from robotcode.core.async_tools import async_tasking_event
8-
from robotcode.core.concurrent import threaded
5+
from robotcode.core.concurrent import check_current_thread_canceled, threaded
6+
from robotcode.core.event import event
97
from robotcode.core.lsp.types import (
108
Range,
119
SemanticTokenModifiers,
@@ -37,28 +35,28 @@
3735
class SemanticTokensProtocolPart(LanguageServerProtocolPart):
3836
_logger: Final = LoggingDescriptor()
3937

40-
def __init__(self, parent: LanguageServerProtocol) -> None:
38+
def __init__(self, parent: "LanguageServerProtocol") -> None:
4139
super().__init__(parent)
4240
self.token_types: List[Enum] = list(SemanticTokenTypes)
4341
self.token_modifiers: List[Enum] = list(SemanticTokenModifiers)
4442

45-
@async_tasking_event
46-
async def collect_full(
43+
@event
44+
def collect_full(
4745
sender, document: TextDocument, **kwargs: Any # NOSONAR
4846
) -> Union[SemanticTokens, SemanticTokensPartialResult, None]:
4947
...
5048

51-
@async_tasking_event
52-
async def collect_full_delta(
49+
@event
50+
def collect_full_delta(
5351
sender,
5452
document: TextDocument,
5553
previous_result_id: str,
5654
**kwargs: Any, # NOSONAR
5755
) -> Union[SemanticTokens, SemanticTokensDelta, SemanticTokensDeltaPartialResult, None]:
5856
...
5957

60-
@async_tasking_event
61-
async def collect_range(
58+
@event
59+
def collect_range(
6260
sender, document: TextDocument, range: Range, **kwargs: Any # NOSONAR
6361
) -> Union[SemanticTokens, SemanticTokensPartialResult, None]:
6462
...
@@ -80,7 +78,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
8078

8179
@rpc_method(name="textDocument/semanticTokens/full", param_type=SemanticTokensParams)
8280
@threaded
83-
async def _text_document_semantic_tokens_full(
81+
def _text_document_semantic_tokens_full(
8482
self, text_document: TextDocumentIdentifier, *args: Any, **kwargs: Any
8583
) -> Union[SemanticTokens, SemanticTokensPartialResult, None]:
8684
results: List[Union[SemanticTokens, SemanticTokensPartialResult]] = []
@@ -89,12 +87,9 @@ async def _text_document_semantic_tokens_full(
8987
if document is None:
9088
return None
9189

92-
for result in await self.collect_full(
93-
self,
94-
document,
95-
callback_filter=language_id_filter(document),
96-
**kwargs,
97-
):
90+
for result in self.collect_full(self, document, callback_filter=language_id_filter(document), **kwargs):
91+
check_current_thread_canceled()
92+
9893
if isinstance(result, BaseException):
9994
if not isinstance(result, CancelledError):
10095
self._logger.exception(result, exc_info=result)
@@ -113,7 +108,7 @@ async def _text_document_semantic_tokens_full(
113108
param_type=SemanticTokensDeltaParams,
114109
)
115110
@threaded
116-
async def _text_document_semantic_tokens_full_delta(
111+
def _text_document_semantic_tokens_full_delta(
117112
self,
118113
text_document: TextDocumentIdentifier,
119114
previous_result_id: str,
@@ -126,13 +121,10 @@ async def _text_document_semantic_tokens_full_delta(
126121
if document is None:
127122
return None
128123

129-
for result in await self.collect_full_delta(
130-
self,
131-
document,
132-
previous_result_id,
133-
callback_filter=language_id_filter(document),
134-
**kwargs,
124+
for result in self.collect_full_delta(
125+
self, document, previous_result_id, callback_filter=language_id_filter(document), **kwargs
135126
):
127+
check_current_thread_canceled()
136128
if isinstance(result, BaseException):
137129
if not isinstance(result, CancelledError):
138130
self._logger.exception(result, exc_info=result)
@@ -148,7 +140,7 @@ async def _text_document_semantic_tokens_full_delta(
148140

149141
@rpc_method(name="textDocument/semanticTokens/range", param_type=SemanticTokensRangeParams)
150142
@threaded
151-
async def _text_document_semantic_tokens_range(
143+
def _text_document_semantic_tokens_range(
152144
self,
153145
text_document: TextDocumentIdentifier,
154146
range: Range,
@@ -161,13 +153,8 @@ async def _text_document_semantic_tokens_range(
161153
if document is None:
162154
return None
163155

164-
for result in await self.collect_range(
165-
self,
166-
document,
167-
range,
168-
callback_filter=language_id_filter(document),
169-
**kwargs,
170-
):
156+
for result in self.collect_range(self, document, range, callback_filter=language_id_filter(document), **kwargs):
157+
check_current_thread_canceled()
171158
if isinstance(result, BaseException):
172159
if not isinstance(result, CancelledError):
173160
self._logger.exception(result, exc_info=result)
@@ -188,4 +175,4 @@ def refresh(self) -> None:
188175
and self.parent.client_capabilities.workspace.semantic_tokens is not None
189176
and self.parent.client_capabilities.workspace.semantic_tokens.refresh_support
190177
):
191-
self.parent.send_request("workspace/semanticTokens/refresh")
178+
self.parent.send_request("workspace/semanticTokens/refresh").result(30)

0 commit comments

Comments
 (0)