Skip to content

Commit

Permalink
trigger "on_server_response_async" also for "initialize" response (#2172
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rchl authored Jan 22, 2023
1 parent bf5a1f2 commit 3dd72e4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
python-version: '3.8'
- run: sudo apt update
- run: sudo apt install --no-install-recommends -y x11-xserver-utils
- run: pip3 install mypy==0.971 flake8==5.0.4 pyright==1.1.285 yapf==0.31.0 --user
- run: pip3 install mypy==0.971 flake8==5.0.4 pyright==1.1.289 yapf==0.31.0 --user
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
# - run: mypy -p plugin
- run: flake8 plugin tests
Expand Down
9 changes: 6 additions & 3 deletions plugin/core/protocol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .typing import Enum, IntEnum, IntFlag, StrEnum
from .typing import Any, Dict, Iterable, List, Literal, Mapping, NotRequired, Optional, TypedDict, Union
from .typing import Any, Dict, Generic, Iterable, List, Literal, Mapping, NotRequired, Optional, TypedDict, TypeVar, Union # noqa: E501
import sublime

INT_MAX = 2**31 - 1
Expand Down Expand Up @@ -5997,11 +5997,14 @@ def from_exception(cls, ex: Exception) -> 'Error':
return Error(ErrorCodes.InternalError, str(ex))


class Response:
T = TypeVar('T', bound=Union[None, bool, int, Uint, float, str, Mapping[str, Any], Iterable[Any]])


class Response(Generic[T]):

__slots__ = ('request_id', 'result')

def __init__(self, request_id: Any, result: Union[None, Mapping[str, Any], Iterable[Any]]) -> None:
def __init__(self, request_id: Any, result: T) -> None:
self.request_id = request_id
self.result = result

Expand Down
9 changes: 7 additions & 2 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
from .protocol import FailureHandlingKind
from .protocol import FileEvent
from .protocol import GeneralClientCapabilities
from .protocol import InitializeError
from .protocol import InitializeParams
from .protocol import InitializeResult
from .protocol import InsertTextMode
from .protocol import Location
from .protocol import LocationLink
Expand Down Expand Up @@ -1387,13 +1389,16 @@ def initialize_async(
self.send_request_async(
Request.initialize(params), self._handle_initialize_success, self._handle_initialize_error)

def _handle_initialize_success(self, result: Any) -> None:
def _handle_initialize_success(self, result: InitializeResult) -> None:
self.capabilities.assign(result.get('capabilities', dict()))
if self._workspace_folders and not self._supports_workspace_folders():
self._workspace_folders = self._workspace_folders[:1]
self.state = ClientStates.READY
if self._plugin_class is not None:
self._plugin = self._plugin_class(weakref.ref(self))
# We've missed calling the "on_server_response_async" API as plugin was not created yet.
# Handle it now and use fake request ID since it shouldn't matter.
self._plugin.on_server_response_async('initialize', Response(-1, result))
self.send_notification(Notification.initialized())
self._maybe_send_did_change_configuration()
execute_commands = self.get_capability('executeCommandProvider.commands')
Expand Down Expand Up @@ -1421,7 +1426,7 @@ def _handle_initialize_success(self, result: Any) -> None:
self._init_callback(self, False)
self._init_callback = None

def _handle_initialize_error(self, result: Any) -> None:
def _handle_initialize_error(self, result: InitializeError) -> None:
self._initialize_error = (result.get('code', -1), Exception(result.get('message', 'Error initializing server')))
# Init callback called after transport is closed to avoid pre-mature GC of Session.
self.end_async()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ per-file-ignores =
deps =
; mypy==0.971
flake8==5.0.4
pyright==1.1.285
pyright==1.1.289
commands =
# mypy disabled as it doesn't currently support cyclic definitions - https://github.com/python/mypy/issues/731
; mypy plugin
Expand Down

0 comments on commit 3dd72e4

Please sign in to comment.