Skip to content

Commit

Permalink
refactor(langserver): remove HasExtendCapabilities protocol and imple…
Browse files Browse the repository at this point in the history
…ment the method directly, extend threaded decorator
  • Loading branch information
d-biehl committed Dec 30, 2023
1 parent 2fae8e3 commit b76eb0f
Show file tree
Hide file tree
Showing 30 changed files with 98 additions and 130 deletions.
17 changes: 15 additions & 2 deletions packages/core/src/robotcode/core/utils/threading.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
from concurrent.futures import CancelledError, Future
from threading import Thread, current_thread, local
from typing import Any, Callable, Dict, Generic, Optional, Tuple, TypeVar, cast
from typing import Any, Callable, Dict, Generic, Optional, Tuple, TypeVar, cast, overload

_F = TypeVar("_F", bound=Callable[..., Any])
_TResult = TypeVar("_TResult")
Expand All @@ -22,11 +22,24 @@ def result(self, timeout: Optional[float] = None) -> _TResult:
return cast(_TResult, super().result(timeout))


def threaded(enabled: bool = True) -> Callable[[_F], _F]:
@overload
def threaded(__func: _F) -> _F:
...


@overload
def threaded(*, enabled: bool = True) -> Callable[[_F], _F]:
...


def threaded(__func: _F = None, *, enabled: bool = True) -> Callable[[_F], _F]:
def decorator(func: _F) -> _F:
setattr(func, __THREADED_MARKER, enabled)
return func

if __func is not None:
return decorator(__func)

return decorator


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import CODE_ACTION_KINDS_ATTR, HasCodeActionKinds, language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

if TYPE_CHECKING:
from robotcode.language_server.common.protocol import LanguageServerProtocol


class CodeActionProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class CodeActionProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
Expand Down Expand Up @@ -61,7 +60,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
)

