Skip to content

Commit

Permalink
chore: move Server.lsp to LanguageServer.lsp
Browse files Browse the repository at this point in the history
This is just for typing. It better refelcts the fact that
JSONRPCProtocol takes LanguageServer as an argument not Server
  • Loading branch information
tombh committed Sep 23, 2023
1 parent c8e7906 commit 06584d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
49 changes: 23 additions & 26 deletions pygls/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@
# limitations under the License. #
############################################################################
from __future__ import annotations

import asyncio
import enum
import functools
import json
import logging
import re
import sys
import uuid
import traceback
import uuid
from collections import namedtuple
from concurrent.futures import Future
from functools import lru_cache, partial
from itertools import zip_longest
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Expand All @@ -37,44 +39,27 @@
Type,
TypeVar,
Union,
TYPE_CHECKING,
)

from pygls.server import WebSocketTransportAdapter

if TYPE_CHECKING:
from pygls.server import LanguageServer
from pygls.server import LanguageServer, WebSocketTransportAdapter


import attrs
from cattrs.errors import ClassValidationError
from lsprotocol import converters

from pygls.capabilities import ServerCapabilitiesBuilder
from pygls.constants import ATTR_FEATURE_TYPE
from pygls.exceptions import (
JsonRpcException,
JsonRpcInternalError,
JsonRpcInvalidParams,
JsonRpcMethodNotFound,
JsonRpcRequestCancelled,
FeatureNotificationError,
FeatureRequestError,
)
from pygls.feature_manager import FeatureManager, assign_help_attrs, is_thread_function
from pygls.lsp import ConfigCallbackType, ShowDocumentCallbackType
from lsprotocol.types import (
CANCEL_REQUEST,
CLIENT_REGISTER_CAPABILITY,
CLIENT_UNREGISTER_CAPABILITY,
EXIT,
INITIALIZE,
INITIALIZED,
LOG_TRACE,
METHOD_TO_TYPES,
NOTEBOOK_DOCUMENT_DID_CHANGE,
NOTEBOOK_DOCUMENT_DID_CLOSE,
NOTEBOOK_DOCUMENT_DID_OPEN,
LOG_TRACE,
SET_TRACE,
SHUTDOWN,
TEXT_DOCUMENT_DID_CHANGE,
Expand All @@ -90,9 +75,6 @@
WORKSPACE_DID_CHANGE_WORKSPACE_FOLDERS,
WORKSPACE_EXECUTE_COMMAND,
WORKSPACE_SEMANTIC_TOKENS_REFRESH,
PositionEncodingKind,
)
from lsprotocol.types import (
ApplyWorkspaceEditParams,
Diagnostic,
DidChangeNotebookDocumentParams,
Expand All @@ -105,9 +87,11 @@
ExecuteCommandParams,
InitializeParams,
InitializeResult,
InitializeResultServerInfoType,
LogMessageParams,
LogTraceParams,
MessageType,
PositionEncodingKind,
PublishDiagnosticsParams,
RegistrationParams,
ResponseErrorMessage,
Expand All @@ -116,12 +100,25 @@
ShowMessageParams,
TraceValues,
UnregistrationParams,
WorkDoneProgressCancelParams,
WorkspaceApplyEditResponse,
WorkspaceEdit,
InitializeResultServerInfoType,
WorkspaceConfigurationParams,
WorkDoneProgressCancelParams,
WorkspaceEdit,
)

from pygls.capabilities import ServerCapabilitiesBuilder
from pygls.constants import ATTR_FEATURE_TYPE
from pygls.exceptions import (
FeatureNotificationError,
FeatureRequestError,
JsonRpcException,
JsonRpcInternalError,
JsonRpcInvalidParams,
JsonRpcMethodNotFound,
JsonRpcRequestCancelled,
)
from pygls.feature_manager import FeatureManager, assign_help_attrs, is_thread_function
from pygls.lsp import ConfigCallbackType, ShowDocumentCallbackType
from pygls.uris import from_fs_path
from pygls.workspace.workspace import Workspace

Expand Down
12 changes: 4 additions & 8 deletions pygls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
Union,
)

import cattrs
from pygls import IS_PYODIDE
from pygls.lsp import ConfigCallbackType, ShowDocumentCallbackType
from pygls.exceptions import (
Expand Down Expand Up @@ -190,10 +189,11 @@ class Server:
"""

lsp: LanguageServerProtocol

def __init__(
self,
protocol_cls: Type[JsonRPCProtocol],
converter_factory: Callable[[], cattrs.Converter],
loop: Optional[asyncio.AbstractEventLoop] = None,
max_workers: int = 2,
sync_kind: TextDocumentSyncKind = TextDocumentSyncKind.Incremental,
Expand All @@ -219,9 +219,6 @@ def __init__(

self.loop = loop

# TODO: Will move this to `LanguageServer` in next commit
self.lsp = protocol_cls(self, converter_factory()) # type: ignore

def shutdown(self):
"""Shutdown server."""
logger.info("Shutting down the server")
Expand Down Expand Up @@ -390,8 +387,6 @@ class LanguageServer(Server):
Advertise :lsp:`NotebookDocument` support to the client.
"""

lsp: LanguageServerProtocol

default_error_message = (
"Unexpected error in LSP server, see server's logs for details"
)
Expand Down Expand Up @@ -421,7 +416,8 @@ def __init__(
self._text_document_sync_kind = text_document_sync_kind
self._notebook_document_sync = notebook_document_sync
self.process_id: Optional[Union[int, None]] = None
super().__init__(protocol_cls, converter_factory, loop, max_workers)
self.lsp = protocol_cls(self, converter_factory())
super().__init__(protocol_cls, loop, max_workers)

def apply_edit(
self, edit: WorkspaceEdit, label: Optional[str] = None
Expand Down

0 comments on commit 06584d2

Please sign in to comment.