Skip to content

Commit

Permalink
refactor(langserver): remove async code from rename
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Jan 3, 2024
1 parent 0c38843 commit 15e409d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from concurrent.futures import CancelledError
from typing import TYPE_CHECKING, Any, Final, List, Optional

from robotcode.core.concurrent import threaded
from robotcode.core.concurrent import check_current_thread_canceled, threaded
from robotcode.core.event import event
from robotcode.core.lsp.types import (
Location,
Expand Down Expand Up @@ -64,6 +64,8 @@ def _text_document_references(
context,
callback_filter=language_id_filter(document),
):
check_current_thread_canceled()

if isinstance(result, BaseException):
if not isinstance(result, CancelledError):
self._logger.exception(result, exc_info=result)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations

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.concurrent import check_current_thread_canceled, threaded
from robotcode.core.event import event
from robotcode.core.lsp.types import (
ErrorCodes,
Position,
Expand Down Expand Up @@ -37,7 +35,7 @@ class CantRenameError(Exception):
class RenameProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

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

def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
Expand All @@ -46,21 +44,19 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
prepare_provider=len(self.collect_prepare) > 0, work_done_progress=True
)

@async_tasking_event
async def collect(
@event
def collect(
sender, document: TextDocument, position: Position, new_name: str # NOSONAR
) -> Optional[WorkspaceEdit]:
...

@async_tasking_event
async def collect_prepare(
sender, document: TextDocument, position: Position # NOSONAR
) -> Optional[PrepareRenameResult]:
@event
def collect_prepare(sender, document: TextDocument, position: Position) -> Optional[PrepareRenameResult]: # NOSONAR
...

@rpc_method(name="textDocument/rename", param_type=RenameParams)
@threaded
async def _text_document_rename(
def _text_document_rename(
self,
text_document: TextDocumentIdentifier,
position: Position,
Expand All @@ -74,13 +70,15 @@ async def _text_document_rename(
if document is None:
return None

for result in await self.collect(
for result in self.collect(
self,
document,
document.position_from_utf16(position),
new_name,
callback_filter=language_id_filter(document),
):
check_current_thread_canceled()

if isinstance(result, BaseException):
if not isinstance(result, CancelledError):
self._logger.exception(result, exc_info=result)
Expand All @@ -92,6 +90,8 @@ async def _text_document_rename(
return None

for we in edits:
check_current_thread_canceled()

if we.changes:
for uri, changes in we.changes.items():
if changes:
Expand All @@ -108,6 +108,8 @@ async def _text_document_rename(

result = WorkspaceEdit()
for we in edits:
check_current_thread_canceled()

if we.changes:
if result.changes is None:
result.changes = {}
Expand All @@ -127,7 +129,7 @@ async def _text_document_rename(

@rpc_method(name="textDocument/prepareRename", param_type=PrepareRenameParams)
@threaded
async def _text_document_prepare_rename(
def _text_document_prepare_rename(
self,
text_document: TextDocumentIdentifier,
position: Position,
Expand All @@ -140,9 +142,11 @@ async def _text_document_prepare_rename(
if document is None:
return None

for result in await self.collect_prepare(
for result in self.collect_prepare(
self, document, document.position_from_utf16(position), callback_filter=language_id_filter(document)
):
check_current_thread_canceled()

if isinstance(result, BaseException):
if isinstance(result, CantRenameError):
raise JsonRPCErrorException(ErrorCodes.INVALID_PARAMS, str(result))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)

from robot.parsing.model.statements import Statement
from robotcode.core.concurrent import threaded
from robotcode.core.concurrent import check_current_thread_canceled, threaded
from robotcode.core.event import event
from robotcode.core.lsp.types import FileEvent, Location, Position, Range, ReferenceContext, WatchKind
from robotcode.core.uri import Uri
Expand Down Expand Up @@ -132,6 +132,8 @@ def _find_references_in_workspace(
result: List[Location] = []

for doc in self.parent.documents.documents:
check_current_thread_canceled()

result.extend(func(doc, *args, **kwargs))
if result and stop_at_first:
break
Expand Down
Loading

0 comments on commit 15e409d

Please sign in to comment.