Skip to content

Commit

Permalink
Move window/logMessage handler to the rest of the notification handle…
Browse files Browse the repository at this point in the history
…rs in window.py
  • Loading branch information
rwols committed Apr 6, 2019
1 parent 977beee commit 12755c2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
11 changes: 11 additions & 0 deletions plugin/core/logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import traceback

try:
from typing import Optional, Dict, Any
assert Optional and Dict and Any
except ImportError:
pass


log_debug = False
log_exceptions = True

Expand Down Expand Up @@ -31,6 +38,10 @@ def server_log(*args) -> None:
printf(*args, prefix="server")


def log_window_message(params: 'Optional[Dict[str, Any]]') -> None:
server_log(params.get("message", "???") if params else "???")


def printf(*args, prefix='LSP'):
"""Print args to the console, prefixed by the plugin name."""
print(prefix + ":", *args)
41 changes: 11 additions & 30 deletions plugin/core/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
except ImportError:
pass

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

Expand Down Expand Up @@ -145,9 +145,9 @@ def receive_payload(self, message: str) -> None:
try:
if "method" in payload:
if "id" in payload:
self.request_handler(payload)
self.handle("request", payload, self._request_handlers, payload.get("id"))
else:
self.notification_handler(payload)
self.handle("notification", payload, self._notification_handlers)
elif "id" in payload:
self.response_handler(payload)
else:
Expand Down Expand Up @@ -186,36 +186,17 @@ def on_request(self, request_method: str, handler: 'Callable') -> None:
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", '')
def handle(self, typestr: str, message: 'Dict[str, Any]', handlers: 'Dict[str, Callable]', *args) -> None:
method = message.get("method", "")
params = message.get("params")
debug('<-- ' + method)
if self.settings.log_payloads and params:
debug(' ' + str(params))
if method in self._request_handlers:
handler = handlers.get(method)
if handler:
try:
self._request_handlers[method](params, request_id)
handler(params, *args)
except Exception as err:
exception_log("Error handling request " + method, err)
exception_log("Error handling {} {}".format(typestr, method), err)
else:
debug("Unhandled request", method)

def notification_handler(self, notification: 'Dict[str, Any]') -> None:
method = notification["method"]
params = notification.get("params")
if method == "window/logMessage":
debug('<-- ' + method)
server_log(params.get("message", "???") if params else "???")
return

debug('<-- ' + method)
if self.settings.log_payloads and params:
debug(' ' + str(params))
if method in self._notification_handlers:
try:
self._notification_handlers[method](params)
except Exception as err:
exception_log("Error handling notification " + method, err)
else:
debug("Unhandled notification:", method)
debug("Unhandled {}".format(typestr), method)
8 changes: 6 additions & 2 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .events import global_events
from .logging import debug
from .logging import debug, server_log
from .types import ClientStates, ClientConfig, WindowLike, ViewLike, LanguageConfig, config_supports_syntax
from .protocol import Notification, Response
from .sessions import Session
Expand Down Expand Up @@ -291,7 +291,7 @@ def notify_did_change(self, view: ViewLike):
class WindowManager(object):
def __init__(self, window: WindowLike, configs: ConfigRegistry, documents: DocumentHandler,
diagnostics: DiagnosticsHandler, session_starter: 'Callable', sublime: 'Any',
handler_dispatcher, on_closed: 'Optional[Callable]'=None) -> None:
handler_dispatcher, on_closed: 'Optional[Callable]' = None) -> None:

# to move here:
# configurations.py: window_client_configs and all references
Expand Down Expand Up @@ -452,6 +452,10 @@ def _handle_session_started(self, session, project_path, config):
"window/showMessage",
lambda params: self._sublime.message_dialog(params.get("message")))

client.on_notification(
"window/logMessage",
lambda params: server_log(params.get("message", "???") if params else "???"))

self._handlers.on_initialized(config.name, self._window, client)

client.send_notification(Notification.initialized())
Expand Down

0 comments on commit 12755c2

Please sign in to comment.