Skip to content

Commit

Permalink
Fixup syntax highlighting for the logs panel
Browse files Browse the repository at this point in the history
I needed to disambiguate between responses and notifications, because
request IDs can be any string! Solved this by using separate arrow
symbols for notifications/responses/requests. This way, we can
disambiguate based on the arrow symbol.
  • Loading branch information
rwols committed Mar 12, 2020
1 parent f333dc0 commit cee7ff9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 66 deletions.
101 changes: 48 additions & 53 deletions Syntaxes/ServerLog.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ hidden: true
scope: output.lsp.log

variables:
method: '[[:alpha:]][[:alnum:]/]*'
method: '[[:alnum:]/$]+'
servername: '[[:alnum:]_-]+'
id: '[^\s:]+'

contexts:
main:
Expand All @@ -22,65 +23,59 @@ contexts:
- match: '^::'
scope: punctuation.accessor.lsp
push:
- - meta_scope: meta.group.lsp
- match: $
pop: true
# responses
- - match: \d+
scope: constant.numeric.integer.decimal.lsp
set:
- match: ':'
scope: punctuation.separator.lsp
set: maybe-payload
- match: ''
pop: true
# notifications or requests
- match: (?=\w)
set:
# requests
- - match: \(
scope: punctuation.section.parens.begin.lsp
set:
- match: '[^\s)]+'
scope: constant.numeric.lsp
set:
- match: \)
scope: punctuation.section.parens.end.lsp
set:
- match: ':'
scope: punctuation.separator.lsp
set: maybe-payload
- match: ''
pop: true
# notifications
- match: ':'
scope: punctuation.separator.lsp
set: maybe-payload
- match: ''
pop: true
- - match: '{{method}}'
scope: keyword.control.lsp
pop: true
# language server name
- - match: \S+
scope: variable.function.lsp
pop: true
# arrows
- - match: -->
- meta_scope: meta.group.lsp
- match: (?:==|--)>
scope: storage.modifier.lsp
pop: true
set: [maybe-payload, request, server-name]
- match: ->
scope: storage.modifier.lsp
set: [maybe-payload, notification, server-name]
- match: '>>>'
scope: storage.modifier.lsp
set: [maybe-payload, response, server-name]
- match: <--
scope: storage.modifier.lsp
pop: true
- match: ==>
set: [maybe-payload, request, server-name]
- match: <-
scope: storage.modifier.lsp
pop: true
- match: unhandled
set: [maybe-payload, notification, server-name]
- match: <<<
scope: storage.modifier.lsp
set: [maybe-payload, response, server-name]
- match: <\?\?
scope: invalid.deprecated.lsp
pop: true
set: [maybe-payload, request, server-name]
- match: <\?
scope: invalid.deprecated.lsp
set: [maybe-payload, notification, server-name]

server-name:
- match: '{{servername}}'
scope: variable.function.lsp
pop: true

request:
- match: ({{method}})(\()({{id}})(\))
captures:
1: keyword.control.lsp
2: punctuation.section.parens.begin.lsp
3: constant.numeric.id.lsp
4: punctuation.section.parens.end.lsp
pop: true

notification:
- match: '{{method}}'
scope: keyword.control.lsp
pop: true

response:
- match: '{{id}}'
scope: constant.numeric.id.lsp
pop: true

maybe-payload:
- match: \s*(?=\S)
- match: ':'
scope: punctuation.separator.lsp
set:
- match: $
pop: true
Expand Down
20 changes: 7 additions & 13 deletions plugin/core/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ def try_terminate_process(process: subprocess.Popen) -> None:
pass # process can be terminated already


class Direction:
Incoming = '<--'
Outgoing = '-->'
OutgoingBlocking = '==>'


class PreformattedPayloadLogger:

def __init__(self, settings: Settings, server_name: str, sink: Callable[[str], None]) -> None:
Expand All @@ -54,12 +48,12 @@ def format_notification(self, direction: str, method: str) -> str:
def outgoing_response(self, request_id: Any, params: Any) -> None:
if not self.settings.log_debug:
return
self.log(self.format_response(Direction.Outgoing, request_id), params, self.settings.log_payloads)
self.log(self.format_response(">>>", request_id), params, self.settings.log_payloads)

def outgoing_request(self, request_id: int, method: str, params: Any, blocking: bool) -> None:
if not self.settings.log_debug:
return
direction = Direction.OutgoingBlocking if blocking else Direction.Outgoing
direction = "==>" if blocking else "-->"
self.log(self.format_request(direction, method, request_id), params, self.settings.log_payloads)

def outgoing_notification(self, method: str, params: Any) -> None:
Expand All @@ -72,23 +66,23 @@ def outgoing_notification(self, method: str, params: Any) -> None:
and method != "textDocument/didOpen"
if log_payload and method == "textDocument/didSave" and isinstance(params, dict) and "text" in params:
log_payload = False
self.log(self.format_notification(Direction.Outgoing, method), params, log_payload)
self.log(self.format_notification(" ->", method), params, log_payload)

def incoming_response(self, request_id: int, params: Any) -> None:
if not self.settings.log_debug:
return
self.log(self.format_response(Direction.Incoming, request_id), params, self.settings.log_payloads)
self.log(self.format_response("<<<", request_id), params, self.settings.log_payloads)

def incoming_request(self, request_id: Any, method: str, params: Any, unhandled: bool) -> None:
if not self.settings.log_debug:
return
direction = "unhandled" if unhandled else Direction.Incoming
direction = "<??" if unhandled else "<--"
self.log(self.format_request(direction, method, request_id), params, self.settings.log_payloads)

def incoming_notification(self, method: str, params: Any, unhandled: bool) -> None:
if not self.settings.log_debug or method == "window/logMessage":
return
direction = "unhandled" if unhandled else Direction.Incoming
direction = "<? " if unhandled else "<- "
self.log(self.format_notification(direction, method), params, self.settings.log_payloads)


Expand Down Expand Up @@ -249,7 +243,7 @@ def request_or_notification_handler(self, payload: Mapping[str, Any]) -> None:
request_id = payload.get("id")
if request_id is not None:
self.handle(request_id, method, params, "request", self._request_handlers,
lambda a, b, c: self.logger.incoming_request(request_id, a, b, c))
lambda *args: self.logger.incoming_request(request_id, *args))
else:
self.handle(None, method, params, "notification", self._notification_handlers,
self.logger.incoming_notification)
Expand Down

0 comments on commit cee7ff9

Please sign in to comment.