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

Sort and select closest result for Find References in quick panel #2337

Merged
merged 5 commits into from
Oct 19, 2023
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
7 changes: 7 additions & 0 deletions plugin/core/protocol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .typing import Enum, IntEnum, IntFlag, StrEnum
from .typing import Any, Dict, Generic, Iterable, List, Literal, Mapping, NotRequired, Optional, TypedDict, TypeVar, Union # noqa: E501
from functools import total_ordering
import sublime

INT_MAX = 2**31 - 1
Expand Down Expand Up @@ -6275,6 +6276,7 @@ def to_payload(self) -> Dict[str, Any]:
return payload


@total_ordering
class Point(object):
def __init__(self, row: int, col: int) -> None:
self.row = int(row)
Expand All @@ -6288,6 +6290,11 @@ def __eq__(self, other: object) -> bool:
return NotImplemented
return self.row == other.row and self.col == other.col

def __lt__(self, other: object) -> bool:
if not isinstance(other, Point):
return NotImplemented
return (self.row, self.col) < (other.row, other.col)

@classmethod
def from_lsp(cls, point: 'Position') -> 'Point':
return Point(point['line'], point['character'])
Expand Down
6 changes: 4 additions & 2 deletions plugin/locationpicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def __init__(
force_group: bool = True,
group: int = -1,
placeholder: str = "",
kind: SublimeKind = sublime.KIND_AMBIGUOUS
kind: SublimeKind = sublime.KIND_AMBIGUOUS,
selected_index: int = -1
) -> None:
self._view = view
self._view_states = ([r.to_tuple() for r in view.sel()], view.viewport_position())
Expand All @@ -92,8 +93,9 @@ def __init__(
for location in locations
],
on_select=self._select_entry,
on_highlight=self._highlight_entry,
flags=sublime.KEEP_OPEN_ON_FOCUS_LOST,
selected_index=selected_index,
on_highlight=self._highlight_entry,
placeholder=placeholder
)

Expand Down
17 changes: 15 additions & 2 deletions plugin/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .core.views import get_line
from .core.views import get_symbol_kind_from_scope
from .core.views import get_uri_and_position_from_location
from .core.views import position_to_offset
from .core.views import text_document_position_params
from .locationpicker import LocationPicker
import functools
Expand Down Expand Up @@ -143,10 +144,22 @@ def _show_references_in_quick_panel(
group: int,
position: int
) -> None:
self.view.run_command("add_jump_record", {"selection": [(r.a, r.b) for r in self.view.sel()]})
selection = self.view.sel()
self.view.run_command("add_jump_record", {"selection": [(r.a, r.b) for r in selection]})
placeholder = "References to " + word
kind = get_symbol_kind_from_scope(self.view.scope_name(position))
LocationPicker(self.view, session, locations, side_by_side, force_group, group, placeholder, kind)
index = 0
locations.sort(key=lambda l: (l['uri'], Point.from_lsp(l['range']['start'])))
if len(selection):
pt = selection[0].b
view_filename = self.view.file_name()
for idx, location in enumerate(locations):
if view_filename != session.config.map_server_uri_to_client_path(location['uri']):
continue
index = idx
if position_to_offset(location['range']['start'], self.view) > pt:
break
LocationPicker(self.view, session, locations, side_by_side, force_group, group, placeholder, kind, index)

def _show_references_in_output_panel(self, word: str, session: Session, locations: List[Location]) -> None:
wm = windows.lookup(session.window)
Expand Down