Skip to content

Commit

Permalink
refactor: simplify message box functions
Browse files Browse the repository at this point in the history
Since now we are on py38, we can just use f-string.
Also fixed some errors which were suppressed due to use of **kwargs.

Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Aug 30, 2024
1 parent 639ffa6 commit 8be17c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion plugin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def set_account_status(
icon, msg = "⚠", "has signed in but not authorized."
else:
icon, msg = "✈", "has been signed in and authorized."
status_message(msg, icon_=icon, console_=True)
status_message(msg, icon=icon, console=True)

@classmethod
def from_view(cls, view: sublime.View) -> CopilotPlugin | None:
Expand Down
8 changes: 4 additions & 4 deletions plugin/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def run(self, plugin: CopilotPlugin, session: Session, _: sublime.Edit) -> None:
session.send_request(Request(REQ_GET_VERSION, {}), self._on_result_get_version)

def _on_result_get_version(self, payload: CopilotPayloadGetVersion) -> None:
message_dialog("Server version: {}", payload["version"])
message_dialog(f'Server version: {payload["version"]}')


class CopilotAskCompletionsCommand(CopilotTextCommand):
Expand Down Expand Up @@ -737,7 +737,7 @@ def _on_result_check_status(self, payload: CopilotPayloadSignInConfirm | Copilot
message_dialog(f'(localChecksOnly) Signed in and authorized with user "{user}".')
elif payload["status"] == "NotAuthorized":
CopilotPlugin.set_account_status(signed_in=True, authorized=False)
message_dialog("Your GitHub account doesn't subscribe to Copilot.", is_error_=True)
message_dialog("Your GitHub account doesn't subscribe to Copilot.", error=True)
else:
CopilotPlugin.set_account_status(signed_in=False, authorized=False)
message_dialog("You haven't signed in yet.")
Expand Down Expand Up @@ -856,7 +856,7 @@ def _on_result_sign_out(self, payload: CopilotPayloadSignOut) -> None:
session_dir = Path.home() / ".config/github-copilot"

if not session_dir.is_dir():
message_dialog(f"Failed to find the session directory: {session_dir}", _error=True)
message_dialog(f"Failed to find the session directory: {session_dir}", error=True)
return

rmtree_ex(str(session_dir), ignore_errors=True)
Expand Down Expand Up @@ -891,7 +891,7 @@ def run(self, plugin: CopilotPlugin, session: Session, _: sublime.Edit, request_
try:
decode_payload = sublime.decode_value(payload)
except ValueError as e:
message_dialog(f"Failed to parse payload: {e}", is_error_=True)
message_dialog(f"Failed to parse payload: {e}", error=True)
decode_payload = {}
session.send_request(Request(request_type, decode_payload), self._on_results_any_request)

Expand Down
40 changes: 17 additions & 23 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,19 @@ def get_view_language_id(view: sublime.View, point: int = 0) -> str:
return ""


def message_dialog(msg_: str, *args: Any, error_: bool = False, console_: bool = False, **kwargs: Any) -> None:
def message_dialog(msg: str, *, error: bool = False, console: bool = False) -> None:
"""
Show a message dialog, whose message is prefixed with "[PACKAGE_NAME]".
:param msg_: The message
:param args: The arguments for `str.format`
:param error_: The error
:param console_: Show message in console as well?
:param kwargs: The keywords arguments for `str.format`
:param msg: The message
:param error: Use ST error dialog instead of message dialog
:param console: Show message in console as well
"""
full_msg = f"[{PACKAGE_NAME}] {msg_.format(*args, **kwargs)}"
messenger = sublime.error_message if error_ else sublime.message_dialog
full_msg = f"[{PACKAGE_NAME}] {msg}"
messenger = sublime.error_message if error else sublime.message_dialog
messenger(full_msg)

if console_:
if console:
print(full_msg)


Expand All @@ -192,15 +190,13 @@ def mutable_view(view: sublime.View) -> Generator[sublime.View, Any, None]:
view.set_read_only(True)


def ok_cancel_dialog(msg_: str, *args: Any, **kwargs: Any) -> bool:
def ok_cancel_dialog(msg: str) -> bool:
"""
Show an OK/cancel dialog, whose message is prefixed with "[PACKAGE_NAME]".
:param msg_: The message
:param args: The arguments for `str.format`
:param kwargs: The keywords arguments for `str.format`
:param msg: The message
"""
return sublime.ok_cancel_dialog(f"[{PACKAGE_NAME}] {msg_.format(*args, **kwargs)}")
return sublime.ok_cancel_dialog(f"[{PACKAGE_NAME}] {msg}")


def remove_prefix(s: str, prefix: str) -> str:
Expand All @@ -214,21 +210,19 @@ def remove_suffix(s: str, suffix: str) -> str:
return s[: -len(suffix)] if suffix and s.endswith(suffix) else s


def status_message(msg_: str, *args: Any, icon_: str | None = "✈", console_: bool = False, **kwargs: Any) -> None:
def status_message(msg: str, icon: str | None = "✈", *, console: bool = False) -> None:
"""
Show a status message in the status bar, whose message is prefixed with `icon` and "Copilot".
:param msg_: The message
:param args: The arguments for `str.format`
:param icon_: The icon
:param console_: Show message in console as well?
:param kwargs: The keywords arguments for `str.format`
:param msg: The message
:param icon: The icon
:param console: Show message in console as well
"""
prefix = f"{icon_} " if icon_ else ""
full_msg = f"{prefix}Copilot {msg_.format(*args, **kwargs)}"
prefix = f"{icon} " if icon else ""
full_msg = f"{prefix}Copilot {msg}"
sublime.status_message(full_msg)

if console_:
if console:
print(full_msg)


Expand Down

0 comments on commit 8be17c3

Please sign in to comment.