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 completion docs not showing for servers that are not resolve providers #1610

Merged
merged 2 commits into from
Mar 11, 2021
Merged
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
90 changes: 40 additions & 50 deletions plugin/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,52 @@ class LspResolveDocsCommand(LspTextCommand):
completions = {} # type: Dict[SessionName, List[CompletionItem]]

def run(self, edit: sublime.Edit, index: int, session_name: str, event: Optional[dict] = None) -> None:
item = self.completions[session_name][index]
detail = self.format_documentation(item.get('detail') or "")
documentation = self.format_documentation(item.get("documentation") or "")
# don't show the detail in the cooperate AC popup if it is already shown in the AC details filed.
self.is_detail_shown = bool(detail)
if not detail or not documentation:

def run_async() -> None:
# To make sure that the detail or documentation fields doesn't exist we need to resove the completion
# item. If those fields appear after the item is resolved we show them in the popup.
session = self.session_by_name(session_name, 'completionProvider.resolveProvider')
if session:
request = Request.resolveCompletionItem(item, self.view)
session.send_request_async(request, self.handle_resolve_response_async)

return sublime.set_timeout_async(run_async)
minihtml_content = self.get_content(documentation, detail)
self.show_popup(minihtml_content)

def format_documentation(self, content: Union[str, Dict[str, str]]) -> str:
return minihtml(self.view, content, allowed_formats=FORMAT_STRING | FORMAT_MARKUP_CONTENT)

def get_content(self, documentation: str, detail: str) -> str:
content = ""
if detail and not self.is_detail_shown:
content += "<div class='highlight'>{}</div>".format(detail)
if documentation:
content += documentation
return content

def show_popup(self, minihtml_content: str) -> None:
show_lsp_popup(
self.view,
minihtml_content,
flags=sublime.COOPERATE_WITH_AUTO_COMPLETE,
md=True,
on_navigate=self.on_navigate)

def on_navigate(self, url: str) -> None:
webbrowser.open(url)
def run_async() -> None:
item = self.completions[session_name][index]
session = self.session_by_name(session_name, 'completionProvider.resolveProvider')
if session:
request = Request.resolveCompletionItem(item, self.view)
session.send_request_async(request, self._handle_resolve_response_async)
else:
self._handle_resolve_response_async(item)

sublime.set_timeout_async(run_async)

def handle_resolve_response_async(self, item: Optional[dict]) -> None:
def _format_documentation(self, content: Union[str, Dict[str, str]]) -> str:
return minihtml(self.view, content, allowed_formats=FORMAT_STRING | FORMAT_MARKUP_CONTENT)

def _handle_resolve_response_async(self, item: Optional[dict]) -> None:
detail = ""
documentation = ""
if item:
detail = self.format_documentation(item.get('detail') or "")
documentation = self.format_documentation(item.get("documentation") or "")
detail = self._format_documentation(item.get('detail') or "")
documentation = self._format_documentation(item.get("documentation") or "")
if not documentation:
documentation = self.format_documentation({"kind": "markdown", "value": "*No documentation available.*"})
minihtml_content = self.get_content(documentation, detail)
show = self.update_popup if self.view.is_popup_visible() else self.show_popup
# NOTE: Update/show popups from the main thread, or else the popup might make the AC widget disappear.
sublime.set_timeout(lambda: show(minihtml_content))

def update_popup(self, minihtml_content: str) -> None:
update_lsp_popup(self.view, minihtml_content, md=True)
documentation = self._format_documentation({"kind": "markdown", "value": "*No documentation available.*"})
minihtml_content = ""
if detail:
minihtml_content += "<div class='highlight'>{}</div>".format(detail)
if documentation:
minihtml_content += documentation

def run_main() -> None:
if not self.view.is_valid():
return
if self.view.is_popup_visible():
update_lsp_popup(self.view, minihtml_content, md=True)
else:
show_lsp_popup(
self.view,
minihtml_content,
flags=sublime.COOPERATE_WITH_AUTO_COMPLETE,
md=True,
on_navigate=self._on_navigate)

sublime.set_timeout(run_main)

def _on_navigate(self, url: str) -> None:
webbrowser.open(url)


class LspCompleteCommand(sublime_plugin.TextCommand):
Expand Down