From b0ec6a677fde1aa5aa4f4b74a09ba375d17e9b2b Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Wed, 27 Nov 2024 13:28:37 +0700 Subject: [PATCH] consolidate error notification into 1 function with an extra severe argument to downgrade to a non-error modal dialog if needed --- plugin/core/logging_notify.py | 24 +++++++----------------- plugin/core/windows.py | 4 ++-- plugin/core/workspace.py | 6 +++--- plugin/execute_command.py | 4 ++-- plugin/tooling.py | 3 +-- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/plugin/core/logging_notify.py b/plugin/core/logging_notify.py index 746391814..0aa29f1e4 100644 --- a/plugin/core/logging_notify.py +++ b/plugin/core/logging_notify.py @@ -2,28 +2,18 @@ import sublime from .settings import userprefs - -def notify(window: sublime.Window | None, message: str, status_message: str = 'LSP: see console log…') -> None: - """Pick either of the 2 ways to show a user notification message: - - via a detailed console message and a short status message - - via a blocking modal dialog""" - if not window: - return - if userprefs().suppress_error_dialogs: - window.status_message(status_message) - print(message) - else: - sublime.message_dialog(message) - - -def notify_error(window: sublime.Window | None, message: str, status_message: str = '❗LSP: see console log…') -> None: +def notify_error(window: sublime.Window | None, message: str, status_message: str = 'LSP: see console log…', + severe: bool = True) -> None: """Pick either of the 2 ways to show a user error notification message: - via a detailed console message and a short status message - - via a blocking error modal dialog""" + - via a blocking error modal dialog (with severe=false non-error modal dialog)""" if not window: return if userprefs().suppress_error_dialogs: window.status_message(status_message) print(message) else: - sublime.error_message(message) + if severe: + sublime.error_message(message) + else: + sublime.message_dialog(message) diff --git a/plugin/core/windows.py b/plugin/core/windows.py index a5d0f1d63..dcffe7da7 100644 --- a/plugin/core/windows.py +++ b/plugin/core/windows.py @@ -7,7 +7,7 @@ from .diagnostics_storage import is_severity_included from .logging import debug from .logging import exception_log -from .logging_notify import notify +from .logging_notify import notify_error from .message_request_handler import MessageRequestHandler from .panels import LOG_LINES_LIMIT_SETTING_NAME from .panels import MAX_LOG_LINES_LIMIT_OFF @@ -302,7 +302,7 @@ def start_async(self, config: ClientConfig, initiating_view: sublime.View) -> No print("Server output:\n{}".format(e.output.decode('utf-8', 'replace'))) self._config_manager.disable_config(config.name, only_for_session=True) config.erase_view_status(initiating_view) - notify(self._window, message, status) + notify_error(self._window, message, status) # Continue with handling pending listeners self._new_session = None sublime.set_timeout_async(self._dequeue_listener_async) diff --git a/plugin/core/workspace.py b/plugin/core/workspace.py index 31d7b92c7..8ca1ea865 100644 --- a/plugin/core/workspace.py +++ b/plugin/core/workspace.py @@ -4,7 +4,7 @@ from .types import matches_pattern from .types import sublime_pattern_to_glob from .url import filename_to_uri -from .logging_notify import notify +from .logging_notify import notify_error from typing import Any import sublime import os @@ -149,7 +149,7 @@ def enable_in_project(window: sublime.Window, config_name: str) -> None: else: message = f"Can't enable {config_name} in the current workspace. Ensure that the project is saved first." status = f"Can't enable {config_name} in this workspace… See console" - notify(window, message, status) + notify_error(window, message, status) def disable_in_project(window: sublime.Window, config_name: str) -> None: @@ -163,4 +163,4 @@ def disable_in_project(window: sublime.Window, config_name: str) -> None: else: message = f"Can't disable {config_name} in the current workspace. Ensure that the project is saved first." status = f"Can't enable {config_name} in this workspace… See console" - notify(window, message, status) + notify_error(window, message, status) diff --git a/plugin/execute_command.py b/plugin/execute_command.py index 89cb2393a..d8dbbca8b 100644 --- a/plugin/execute_command.py +++ b/plugin/execute_command.py @@ -1,5 +1,5 @@ from __future__ import annotations -from .core.logging_notify import notify +from .core.logging_notify import notify_error from .core.protocol import Error from .core.protocol import ExecuteCommandParams from .core.registry import LspTextCommand @@ -61,7 +61,7 @@ def handle_error_async(self, error: Error, command_name: str) -> None: """ message = f"command {command_name} failed. Reason: {str(error)}" status = f"{command_name} failed… See console" - notify(self.view.window(), message, status) + notify_error(self.view.window(), message, status) def _expand_variables(self, command_args: list[Any]) -> list[Any]: view = self.view diff --git a/plugin/tooling.py b/plugin/tooling.py index 617ff7317..e6427f47e 100644 --- a/plugin/tooling.py +++ b/plugin/tooling.py @@ -1,7 +1,6 @@ from __future__ import annotations from .core.css import css from .core.logging import debug -from .core.logging_notify import notify from .core.logging_notify import notify_error from .core.registry import windows from .core.sessions import get_plugin @@ -313,7 +312,7 @@ def run(self) -> None: view = wm.window.active_view() if not view: message = 'Troubleshooting must be run with a file opened' - notify(self.window, message, message) + notify_error(self.window, message, message) return active_view = view configs = wm.get_config_manager().get_configs()