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

Send range with textDocument/hover #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
// Show Type annotation on code selection
// {
// "command": "lsp_metals_hover_range",
// "keys": ["UNBOUND"]
// }
]
48 changes: 48 additions & 0 deletions st4/LspMetalsHoverRange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from LSP.plugin.core.registry import LspTextCommand
from LSP.plugin.core.typing import Union
from LSP.plugin.core.protocol import Request, Range, TextDocumentIdentifier, Error, Hover
from LSP.plugin.core.views import first_selection_region, text_document_identifier, region_to_range, FORMAT_MARKED_STRING, FORMAT_MARKUP_CONTENT, minihtml, update_lsp_popup, show_lsp_popup
import sublime

HoverOrError = Union[Hover, Error]

class LspMetalsHoverRangeCommand(LspTextCommand):
session_name = "metals"

def run(self, edit: sublime.Edit) -> None:
session = self.session_by_name()
if not session:
return
view = self.view
region = first_selection_region(view)
begin = region.begin()
document_range = {
"textDocument": text_document_identifier(view),
"range": region_to_range(view, region).to_lsp()
}

def on_response(response: HoverOrError):
if isinstance(response, Error):
sublime.status_message('Hover error: {}'.format(response))

if response:
content = (response.get('contents') or '') if isinstance(response, dict) else ''
formated = minihtml(self.view, content, allowed_formats=FORMAT_MARKED_STRING | FORMAT_MARKUP_CONTENT)
if self.view.is_popup_visible():
update_lsp_popup(self.view, formated)
else:
show_lsp_popup(
self.view,
formated,
flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
location=begin
)

def on_error(error: Error):
sublime.status_message('Hover error: {}'.format(error))

session.send_request(
Request("textDocument/hover", document_range, self.view),
on_response,
on_error
)
1 change: 1 addition & 0 deletions st4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import mdpopups
from functools import reduce
from . LspMetalsFocus import LspMetalsFocusViewCommand, ActiveViewListener
from . LspMetalsHoverRange import LspMetalsHoverRangeCommand

# TODO: Bring to public API
from LSP.plugin.core.views import location_to_encoded_filename
Expand Down