-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor visual tweaks to ShowMessageRequest popup (#2340)
- Loading branch information
Showing
3 changed files
with
58 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,59 @@ | ||
from .protocol import MessageType | ||
from .protocol import Response | ||
from .protocol import ShowMessageRequestParams | ||
from .sessions import Session | ||
from .typing import Any, List, Callable | ||
from .typing import Any, Dict, List | ||
from .views import show_lsp_popup | ||
from .views import text2html | ||
import sublime | ||
|
||
|
||
ICONS = { | ||
MessageType.Error: '❗', | ||
MessageType.Warning: '⚠️', | ||
MessageType.Info: 'ℹ️', | ||
MessageType.Log: '📝' | ||
} # type: Dict[MessageType, str] | ||
|
||
|
||
class MessageRequestHandler(): | ||
def __init__(self, view: sublime.View, session: Session, request_id: Any, params: dict, source: str) -> None: | ||
def __init__( | ||
self, view: sublime.View, session: Session, request_id: Any, params: ShowMessageRequestParams, source: str | ||
) -> None: | ||
self.session = session | ||
self.request_id = request_id | ||
self.request_sent = False | ||
self.view = view | ||
self.actions = params.get("actions", []) | ||
self.titles = list(action.get("title") for action in self.actions) | ||
self.message = params.get('message', '') | ||
self.action_titles = list(action.get("title") for action in self.actions) | ||
self.message = params['message'] | ||
self.message_type = params.get('type', 4) | ||
self.source = source | ||
|
||
def _send_user_choice(self, href: int = -1) -> None: | ||
if not self.request_sent: | ||
self.request_sent = True | ||
self.view.hide_popup() | ||
# when noop; nothing was selected e.g. the user pressed escape | ||
param = None | ||
index = int(href) | ||
if index != -1: | ||
param = self.actions[index] | ||
response = Response(self.request_id, param) | ||
self.session.send_response(response) | ||
|
||
def show(self) -> None: | ||
show_notification( | ||
formatted = [] # type: List[str] | ||
formatted.append("<h2>{}</h2>".format(self.source)) | ||
icon = ICONS.get(self.message_type, '') | ||
formatted.append("<div class='message'>{} {}</div>".format(icon, text2html(self.message))) | ||
if self.action_titles: | ||
buttons = [] # type: List[str] | ||
for idx, title in enumerate(self.action_titles): | ||
buttons.append("<a href='{}'>{}</a>".format(idx, text2html(title))) | ||
formatted.append("<div class='actions'>" + " ".join(buttons) + "</div>") | ||
show_lsp_popup( | ||
self.view, | ||
self.source, | ||
self.message_type, | ||
self.message, | ||
self.titles, | ||
self._send_user_choice, | ||
self._send_user_choice | ||
) | ||
|
||
|
||
def message_content(source: str, message_type: int, message: str, titles: List[str]) -> str: | ||
formatted = [] | ||
icons = { | ||
1: '❗', | ||
2: '⚠️', | ||
3: 'ℹ️', | ||
4: '📝' | ||
} | ||
icon = icons.get(message_type, '') | ||
formatted.append("<h2>{}</h2>".format(source)) | ||
formatted.append("<p class='message'>{} {}</p>".format(icon, message)) | ||
"".join(formatted), | ||
css=sublime.load_resource("Packages/LSP/notification.css"), | ||
wrapper_class='notification', | ||
on_navigate=self._send_user_choice, | ||
on_hide=self._send_user_choice) | ||
|
||
buttons = [] | ||
for idx, title in enumerate(titles): | ||
buttons.append("<a href='{}'>{}</a>".format(idx, title)) | ||
|
||
formatted.append("<p class='actions'>" + " ".join(buttons) + "</p>") | ||
|
||
return "".join(formatted) | ||
|
||
|
||
def show_notification(view: sublime.View, source: str, message_type: int, message: str, titles: List[str], | ||
on_navigate: Callable, on_hide: Callable) -> None: | ||
stylesheet = sublime.load_resource("Packages/LSP/notification.css") | ||
contents = message_content(source, message_type, message, titles) | ||
show_lsp_popup( | ||
view, | ||
contents, | ||
css=stylesheet, | ||
wrapper_class='notification', | ||
on_navigate=on_navigate, | ||
on_hide=on_hide) | ||
def _send_user_choice(self, href: int = -1) -> None: | ||
if self.request_sent: | ||
return | ||
self.request_sent = True | ||
self.view.hide_popup() | ||
index = int(href) | ||
param = self.actions[index] if index != -1 else None | ||
response = Response(self.request_id, param) | ||
self.session.send_response(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters