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 when possible #1900

Merged
merged 19 commits into from
Nov 27, 2021
Merged
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
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