Skip to content

Commit

Permalink
Send range with textDocument/hover when possible (#1900)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoub-benali authored Nov 27, 2021
1 parent acfd640 commit f6ab314
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions plugin/core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class InsertTextMode:
'position': Position,
}, total=True)

ExperimentalTextDocumentRangeParams = TypedDict('ExperimentalTextDocumentRangeParams', {
'textDocument': TextDocumentIdentifier,
'position': Position,
'range': RangeLsp,
}, total=True)

CodeDescription = TypedDict('CodeDescription', {
'href': str
}, total=True)
Expand Down
10 changes: 10 additions & 0 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .protocol import DiagnosticRelatedInformation
from .protocol import DiagnosticSeverity
from .protocol import DocumentUri
from .protocol import ExperimentalTextDocumentRangeParams
from .protocol import Location
from .protocol import LocationLink
from .protocol import MarkedString
Expand Down Expand Up @@ -271,6 +272,15 @@ def text_document_position_params(view: sublime.View, location: int) -> TextDocu
return {"textDocument": text_document_identifier(view), "position": position(view, location)}


def text_document_range_params(view: sublime.View, location: int,
region: sublime.Region) -> ExperimentalTextDocumentRangeParams:
return {
"textDocument": text_document_identifier(view),
"position": position(view, location),
"range": region_to_range(view, region).to_lsp()
}


def did_open_text_document_params(view: sublime.View, language_id: str) -> Dict[str, Any]:
return {"textDocument": text_document_item(view, language_id)}

Expand Down
15 changes: 14 additions & 1 deletion plugin/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from .core.promise import Promise
from .core.protocol import Diagnostic
from .core.protocol import Error
from .core.protocol import ExperimentalTextDocumentRangeParams
from .core.protocol import Hover
from .core.protocol import Position
from .core.protocol import RangeLsp
from .core.protocol import Request
from .core.protocol import TextDocumentPositionParams
from .core.registry import LspTextCommand
from .core.registry import windows
from .core.sessions import Session
from .core.sessions import SessionBufferProtocol
from .core.settings import userprefs
from .core.typing import List, Optional, Dict, Tuple, Sequence, Union
Expand All @@ -22,6 +25,7 @@
from .core.views import make_link
from .core.views import show_lsp_popup
from .core.views import text_document_position_params
from .core.views import text_document_range_params
from .core.views import unpack_href_location
from .core.views import update_lsp_popup
from .core.windows import AbstractViewListener
Expand Down Expand Up @@ -120,14 +124,23 @@ def run_async() -> None:

def request_symbol_hover_async(self, listener: AbstractViewListener, point: int) -> None:
hover_promises = [] # type: List[Promise[ResolvedHover]]
document_position = text_document_position_params(self.view, point)
for session in listener.sessions_async('hoverProvider'):
document_position = self._create_hover_request(session, point)
hover_promises.append(session.send_request_task(
Request("textDocument/hover", document_position, self.view)
))

Promise.all(hover_promises).then(lambda responses: self._on_all_settled(responses, listener, point))

def _create_hover_request(
self, session: Session, point: int
) -> Union[TextDocumentPositionParams, ExperimentalTextDocumentRangeParams]:
if session.get_capability('experimental.rangeHoverProvider'):
region = first_selection_region(self.view)
if region is not None and region.contains(point):
return text_document_range_params(self.view, point, region)
return text_document_position_params(self.view, point)

def _on_all_settled(self, responses: List[ResolvedHover], listener: AbstractViewListener, point: int) -> None:
hovers = [] # type: List[Hover]
errors = [] # type: List[Error]
Expand Down

0 comments on commit f6ab314

Please sign in to comment.