Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix "Error rewriting command" warning triggered on startup #2277

Merged
merged 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from .plugin.core.registry import LspNextDiagnosticCommand
from .plugin.core.registry import LspOpenLocationCommand
from .plugin.core.registry import LspPrevDiagnosticCommand
from .plugin.core.registry import LspRecheckSessionsCommand
from .plugin.core.registry import LspRestartServerCommand
from .plugin.core.registry import windows
from .plugin.core.sessions import AbstractPlugin
Expand Down
29 changes: 22 additions & 7 deletions plugin/core/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from .types import ClientConfig
from .typing import Generator, List, Optional, Set, Dict, Deque
from .workspace import enable_in_project, disable_in_project
from abc import ABCMeta
from abc import abstractmethod
from collections import deque
from datetime import datetime, timedelta
from weakref import WeakSet
import sublime
import urllib.parse

Expand All @@ -14,14 +17,25 @@
RETRY_COUNT_TIMEDELTA = timedelta(minutes=3)


class WindowConfigChangeListener(metaclass=ABCMeta):

@abstractmethod
def on_configs_changed(self, config_name: Optional[str] = None) -> None:
raise NotImplementedError()


class WindowConfigManager(object):
def __init__(self, window: sublime.Window, global_configs: Dict[str, ClientConfig]) -> None:
self._window = window
self._global_configs = global_configs
self._disabled_for_session = set() # type: Set[str]
self._crashes = {} # type: Dict[str, Deque[datetime]]
self.all = {} # type: Dict[str, ClientConfig]
self.update()
self._change_listeners = WeakSet() # type: WeakSet[WindowConfigChangeListener]
self._reload_configs()

def add_change_listener(self, listener: WindowConfigChangeListener) -> None:
self._change_listeners.add(listener)

def get_configs(self) -> List[ClientConfig]:
return sorted(self.all.values(), key=lambda config: config.name)
Expand All @@ -44,7 +58,7 @@ def match_view(self, view: sublime.View, include_disabled: bool = False) -> Gene
except (IndexError, RuntimeError):
pass

def update(self, updated_config_name: Optional[str] = None) -> None:
def _reload_configs(self, updated_config_name: Optional[str] = None) -> None:
project_settings = (self._window.project_data() or {}).get("settings", {}).get("LSP", {})
if updated_config_name is None:
self.all.clear()
Expand All @@ -67,7 +81,11 @@ def update(self, updated_config_name: Optional[str] = None) -> None:
self.all[name] = ClientConfig.from_dict(name, c)
except Exception as ex:
exception_log("failed to load project-only configuration {}".format(name), ex)
self._window.run_command("lsp_recheck_sessions", {'config_name': updated_config_name})

def update(self, updated_config_name: Optional[str] = None) -> None:
self._reload_configs(updated_config_name)
for listener in self._change_listeners:
listener.on_configs_changed(updated_config_name)

def enable_config(self, config_name: str) -> None:
if not self._reenable_disabled_for_session(config_name):
Expand All @@ -76,7 +94,7 @@ def enable_config(self, config_name: str) -> None:

def disable_config(self, config_name: str, only_for_session: bool = False) -> None:
if only_for_session:
self._disable_for_session(config_name)
self._disabled_for_session.add(config_name)
else:
disable_in_project(self._window, config_name)
self.update(config_name)
Expand All @@ -97,9 +115,6 @@ def record_crash(self, config_name: str, exit_code: int, exception: Optional[Exc
config_name, crash_count, RETRY_MAX_COUNT, RETRY_COUNT_TIMEDELTA.total_seconds(), exit_code, exception))
return crash_count < RETRY_MAX_COUNT

def _disable_for_session(self, config_name: str) -> None:
self._disabled_for_session.add(config_name)

def _reenable_disabled_for_session(self, config_name: str) -> bool:
try:
self._disabled_for_session.remove(config_name)
Expand Down
11 changes: 0 additions & 11 deletions plugin/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,6 @@ def run_async() -> None:
sublime.set_timeout_async(run_async)


class LspRecheckSessionsCommand(sublime_plugin.WindowCommand):
def run(self, config_name: Optional[str] = None) -> None:

def run_async() -> None:
wm = windows.lookup(self.window)
if wm:
wm.restart_sessions_async(config_name)

sublime.set_timeout_async(run_async)


def navigate_diagnostics(view: sublime.View, point: Optional[int], forward: bool = True) -> None:
try:
uri = uri_from_view(view)
Expand Down
13 changes: 11 additions & 2 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from ...third_party import WebsocketServer # type: ignore
from .configurations import WindowConfigManager, RETRY_MAX_COUNT, RETRY_COUNT_TIMEDELTA
from .configurations import RETRY_COUNT_TIMEDELTA
from .configurations import RETRY_MAX_COUNT
from .configurations import WindowConfigChangeListener
from .configurations import WindowConfigManager
from .diagnostics_storage import is_severity_included
from .logging import debug
from .logging import exception_log
Expand Down Expand Up @@ -62,7 +65,7 @@ def set_diagnostics_count(view: sublime.View, errors: int, warnings: int) -> Non
pass


class WindowManager(Manager):
class WindowManager(Manager, WindowConfigChangeListener):

def __init__(self, window: sublime.Window, workspace: ProjectFolders, config_manager: WindowConfigManager) -> None:
self._window = window
Expand All @@ -81,6 +84,7 @@ def __init__(self, window: sublime.Window, workspace: ProjectFolders, config_man
self.total_warning_count = 0
sublime.set_timeout(functools.partial(self._update_panel_main_thread, _NO_DIAGNOSTICS_PLACEHOLDER, []))
self.panel_manager.ensure_log_panel()
self._config_manager.add_change_listener(self)

@property
def window(self) -> sublime.Window:
Expand Down Expand Up @@ -482,6 +486,11 @@ def _update_panel_main_thread(self, characters: str, prephantoms: List[Tuple[int
phantoms.append(sublime.Phantom(region, "({})".format(make_link(href, code)), sublime.LAYOUT_INLINE))
self._panel_code_phantoms.update(phantoms)

# --- Implements WindowConfigChangeListener ------------------------------------------------------------------------

def on_configs_changed(self, config_name: Optional[str] = None) -> None:
sublime.set_timeout_async(lambda: self.restart_sessions_async(config_name))


class WindowRegistry:
def __init__(self) -> None:
Expand Down