Skip to content

Commit

Permalink
Fix performance regression in Goto Symbol overlay (#2348)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann authored Oct 28, 2023
1 parent 3b17a99 commit 4ff494b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions plugin/symbols.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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
from .core.protocol import SymbolTag
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
Expand Down Expand Up @@ -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']
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -285,20 +286,23 @@ 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
return items, selected_index

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 ""
Expand Down

0 comments on commit 4ff494b

Please sign in to comment.