@rpc_method(name="textDocument/codeAction", param_type=CodeActionParams)
@threaded()
@threaded
async def _text_document_code_action(
self,
text_document: TextDocumentIdentifier,
Expand Down Expand Up @@ -102,7 +101,7 @@ async def _text_document_code_action(
return results

@rpc_method(name="codeAction/resolve", param_type=CodeAction)
@threaded()
@threaded
async def _text_document_code_action_resolve(
self,
params: CodeAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.text_document import TextDocument

from .protocol_part import LanguageServerProtocolPart
Expand All @@ -25,7 +24,7 @@
from robotcode.language_server.common.protocol import LanguageServerProtocol


class CodeLensProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class CodeLensProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
Expand All @@ -45,7 +44,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
capabilities.code_lens_provider = CodeLensOptions(resolve_provider=True if len(self.resolve) > 0 else None)

@rpc_method(name="textDocument/codeLens", param_type=CodeLensParams)
@threaded()
@threaded
async def _text_document_code_lens(
self, text_document: TextDocumentIdentifier, *args: Any, **kwargs: Any
) -> Optional[List[CodeLens]]:
Expand All @@ -71,7 +70,7 @@ async def _text_document_code_lens(
return results

@rpc_method(name="codeLens/resolve", param_type=CodeLens)
@threaded()
@threaded
async def _code_lens_resolve(self, params: CodeLens, *args: Any, **kwargs: Any) -> CodeLens:
results: List[CodeLens] = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import JsonRPCErrorException, rpc_method
from robotcode.language_server.common.decorators import get_command_id, is_command
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart

if TYPE_CHECKING:
Expand All @@ -34,7 +33,7 @@ class CommandEntry:
callback: _FUNC_TYPE


class CommandsProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class CommandsProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

PREFIX: Final = f"{uuid.uuid4()}"
Expand Down Expand Up @@ -72,7 +71,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
capabilities.execute_command_provider = ExecuteCommandOptions(list(self.commands.keys()))

@rpc_method(name="workspace/executeCommand", param_type=ExecuteCommandParams)
@threaded()
@threaded
async def _workspace_execute_command(
self, command: str, arguments: Optional[List[LSPAny]], *args: Any, **kwargs: Any
) -> Optional[LSPAny]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
HasTriggerCharacters,
language_id_filter,
)
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

if TYPE_CHECKING:
from robotcode.language_server.common.protocol import LanguageServerProtocol


class CompletionProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class CompletionProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
Expand Down Expand Up @@ -84,7 +83,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
)

@rpc_method(name="textDocument/completion", param_type=CompletionParams)
@threaded()
@threaded
async def _text_document_completion(
self,
text_document: TextDocumentIdentifier,
Expand Down Expand Up @@ -155,7 +154,7 @@ def update_completion_item_to_utf16(self, document: TextDocument, item: Completi
item.text_edit.replace = document.range_to_utf16(item.text_edit.replace)

@rpc_method(name="completionItem/resolve", param_type=CompletionItem)
@threaded()
@threaded
async def _completion_item_resolve(
self,
params: CompletionItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from asyncio import CancelledError
from typing import TYPE_CHECKING, Any, Final, List, Optional, Union

from robotcode.core.async_tools import async_tasking_event
from robotcode.core.event import event
from robotcode.core.lsp.types import (
DeclarationParams,
Location,
Expand All @@ -13,28 +13,25 @@
TextDocumentIdentifier,
)
from robotcode.core.utils.logging import LoggingDescriptor
from robotcode.core.utils.threading import threaded
from robotcode.core.utils.threading import check_thread_canceled, threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import (
HasExtendCapabilities,
)
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

if TYPE_CHECKING:
from robotcode.language_server.common.protocol import LanguageServerProtocol


class DeclarationProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class DeclarationProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

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

@async_tasking_event
async def collect(
@event
def collect(
sender, document: TextDocument, position: Position # NOSONAR
) -> Union[Location, List[Location], List[LocationLink], None]:
...
Expand All @@ -51,8 +48,8 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
capabilities.declaration_provider = True

@rpc_method(name="textDocument/declaration", param_type=DeclarationParams)
@threaded()
async def _text_document_declaration(
@threaded
def _text_document_declaration(
self,
text_document: TextDocumentIdentifier,
position: Position,
Expand All @@ -66,7 +63,9 @@ async def _text_document_declaration(
if document is None:
return None

for result in await self.collect(self, document, position, callback_filter=language_id_filter(document)):
for result in self.collect(self, document, position, callback_filter=language_id_filter(document)):
check_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
Expand Up @@ -16,15 +16,14 @@
from robotcode.core.utils.threading import check_thread_canceled, threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

if TYPE_CHECKING:
from robotcode.language_server.common.protocol import LanguageServerProtocol


class DefinitionProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class DefinitionProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
Expand All @@ -49,7 +48,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
capabilities.definition_provider = True

@rpc_method(name="textDocument/definition", param_type=DefinitionParams)
@threaded()
@threaded
def _text_document_definition(
self, text_document: TextDocumentIdentifier, position: Position, *args: Any, **kwargs: Any
) -> Optional[Union[Location, List[Location], List[LocationLink]]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import JsonRPCErrorException, rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

Expand Down Expand Up @@ -107,7 +106,7 @@ def _cancel_all_tasks(loop: asyncio.AbstractEventLoop) -> None:
)


class DiagnosticsProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class DiagnosticsProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, protocol: LanguageServerProtocol) -> None:
Expand Down Expand Up @@ -279,7 +278,7 @@ async def force_refresh_document(self, document: TextDocument, refresh: bool = T
await self.refresh()

@_logger.call
@threaded()
@threaded
def on_did_close(self, sender: Any, document: TextDocument) -> None:
create_sub_task(self._close_diagnostics_for_document(document), loop=self.diagnostics_loop)

Expand Down Expand Up @@ -500,7 +499,7 @@ def update_document_diagnostics(self, sender: Any, document: TextDocument) -> No
self.create_document_diagnostics_task(document, True)

@rpc_method(name="textDocument/diagnostic", param_type=DocumentDiagnosticParams)
@threaded()
@threaded
async def _text_document_diagnostic(
self,
text_document: TextDocumentIdentifier,
Expand Down Expand Up @@ -536,7 +535,7 @@ def get_diagnostics_data(self, document: TextDocument) -> DiagnosticsData:
return data

@rpc_method(name="workspace/diagnostic", param_type=WorkspaceDiagnosticParams)
@threaded()
@threaded
async def _workspace_diagnostic(
self,
identifier: Optional[str],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

if TYPE_CHECKING:
from robotcode.language_server.common.protocol import LanguageServerProtocol


class DocumentHighlightProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class DocumentHighlightProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
Expand All @@ -41,7 +40,7 @@ async def collect(
...

@rpc_method(name="textDocument/documentHighlight", param_type=DocumentHighlightParams)
@threaded()
@threaded
async def _text_document_document_highlight(
self,
text_document: TextDocumentIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

Expand All @@ -54,7 +53,7 @@ def decorator(func: _F) -> _F:
return decorator


class DocumentSymbolsProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class DocumentSymbolsProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: LanguageServerProtocol) -> None:
Expand Down Expand Up @@ -97,7 +96,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
capabilities.document_symbol_provider = True

@rpc_method(name="textDocument/documentSymbol", param_type=DocumentSymbolParams)
@threaded()
@threaded
async def _text_document_symbol(
self, text_document: TextDocumentIdentifier, *args: Any, **kwargs: Any
) -> Optional[Union[List[DocumentSymbol], List[SymbolInformation], None]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
from robotcode.core.utils.threading import threaded
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.decorators import language_id_filter
from robotcode.language_server.common.has_extend_capabilities import HasExtendCapabilities
from robotcode.language_server.common.parts.protocol_part import LanguageServerProtocolPart
from robotcode.language_server.common.text_document import TextDocument

if TYPE_CHECKING:
from robotcode.language_server.common.protocol import LanguageServerProtocol # pragma: no cover


class FoldingRangeProtocolPart(LanguageServerProtocolPart, HasExtendCapabilities):
class FoldingRangeProtocolPart(LanguageServerProtocolPart):
_logger: Final = LoggingDescriptor()

def __init__(self, parent: "LanguageServerProtocol") -> None:
Expand All @@ -36,7 +35,7 @@ def extend_capabilities(self, capabilities: ServerCapabilities) -> None:
capabilities.folding_range_provider = True

@rpc_method(name="textDocument/foldingRange", param_type=FoldingRangeParams)
@threaded()
@threaded
def _text_document_folding_range(
self, text_document: TextDocumentIdentifier, *args: Any, **kwargs: Any
) -> Optional[List[FoldingRange]]:
Expand Down
Loading

0 comments on commit b76eb0f

Please sign in to comment.