Skip to content

Commit

Permalink
Implement window/showMessageRequest (#470)
Browse files Browse the repository at this point in the history
* Show actions of "window/showMessageRequest" in a quick panel.
* Pass the client so that we can send the command.
* Fix indentation issues.
* Fix indent.
* Send response to LSP server with user choice.
* Fix lambda in "client.on_request".
  • Loading branch information
ayoub-benali authored and rwols committed Dec 2, 2018
1 parent 3dd22f9 commit 5f9b914
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ script:
- coverage run -m unittest discover
- sh travis.sh run_tests
after_success:
- coveralls
- coveralls
15 changes: 15 additions & 0 deletions plugin/core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ def to_payload(self, id) -> 'Dict[str, Any]':
return r


class Response:
def __init__(self, request_id: int, result: 'Dict[str, Any]') -> None:
self.request_id = request_id
self.result = result
self.jsonrpc = "2.0"

def to_payload(self) -> 'Dict[str, Any]':
r = {
"id": self.request_id,
"jsonrpc": self.jsonrpc,
"result": self.result
}
return r


class Notification:
def __init__(self, method: str, params: dict = {}) -> None:
self.method = method
Expand Down
8 changes: 6 additions & 2 deletions plugin/core/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
pass

from .logging import debug, exception_log, server_log
from .protocol import Request, Notification
from .protocol import Request, Notification, Response
from .types import Settings


Expand Down Expand Up @@ -95,6 +95,9 @@ def send_notification(self, notification: Notification) -> None:
debug(' --> ' + notification.method)
self.send_payload(notification.to_payload())

def send_response(self, response: Response) -> None:
self.send_payload(response.to_payload())

def exit(self) -> None:
self.exiting = True
self.send_notification(Notification.exit())
Expand Down Expand Up @@ -173,14 +176,15 @@ def on_notification(self, notification_method: str, handler: 'Callable') -> None
self._notification_handlers[notification_method] = handler

def request_handler(self, request: 'Dict[str, Any]') -> None:
request_id = request.get("id")
params = request.get("params", dict())
method = request.get("method", '')
debug('<-- ' + method)
if self.settings.log_payloads and params:
debug(' ' + str(params))
if method in self._request_handlers:
try:
self._request_handlers[method](params)
self._request_handlers[method](params, request_id)
except Exception as err:
exception_log("Error handling request " + method, err)
else:
Expand Down
2 changes: 1 addition & 1 deletion plugin/core/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_server_request(self):

client.on_request(
"ping",
lambda params: pings.append(params))
lambda params, request_id: pings.append(params))

transport.receive('{ "id": 1, "method": "ping"}')
self.assertEqual(len(pings), 1)
Expand Down
1 change: 0 additions & 1 deletion plugin/core/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ def read_stdout(self) -> None:

if (content_length > 0):
content = self.process.stdout.read(content_length)

self.on_receive(content.decode("UTF-8"))

except IOError as err:
Expand Down
21 changes: 14 additions & 7 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .events import global_events
from .logging import debug
from .types import ClientStates, ClientConfig, WindowLike, ViewLike, LanguageConfig, config_supports_syntax
from .protocol import Notification
from .protocol import Notification, Response
from .sessions import Session
from .url import filename_to_uri
from .workspace import get_project_path
from .rpc import Client
try:
from typing_extensions import Protocol
from typing import Optional, List, Callable, Dict, Any
Expand Down Expand Up @@ -382,12 +383,18 @@ def _start_client(self, config: ClientConfig):
debug("window {} added session {}".format(self._window.id(), config.name))
self._sessions[config.name] = session

def _handle_message_request(self, params: dict):
message = params.get("message", "(missing message)")
def _handle_message_request(self, params: dict, client: Client, request_id: int) -> None:
actions = params.get("actions", [])
addendum = "TODO: showMessageRequest with actions:"
titles = list(action.get("title") for action in actions)
self._sublime.message_dialog("\n".join([message, addendum] + titles))

def send_user_choice(index):
# otherwise noop; nothing was selected e.g. the user pressed escape
if index != -1:
response = Response(request_id, {"title": titles[index]})
client.send_response(response)

if actions:
self._sublime.active_window().show_quick_panel(titles, send_user_choice)

def restart_sessions(self):
self._restarting = True
Expand Down Expand Up @@ -424,11 +431,11 @@ def _handle_session_started(self, session, project_path, config):
# handle server requests and notifications
client.on_request(
"workspace/applyEdit",
lambda params: self._apply_workspace_edit(params))
lambda params, request_id: self._apply_workspace_edit(params))

client.on_request(
"window/showMessageRequest",
lambda params: self._handle_message_request(params))
lambda params, request_id: self._handle_message_request(params, client, request_id))

client.on_notification(
"textDocument/publishDiagnostics",
Expand Down

0 comments on commit 5f9b914

Please sign in to comment.