Skip to content

Commit

Permalink
Use client's window when handling diagnostics and edits
Browse files Browse the repository at this point in the history
Fixes #251
  • Loading branch information
tomv564 committed Feb 4, 2018
1 parent 3bc9cca commit bf6a5b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
32 changes: 18 additions & 14 deletions plugin/core/diagnostics.py
Expand Up @@ -34,35 +34,39 @@ def update_file_diagnostics(window: sublime.Window, file_path: str, source: str,


class DiagnosticsUpdate(object):

def __init__(self, file_path: str, diagnostics: 'List[Diagnostic]') -> 'None':
def __init__(self, window: sublime.Window, client_name: str,
file_path: str, diagnostics: 'List[Diagnostic]') -> 'None':
self.window = window
self.client_name = client_name
self.file_path = file_path
self.diagnostics = diagnostics


def handle_diagnostics(update: 'Any'):
file_path = uri_to_filename(update.get('uri'))
window = sublime.active_window()
def handle_client_diagnostics(window: sublime.Window, client_name: str, update: dict):
maybe_file_uri = update.get('uri')
if maybe_file_uri is not None:
file_path = uri_to_filename(maybe_file_uri)

diagnostics = list(
Diagnostic.from_lsp(item) for item in update.get('diagnostics', []))
diagnostics = list(
Diagnostic.from_lsp(item) for item in update.get('diagnostics', []))

origin = 'lsp' # TODO: use actual client name to be able to update diagnostics per client
update_file_diagnostics(window, file_path, origin, diagnostics)
Events.publish("document.diagnostics", DiagnosticsUpdate(file_path, diagnostics))
# TODO: expose updates to features
update_file_diagnostics(window, file_path, client_name, diagnostics)
Events.publish("document.diagnostics", DiagnosticsUpdate(window, client_name, file_path, diagnostics))
else:
debug('missing uri in diagnostics update')
# TODO: expose updates to features


def remove_diagnostics(view: sublime.View):
def remove_diagnostics(view: sublime.View, client_name: str):
"""Removes diagnostics for a file if no views exist for it
"""
window = sublime.active_window()

file_path = view.file_name()
if file_path:
if not window.find_open_file(file_path):
update_file_diagnostics(window, file_path, 'lsp', [])
Events.publish("document.diagnostics", DiagnosticsUpdate(file_path, []))
update_file_diagnostics(window, file_path, client_name, [])
Events.publish("document.diagnostics", DiagnosticsUpdate(window, client_name, file_path, []))
else:
debug('file still open?')

Expand Down
10 changes: 6 additions & 4 deletions plugin/core/main.py
Expand Up @@ -32,7 +32,7 @@
from .documents import (
initialize_document_sync, notify_did_open, clear_document_states
)
from .diagnostics import handle_diagnostics, remove_diagnostics
from .diagnostics import handle_client_diagnostics, remove_diagnostics
from .edit import apply_workspace_edit


Expand Down Expand Up @@ -120,18 +120,20 @@ def handle_initialize_result(result, client, window, config):
# handle server requests and notifications
client.on_request(
"workspace/applyEdit",
lambda params: apply_workspace_edit(sublime.active_window(), params))
lambda params: apply_workspace_edit(window, params))

client.on_request(
"window/showMessageRequest",
lambda params: handle_message_request(params))

client.on_notification(
"textDocument/publishDiagnostics",
lambda params: handle_diagnostics(params))
lambda params: handle_client_diagnostics(window, config.name, params))

client.on_notification(
"window/showMessage",
lambda params: sublime.message_dialog(params.get("message")))

if settings.log_server:
client.on_notification(
"window/logMessage",
Expand All @@ -147,7 +149,7 @@ def handle_initialize_result(result, client, window, config):
if document_sync:
initialize_document_sync(document_sync)

Events.subscribe('view.on_close', remove_diagnostics)
Events.subscribe('view.on_close', lambda view: remove_diagnostics(view, config.name))

client.send_notification(Notification.initialized())
if config.settings:
Expand Down
2 changes: 1 addition & 1 deletion plugin/diagnostics.py
Expand Up @@ -167,7 +167,7 @@ def update_diagnostics_in_view(view: sublime.View, diagnostics: 'List[Diagnostic


def handle_diagnostics(update: DiagnosticsUpdate):
window = sublime.active_window()
window = update.window
view = window.find_open_file(update.file_path)
if view:
update_diagnostics_in_view(view, update.diagnostics)
Expand Down

0 comments on commit bf6a5b4

Please sign in to comment.