diff --git a/plugin/symbols.py b/plugin/symbols.py index 78df31b41..1799f44ca 100644 --- a/plugin/symbols.py +++ b/plugin/symbols.py @@ -1,6 +1,7 @@ import weakref from .core.protocol import DocumentSymbol from .core.protocol import DocumentSymbolParams +from .core.protocol import Point from .core.protocol import Request from .core.protocol import SymbolInformation from .core.protocol import SymbolKind @@ -8,6 +9,7 @@ from .core.registry import LspTextCommand from .core.sessions import print_to_status_bar from .core.typing import Any, List, Optional, Tuple, Dict, Union, cast +from .core.views import offset_to_point from .core.views import range_to_region from .core.views import SublimeKind from .core.views import SYMBOL_KINDS @@ -77,7 +79,7 @@ def symbol_information_to_quick_panel_item( def symbol_to_list_input_item( - view: sublime.View, item: Union[DocumentSymbol, SymbolInformation], hierarchy: str = '' + item: Union[DocumentSymbol, SymbolInformation], hierarchy: str = '' ) -> sublime.ListInputItem: name = item['name'] kind = item['kind'] @@ -91,17 +93,16 @@ def symbol_to_list_input_item( details.append(detail) if hierarchy: details.append(hierarchy + " > " + name) - region = range_to_region(selection_range, view) else: item = cast(SymbolInformation, item) container_name = item.get('containerName') if container_name: details.append(container_name) - region = range_to_region(item['location']['range'], view) + selection_range = item['location']['range'] deprecated = SymbolTag.Deprecated in (item.get('tags') or []) or item.get('deprecated', False) return sublime.ListInputItem( name, - {'kind': kind, 'region': [region.a, region.b], 'deprecated': deprecated}, + {'kind': kind, 'range': selection_range, 'deprecated': deprecated}, details=" • ".join(details), annotation=st_kind[2], kind=st_kind @@ -198,8 +199,8 @@ def handle_response_async(self, response: Union[List[DocumentSymbol], List[Symbo else: items = cast(List[SymbolInformation], response) for item in items: - self.items.append(symbol_to_list_input_item(self.view, item)) - self.items.sort(key=lambda item: item.value['region']) + self.items.append(symbol_to_list_input_item(item)) + self.items.sort(key=lambda item: Point.from_lsp(item.value['range']['start'])) window = self.view.window() if window: self.cached = True @@ -217,7 +218,7 @@ def process_document_symbol_recursive( ) -> List[sublime.ListInputItem]: name = item['name'] name_hierarchy = hierarchy + " > " + name if hierarchy else name - items = [symbol_to_list_input_item(self.view, item, hierarchy)] + items = [symbol_to_list_input_item(item, hierarchy)] for child in item.get('children') or []: items.extend(self.process_document_symbol_recursive(child, name_hierarchy)) return items @@ -285,9 +286,11 @@ def list_items(self) -> Tuple[List[sublime.ListInputItem], int]: items = [item for item in self.items if not self.kind or item.value['kind'] == self.kind] selected_index = 0 if self.old_selection: - pt = self.old_selection[0].b + caret_point = offset_to_point(self.view, self.old_selection[0].b) for index, item in enumerate(items): - if item.value['region'][0] <= pt: + start = item.value['range']['start'] + if start['line'] < caret_point.row or \ + start['line'] == caret_point.row and start['character'] <= caret_point.col: selected_index = index else: break @@ -295,10 +298,11 @@ def list_items(self) -> Tuple[List[sublime.ListInputItem], int]: def preview(self, text: Any) -> Union[str, sublime.Html, None]: if isinstance(text, dict): - r = text.get('region') + r = text.get('range') if r: - self.view.run_command('lsp_selection_set', {'regions': [(r[0], r[1])]}) - self.view.show_at_center(r[0]) + region = range_to_region(r, self.view) + self.view.run_command('lsp_selection_set', {'regions': [(region.a, region.b)]}) + self.view.show_at_center(region.a) if text.get('deprecated'): return "⚠ Deprecated" return ""