Skip to content

Commit 9fa725a

Browse files
authored
Allow plugins to modify server response messages (#1992)
1 parent e7ca542 commit 9fa725a

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

plugin/core/sessions.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,17 @@ def on_pre_send_notification_async(self, notification: Notification) -> None:
875875
"""
876876
pass
877877

878+
def on_server_response_async(self, method: str, response: Response) -> None:
879+
"""
880+
Notifies about a response message that has been received from the language server.
881+
Only successful responses are passed to this method.
882+
883+
:param method: The method of the request.
884+
:param response: The response object to the request. The response.result field can be modified by the
885+
plugin, before it gets further handled by the LSP package.
886+
"""
887+
pass
888+
878889
def on_open_uri_async(self, uri: DocumentUri, callback: Callable[[str, str, str], None]) -> bool:
879890
"""
880891
Called when a language server reports to open an URI. If you know how to handle this URI, then return True and
@@ -1898,10 +1909,12 @@ def deduce_payload(
18981909
return res
18991910
elif "id" in payload:
19001911
response_id = int(payload["id"])
1901-
handler, result, is_error = self.response_handler(response_id, payload)
1902-
response_tuple = (handler, result, None, None, None)
1912+
handler, method, result, is_error = self.response_handler(response_id, payload)
19031913
self._logger.incoming_response(response_id, result, is_error)
1904-
return response_tuple
1914+
response = Response(response_id, result)
1915+
if self._plugin and not is_error:
1916+
self._plugin.on_server_response_async(method, response) # type: ignore
1917+
return handler, response.result, None, None, None
19051918
else:
19061919
debug("Unknown payload type: ", payload)
19071920
return (None, None, None, None, None)
@@ -1925,21 +1938,25 @@ def on_payload(self, payload: Dict[str, Any]) -> None:
19251938
except Exception as err:
19261939
exception_log("Error handling {}".format(typestr), err)
19271940

1928-
def response_handler(self, response_id: int, response: Dict[str, Any]) -> Tuple[Optional[Callable], Any, bool]:
1941+
def response_handler(
1942+
self,
1943+
response_id: int,
1944+
response: Dict[str, Any]
1945+
) -> Tuple[Optional[Callable], Optional[str], Any, bool]:
19291946
request, handler, error_handler = self._response_handlers.pop(response_id, (None, None, None))
19301947
if not request:
19311948
error = {"code": ErrorCode.InvalidParams, "message": "unknown response ID {}".format(response_id)}
1932-
return (print_to_status_bar, error, True)
1949+
return (print_to_status_bar, None, error, True)
19331950
self._invoke_views(request, "on_request_finished_async", response_id)
19341951
if "result" in response and "error" not in response:
1935-
return (handler, response["result"], False)
1952+
return (handler, request.method, response["result"], False)
19361953
if not error_handler:
19371954
error_handler = print_to_status_bar
19381955
if "result" not in response and "error" in response:
19391956
error = response["error"]
19401957
else:
19411958
error = {"code": ErrorCode.InvalidParams, "message": "invalid response payload"}
1942-
return (error_handler, error, True)
1959+
return (error_handler, request.method, error, True)
19431960

19441961
def _get_handler(self, method: str) -> Optional[Callable]:
19451962
return getattr(self, method2attr(method), None)

0 commit comments

Comments
 (0)