Skip to content

Commit ec3ab42

Browse files
committed
remove unnecessary CancelationToken
1 parent 333d27d commit ec3ab42

File tree

11 files changed

+121
-230
lines changed

11 files changed

+121
-230
lines changed

robotcode/jsonrpc2/protocol.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@
3030
runtime_checkable,
3131
)
3232

33-
from ..utils.async_tools import (
34-
CancelationToken,
35-
async_event,
36-
create_sub_future,
37-
create_sub_task,
38-
)
33+
from ..utils.async_tools import async_event, create_sub_future, create_sub_task
3934
from ..utils.dataclasses import as_json, from_dict
4035
from ..utils.inspect import ensure_coroutine, iter_methods
4136
from ..utils.logging import LoggingDescriptor
@@ -332,7 +327,6 @@ class SendedRequestEntry(NamedTuple):
332327
class ReceivedRequestEntry(NamedTuple):
333328
future: asyncio.Future[Any]
334329
request: Optional[Any]
335-
cancel_token: CancelationToken
336330
cancelable: bool
337331

338332

@@ -664,16 +658,13 @@ def handle_request(self, message: JsonRPCRequest) -> Optional[asyncio.Task[_T]]:
664658
return None
665659

666660
params = self._convert_params(e.method, e.param_type, message.params)
667-
cancel_token = CancelationToken()
668661

669662
task = create_sub_task(
670-
ensure_coroutine(e.method)(
671-
*params[0], **({"cancel_token": cancel_token} if e.cancelable else {}), **params[1]
672-
),
663+
ensure_coroutine(e.method)(*params[0], **params[1]),
673664
name=message.method,
674665
)
675666
with self._received_request_lock:
676-
self._received_request[message.id] = ReceivedRequestEntry(task, message, cancel_token, e.cancelable)
667+
self._received_request[message.id] = ReceivedRequestEntry(task, message, e.cancelable)
677668

678669
def done(t: asyncio.Task[Any]) -> None:
679670
try:
@@ -702,15 +693,12 @@ def cancel_request(self, id: Union[int, str, None]) -> None:
702693

703694
if entry is not None and entry.future is not None and not entry.future.cancelled():
704695
self._logger.debug(f"try to cancel request {entry.request}")
705-
entry.cancel_token.cancel()
706696
entry.future.cancel()
707697

708698
@_logger.call
709699
async def cancel_all_received_request(self) -> None:
710700
for entry in self._received_request.values():
711701
if entry is not None and entry.cancelable and entry.future is not None and not entry.future.cancelled():
712-
if entry.cancel_token:
713-
entry.cancel_token.cancel()
714702
entry.future.cancel()
715703

716704
@_logger.call

robotcode/language_server/common/parts/diagnostics.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, cast
77

