Skip to content

Commit

Permalink
consolidate error notification into 1 function
Browse files Browse the repository at this point in the history
with an extra severe argument to downgrade to a non-error modal dialog if needed
  • Loading branch information
eugenesvk committed Nov 27, 2024
1 parent 5ab0e73 commit 546a8df
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
24 changes: 7 additions & 17 deletions plugin/core/logging_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions plugin/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
4 changes: 2 additions & 2 deletions plugin/execute_command.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions plugin/tooling.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 546a8df

Please sign in to comment.