Skip to content

Commit 991f281

Browse files
authored
Refactor transports (#2851)
1 parent edf0f43 commit 991f281

File tree

9 files changed

+448
-331
lines changed

9 files changed

+448
-331
lines changed

LSP.sublime-settings

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,18 @@
324324
// // file on disk.
325325
// "schemes": ["file", "buffer", "res"],
326326
//
327-
// // When you want to connect to the language server via TCP (on
328-
// // localhost), specify the port here. If you put a value of 0 here,
329-
// // then LSP will select a free port number on localhost. In that case,
330-
// // you can use the string templates $port or ${port} in the "command".
331-
// // The syntax "{port}" is also allowed, but deprecated in favor of
332-
// // $port and ${port}.
327+
// // When set to a positive number bigger than 0, specifies the TCP port to
328+
// // use to connect to the language server process listening on the given
329+
// // port. When set to zero, a free TCP port is chosen. Chosen TCP port
330+
// // number can be accessed through a template variable, i.e. as `${port}`
331+
// // in the "command"`.
332+
// //
333+
// // Set to a negative number to make the LSP client act as TCP server
334+
// // awaiting connection from the LSP server. Using `-1` opens a random port.
335+
// // To use a fixed port number, use `-X` as the value for `tcp_port`, where
336+
// // `X` is the desired (positive) port number.
337+
// //
338+
// // If not specified, STDIO is used as the transport.
333339
// "tcp_port": 1234,
334340
//
335341
// // Sent to server once using workspace/didChangeConfiguration

plugin/core/sessions.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@
111111
from .protocol import ResponseError
112112
from .settings import globalprefs
113113
from .settings import userprefs
114-
from .transports import Transport
115114
from .transports import TransportCallbacks
115+
from .transports import TransportWrapper
116116
from .types import Capabilities
117117
from .types import ClientConfig
118118
from .types import ClientStates
@@ -958,15 +958,15 @@ def check_applicable(self, sb: SessionBufferProtocol, *, suppress_requests: bool
958958
_PARTIAL_RESULT_PROGRESS_PREFIX = "$ublime-partial-result-progress-"
959959

960960

961-
class Session(APIHandler, TransportCallbacks['dict[str, Any]']):
961+
class Session(APIHandler, TransportCallbacks):
962962

963963
def __init__(self, manager: Manager, logger: Logger, workspace_folders: list[WorkspaceFolder],
964964
config: ClientConfig, plugin_class: type[AbstractPlugin] | None) -> None:
965-
self.transport: Transport | None = None
965+
self.transport: TransportWrapper | None = None
966966
self.working_directory: str | None = None
967967
self.request_id = 0 # Our request IDs are always integers.
968968
self._logger = logger
969-
self._response_handlers: dict[int, tuple[Request[Any, Any], Callable[[Any], None], Callable[[ResponseError], None]]] = {} # noqa: E501
969+
self._response_handlers: dict[str | int, tuple[Request[Any, Any], Callable[[Any], None], Callable[[ResponseError], None]]] = {} # noqa: E501
970970
self.config = config
971971
self.config_status_message = ''
972972
self.manager = weakref.ref(manager)
@@ -1238,7 +1238,7 @@ def initialize_async(
12381238
self,
12391239
variables: dict[str, str],
12401240
working_directory: str | None,
1241-
transport: Transport,
1241+
transport: TransportWrapper,
12421242
init_callback: InitCallback
12431243
) -> None:
12441244
self.transport = transport
@@ -2046,7 +2046,8 @@ def on_progress(self, params: ProgressParams) -> None:
20462046
# call window/workDoneProgress/create before hand. In that case, we check the 'kind' field of the
20472047
# progress data. If the 'kind' field is 'begin', we set up a progress reporter anyway.
20482048
try:
2049-
request_id = int(token[len(_WORK_DONE_PROGRESS_PREFIX):]) # type: ignore
2049+
token = str(token)
2050+
request_id = int(token[len(_WORK_DONE_PROGRESS_PREFIX):])
20502051
request = self._response_handlers[request_id][0]
20512052
self._invoke_views(request, "on_request_progress", request_id, params)
20522053
return
@@ -2196,21 +2197,20 @@ def send_error_response(self, request_id: int | str, error: Error) -> None:
21962197

21972198
def exit(self) -> None:
21982199
self.send_notification(Notification.exit())
2199-
try:
2200-
self.transport.close() # type: ignore
2201-
except AttributeError:
2202-
pass
2200+
if self.transport:
2201+
self.transport.close()
2202+
self.transport = None
22032203

22042204
def send_payload(self, payload: JSONRPCMessage) -> None:
22052205
try:
2206-
self.transport.send(payload) # type: ignore
2206+
self.transport.send(payload) # pyright: ignore[reportOptionalMemberAccess]
22072207
except AttributeError:
22082208
pass
22092209

22102210
def deduce_payload(
22112211
self,
2212-
payload: dict[str, Any]
2213-
) -> tuple[Callable | None, Any, int | None, str | None, str | None]:
2212+
payload: JSONRPCMessage
2213+
) -> tuple[Callable | None, Any, str | int | None, str | None, str | None]:
22142214
if "method" in payload:
22152215
method = payload["method"]
22162216
handler = self._get_handler(method)
@@ -2230,7 +2230,7 @@ def deduce_payload(
22302230
return res
22312231
elif "id" in payload:
22322232
response_id = payload["id"]
2233-
if response_id is None:
2233+
if response_id is None: # pyright: ignore[reportUnnecessaryComparison]
22342234
self._logger.incoming_response('<missing>', payload.get("error"), True)
22352235
return (None, None, None, None, None)
22362236
handler, method, result, is_error = self.response_handler(response_id, payload)
@@ -2240,10 +2240,10 @@ def deduce_payload(
22402240
self._plugin.on_server_response_async(method, response) # type: ignore
22412241
return handler, response.result, None, None, None
22422242
else:
2243-
debug("Unknown payload type: ", payload)
2243+
debug("Unknown payload type: ", payload) # pyright: ignore[reportUnreachable]
22442244
return (None, None, None, None, None)
22452245

2246-
def on_payload(self, payload: dict[str, Any]) -> None:
2246+
def on_payload(self, payload: JSONRPCMessage) -> None:
22472247
handler, result, req_id, typestr, _method = self.deduce_payload(payload)
22482248
if handler:
22492249
result_promise: Promise[Response[Any]] | None = None
@@ -2268,7 +2268,7 @@ def on_payload(self, payload: dict[str, Any]) -> None:
22682268
result_promise.then(self.send_response)
22692269

22702270
def response_handler(
2271-
self, response_id: int, response: dict[str, Any]
2271+
self, response_id: str | int, response: JSONRPCMessage
22722272
) -> tuple[Callable[[ResponseError], None], str | None, Any, bool]:
22732273
matching_handler = self._response_handlers.pop(response_id)
22742274
if not matching_handler:

0 commit comments

Comments
 (0)