Skip to content

Commit

Permalink
Send range with textDocument/hover
Browse files Browse the repository at this point in the history
This is needed to handle scalameta/metals#3060
It should be handle by LSP package once microsoft/language-server-protocol#377
is resolved
  • Loading branch information
ayoub-benali committed Nov 14, 2021
1 parent e5d3786 commit d806c9a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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

0 comments on commit d806c9a

Please sign in to comment.