From f110f31556a140150f5279ed2bd9510a49f50738 Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Wed, 4 Oct 2023 21:28:57 +0200 Subject: [PATCH 1/3] Select closest location for Find References in quick panel --- plugin/core/protocol.py | 7 +++++++ plugin/locationpicker.py | 6 ++++-- plugin/references.py | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/plugin/core/protocol.py b/plugin/core/protocol.py index 372a53ae9..5a0748a7f 100644 --- a/plugin/core/protocol.py +++ b/plugin/core/protocol.py @@ -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 @@ -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) @@ -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']) diff --git a/plugin/locationpicker.py b/plugin/locationpicker.py index 0ee78fd32..16bac6662 100644 --- a/plugin/locationpicker.py +++ b/plugin/locationpicker.py @@ -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()) @@ -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 ) diff --git a/plugin/references.py b/plugin/references.py index 6c6af0441..5e7f2aa05 100644 --- a/plugin/references.py +++ b/plugin/references.py @@ -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 @@ -143,10 +144,24 @@ 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): + filename = session.config.map_server_uri_to_client_path(location['uri']) + if not filename == view_filename: + continue + if position_to_offset(location['range']['start'], self.view) <= pt: + index = idx + else: + 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) From 02ef4ca769cc813f9d8e02ce088d12be4392171a Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sat, 14 Oct 2023 19:37:09 +0200 Subject: [PATCH 2/3] Tweak --- plugin/references.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/references.py b/plugin/references.py index 5e7f2aa05..68e734bf3 100644 --- a/plugin/references.py +++ b/plugin/references.py @@ -155,7 +155,7 @@ def _show_references_in_quick_panel( view_filename = self.view.file_name() for idx, location in enumerate(locations): filename = session.config.map_server_uri_to_client_path(location['uri']) - if not filename == view_filename: + if filename != view_filename: continue if position_to_offset(location['range']['start'], self.view) <= pt: index = idx From 8e9b25aa80420fd93edc385b4d574f3b2e5378f1 Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sat, 14 Oct 2023 19:42:01 +0200 Subject: [PATCH 3/3] More tweaks --- plugin/references.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/plugin/references.py b/plugin/references.py index 68e734bf3..35961378b 100644 --- a/plugin/references.py +++ b/plugin/references.py @@ -154,12 +154,10 @@ def _show_references_in_quick_panel( pt = selection[0].b view_filename = self.view.file_name() for idx, location in enumerate(locations): - filename = session.config.map_server_uri_to_client_path(location['uri']) - if filename != view_filename: + if view_filename != session.config.map_server_uri_to_client_path(location['uri']): continue - if position_to_offset(location['range']['start'], self.view) <= pt: - index = idx - else: + 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)