88
from ....utils.async_tools import (
9-
CancelationToken,
109
Lock,
1110
async_tasking_event_iterator,
1211
check_canceled,
@@ -36,7 +35,6 @@ def __init__(
3635
self,
3736
uri: Uri,
3837
version: Optional[int],
39-
cancelation_token: CancelationToken,
4038
factory: Callable[..., asyncio.Future[Any]],
4139
done_callback: Callable[[PublishDiagnosticsEntry], Any],
4240
) -> None:
@@ -49,7 +47,6 @@ def __init__(
4947

5048
self._future: Optional[asyncio.Future[Any]] = None
5149

52-
self.cancel_token = cancelation_token
5350
self.done = False
5451

5552
def _done(t: asyncio.Future[Any]) -> None:
@@ -86,9 +83,6 @@ async def cancel(self) -> None:
8683
if self.future is None:
8784
return
8885

89-
if not self.done:
90-
self.cancel_token.cancel()
91-
9286
self.done = True
9387

9488
if not self.future.done() and not self.future.cancelled():
@@ -129,9 +123,7 @@ def __init__(self, protocol: LanguageServerProtocol) -> None:
129123
self.parent.documents.did_save.add(self.on_did_save)
130124

131125
@async_tasking_event_iterator
132-
async def collect(
133-
sender, document: TextDocument, cancelation_token: CancelationToken # NOSONAR
134-
) -> DiagnosticsResult:
126+
async def collect(sender, document: TextDocument) -> DiagnosticsResult: # NOSONAR
135127
...
136128

137129
@_logger.call
@@ -206,17 +198,15 @@ async def start_publish_diagnostics_task(self, document: TextDocument) -> None:
206198

207199
await self._cancel_entry(entry)
208200

209-
cancelation_token = CancelationToken()
210201
self._running_diagnostics[document.uri] = PublishDiagnosticsEntry(
211202
document.uri,
212203
document.version,
213-
cancelation_token,
214-
lambda: run_coroutine_in_thread(self.publish_diagnostics, document.document_uri, cancelation_token),
204+
lambda: run_coroutine_in_thread(self.publish_diagnostics, document.document_uri),
215205
self._delete_entry,
216206
)
217207

218208
@_logger.call
219-
async def publish_diagnostics(self, document_uri: DocumentUri, cancelation_token: CancelationToken) -> None:
209+
async def publish_diagnostics(self, document_uri: DocumentUri) -> None:
220210
document = await self.parent.documents.get(document_uri)
221211
if document is None:
222212
return
@@ -228,7 +218,6 @@ async def publish_diagnostics(self, document_uri: DocumentUri, cancelation_token
228218
async for result_any in self.collect(
229219
self,
230220
document,
231-
cancelation_token,
232221
callback_filter=language_id_filter(document),
233222
return_exceptions=True,
234223
):

robotcode/language_server/common/parts/hover.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import TYPE_CHECKING, Any, List, Optional
55

66
from ....jsonrpc2.protocol import rpc_method
7-
from ....utils.async_tools import CancelationToken, async_tasking_event
7+
from ....utils.async_tools import async_tasking_event
88
from ....utils.logging import LoggingDescriptor
99
from ..decorators import language_id_filter
1010
from ..has_extend_capabilities import HasExtendCapabilities
@@ -31,9 +31,7 @@ def __init__(self, parent: LanguageServerProtocol) -> None:
3131
super().__init__(parent)
3232

3333
@async_tasking_event
34-
async def collect(
35-
sender, document: TextDocument, position: Position, cancel_token: Optional[CancelationToken] = None
36-
) -> Optional[Hover]: # NOSONAR
34+
async def collect(sender, document: TextDocument, position: Position) -> Optional[Hover]: # NOSONAR
3735
...
3836

3937
def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
@@ -45,7 +43,6 @@ async def _text_document_hover(
4543
self,
4644
text_document: TextDocumentIdentifier,
4745
position: Position,
48-
cancel_token: Optional[CancelationToken] = None,
4946
*args: Any,
5047
**kwargs: Any,
5148
) -> Optional[Hover]:
@@ -60,7 +57,6 @@ async def _text_document_hover(
6057
self,
6158
document,
6259
position,
63-
cancel_token=cancel_token,
6460
callback_filter=language_id_filter(document),
6561
):
6662
if isinstance(result, BaseException):

robotcode/language_server/robotframework/diagnostics/analyzer.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import re
66
from typing import Any, List, Optional, Union, cast
77

8-
from ....utils.async_tools import CancelationToken
98
from ....utils.uri import Uri
109
from ...common.lsp_types import (
1110
CodeDescription,
@@ -33,12 +32,9 @@
3332

3433

3534
class Analyzer(AsyncVisitor):
36-
async def get(
37-
self, model: ast.AST, namespace: Namespace, cancelation_token: Optional[CancelationToken] = None
38-
) -> List[Diagnostic]:
35+
async def get(self, model: ast.AST, namespace: Namespace) -> List[Diagnostic]:
3936
self._results: List[Diagnostic] = []
4037
self._namespace = namespace
41-
self.cancelation_token = cancelation_token
4238
self.current_testcase_or_keyword_name: Optional[str] = None
4339
self.finder = KeywordFinder(self._namespace)
4440

@@ -78,6 +74,7 @@ async def append_diagnostics(
7874
related_information: Optional[List[DiagnosticRelatedInformation]] = None,
7975
data: Optional[Any] = None,
8076
) -> None:
77+
8178
if await self.should_ignore(self._namespace.document, range):
8279
return
8380

robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525

2626
from ....utils.async_itertools import async_chain
27-
from ....utils.async_tools import CancelationToken, Lock
27+
from ....utils.async_tools import Lock
2828
from ....utils.logging import LoggingDescriptor
2929
from ....utils.uri import Uri
3030
from ...common.lsp_types import (
@@ -542,10 +542,10 @@ async def invalidate(self) -> None:
542542
self.invalidated_callback(self)
543543

544544
@_logger.call
545-
async def get_diagnostisc(self, cancelation_token: Optional[CancelationToken] = None) -> List[Diagnostic]:
545+
async def get_diagnostisc(self) -> List[Diagnostic]:
546546
await self.ensure_initialized()
547547

548-
await self._analyze(cancelation_token)
548+
await self._analyze()
549549

550550
return self._diagnostics
551551

@@ -1220,15 +1220,15 @@ async def append_diagnostics(
12201220
)
12211221

12221222
@_logger.call
1223-
async def _analyze(self, cancelation_token: Optional[CancelationToken] = None) -> None:
1223+
async def _analyze(self) -> None:
12241224
from .analyzer import Analyzer
12251225

12261226
if not self._analyzed:
12271227
async with self._analyze_lock:
12281228
if not self._analyzed:
12291229
canceled = False
12301230
try:
1231-
result = await Analyzer().get(self.model, self, cancelation_token)
1231+
result = await Analyzer().get(self.model, self)
12321232

12331233
self._diagnostics += result
12341234

robotcode/language_server/robotframework/parts/diagnostics.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import asyncio
55
from typing import TYPE_CHECKING, Any, List, Optional
66

7-
from ....utils.async_tools import CancelationToken, check_canceled, threaded
7+
from ....utils.async_tools import check_canceled, threaded
88
from ....utils.logging import LoggingDescriptor
99
from ...common.decorators import language_id
1010
from ...common.lsp_types import Diagnostic, DiagnosticSeverity, Position, Range
@@ -40,17 +40,13 @@ async def namespace_invalidated(self, sender: Any, document: TextDocument) -> No
4040

4141
@language_id("robotframework")
4242
@threaded()
43-
async def collect_namespace_diagnostics(
44-
self, sender: Any, document: TextDocument, cancelation_token: CancelationToken
45-
) -> DiagnosticsResult:
43+
async def collect_namespace_diagnostics(self, sender: Any, document: TextDocument) -> DiagnosticsResult:
4644
try:
4745
namespace = await self.parent.documents_cache.get_namespace(document)
4846
if namespace is None:
4947
return DiagnosticsResult(self.collect_namespace_diagnostics, None)
5048

51-
return DiagnosticsResult(
52-
self.collect_namespace_diagnostics, await namespace.get_diagnostisc(cancelation_token)
53-
)
49+
return DiagnosticsResult(self.collect_namespace_diagnostics, await namespace.get_diagnostisc())
5450
except (asyncio.CancelledError, SystemExit, KeyboardInterrupt):
5551
raise
5652
except BaseException as e:
@@ -96,9 +92,7 @@ def _create_error_from_token(self, token: Token, source: Optional[str] = None) -
9692

9793
@language_id("robotframework")
9894
@_logger.call
99-
async def collect_token_errors(
100-
self, sender: Any, document: TextDocument, cancelation_token: CancelationToken
101-
) -> DiagnosticsResult:
95+
async def collect_token_errors(self, sender: Any, document: TextDocument) -> DiagnosticsResult:
10296
from robot.errors import VariableError
10397
from robot.parsing.lexer.tokens import Token
10498

@@ -164,9 +158,7 @@ async def collect_token_errors(
164158
@language_id("robotframework")
165159
@threaded()
166160
@_logger.call
167-
async def collect_walk_model_errors(
168-
self, sender: Any, document: TextDocument, cancelation_token: CancelationToken
169-
) -> DiagnosticsResult:
161+
async def collect_walk_model_errors(self, sender: Any, document: TextDocument) -> DiagnosticsResult:
170162

171163
from ..utils.ast import HasError, HasErrors
172164
from ..utils.async_ast import iter_nodes

0 commit comments

Comments
 (0)