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

Add a couple of types here and there #1862

Merged
merged 5 commits into from
Oct 1, 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
13 changes: 11 additions & 2 deletions plugin/core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class InsertTextMode:
'end': Position
})

TextDocumentIdentifier = TypedDict('TextDocumentIdentifier', {
'uri': DocumentUri,
}, total=True)

TextDocumentPositionParams = TypedDict('TextDocumentPositionParams', {
'textDocument': TextDocumentIdentifier,
'position': Position,
}, total=True)

CodeDescription = TypedDict('CodeDescription', {
'href': str
}, total=True)
Expand Down Expand Up @@ -456,7 +465,7 @@ def __eq__(self, other: object) -> bool:
def from_lsp(cls, point: Position) -> 'Point':
return Point(point['line'], point['character'])

def to_lsp(self) -> Dict[str, Any]:
def to_lsp(self) -> Position:
return {
"line": self.row,
"character": self.col
Expand All @@ -481,7 +490,7 @@ def __eq__(self, other: object) -> bool:
def from_lsp(cls, range: RangeLsp) -> 'Range':
return Range(Point.from_lsp(range['start']), Point.from_lsp(range['end']))

def to_lsp(self) -> Dict[str, Any]:
def to_lsp(self) -> RangeLsp:
return {
'start': self.start.to_lsp(),
'end': self.end.to_lsp()
Expand Down
28 changes: 15 additions & 13 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from .protocol import Range
from .protocol import RangeLsp
from .protocol import Request
from .protocol import TextDocumentIdentifier
from .protocol import TextDocumentPositionParams
from .settings import userprefs
from .types import ClientConfig
from .typing import Callable, Optional, Dict, Any, Iterable, List, Union, Tuple, Sequence, cast
Expand Down Expand Up @@ -156,7 +158,7 @@ def offset_to_point(view: sublime.View, offset: int) -> Point:
return Point(*view.rowcol_utf16(offset))


def position(view: sublime.View, offset: int) -> Dict[str, Any]:
def position(view: sublime.View, offset: int) -> Position:
return offset_to_point(view, offset).to_lsp()


Expand Down Expand Up @@ -225,7 +227,7 @@ def uri_from_view(view: sublime.View) -> DocumentUri:
raise MissingUriError(view.id())


def text_document_identifier(view_or_uri: Union[DocumentUri, sublime.View]) -> Dict[str, Any]:
def text_document_identifier(view_or_uri: Union[DocumentUri, sublime.View]) -> TextDocumentIdentifier:
if isinstance(view_or_uri, DocumentUri):
uri = view_or_uri
else:
Expand Down Expand Up @@ -265,8 +267,8 @@ def versioned_text_document_identifier(view: sublime.View, version: int) -> Dict
return {"uri": uri_from_view(view), "version": version}


def text_document_position_params(view: sublime.View, location: int) -> Dict[str, Any]:
return {"textDocument": text_document_identifier(view), "position": offset_to_point(view, location).to_lsp()}
def text_document_position_params(view: sublime.View, location: int) -> TextDocumentPositionParams:
return {"textDocument": text_document_identifier(view), "position": position(view, location)}


def did_open_text_document_params(view: sublime.View, language_id: str) -> Dict[str, Any]:
Expand All @@ -278,7 +280,7 @@ def render_text_change(change: sublime.TextChange) -> Dict[str, Any]:
return {
"range": {
"start": {"line": change.a.row, "character": change.a.col_utf16},
"end": {"line": change.b.row, "character": change.b.col_utf16}},
"end": {"line": change.b.row, "character": change.b.col_utf16}},
"rangeLength": change.len_utf16,
"text": change.str
}
Expand Down Expand Up @@ -387,16 +389,16 @@ def text_document_code_action_params(
diagnostics: Sequence[Diagnostic],
on_save_actions: Optional[Sequence[str]] = None
) -> Dict[str, Any]:
params = {
context = {
"diagnostics": diagnostics
} # type: Dict[str, Any]
if on_save_actions:
context['only'] = on_save_actions
return {
"textDocument": text_document_identifier(view),
"range": region_to_range(view, region).to_lsp(),
"context": {
"diagnostics": diagnostics
}
"context": context
}
if on_save_actions:
params['context']['only'] = on_save_actions
return params


# Workaround for a limited margin-collapsing capabilities of the minihtml.
Expand Down Expand Up @@ -682,7 +684,7 @@ def location_to_human_readable(
Format an LSP Location (or LocationLink) into a string suitable for a human to read
"""
uri, position = get_uri_and_position_from_location(location)
scheme, parsed = parse_uri(uri)
scheme, _ = parse_uri(uri)
if scheme == "file":
fmt = "{}:{}"
pathname = config.map_server_uri_to_client_path(uri)
Expand Down
12 changes: 6 additions & 6 deletions plugin/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ class LspSymbolReferencesCommand(LspTextCommand):

capability = 'referencesProvider'

def __init__(self, view: sublime.View) -> None:
super().__init__(view)
self._picker = None # type: Optional[LocationPicker]

def run(self, _: sublime.Edit, event: Optional[dict] = None, point: Optional[int] = None) -> None:
session = self.best_session(self.capability)
file_path = self.view.file_name()
pos = get_position(self.view, event, point)
if session and file_path and pos is not None:
params = text_document_position_params(self.view, pos)
params['context'] = {"includeDeclaration": False}
position_params = text_document_position_params(self.view, pos)
params = {
'textDocument': position_params['textDocument'],
'position': position_params['position'],
'context': {"includeDeclaration": False},
}
request = Request("textDocument/references", params, self.view, progress=True)
session.send_request(
request,
Expand Down
8 changes: 6 additions & 2 deletions plugin/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ def run(
def _do_rename(self, position: int, new_name: str) -> None:
session = self.best_session(self.capability)
if session:
params = text_document_position_params(self.view, position)
params["newName"] = new_name
position_params = text_document_position_params(self.view, position)
params = {
"textDocument": position_params["textDocument"],
"position": position_params["position"],
"newName": new_name,
}
session.send_request(
Request("textDocument/rename", params, self.view, progress=True),
# This has to run on the main thread due to calling apply_workspace_edit
Expand Down