forked from rosshadden/sublime-xpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublime_input_quickpanel.py
118 lines (96 loc) · 4.4 KB
/
sublime_input_quickpanel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import sublime
import sublime_plugin
from .sublime_input_view import RequestViewInputCommand
on_modified_callbacks = {}
class QuickPanelFromInputCommand(RequestViewInputCommand): # this command should be overidden and not used directly
items = None
ignore_view_activations = False
highlighted_index = -1
highlighted_result = None
def run(self, edit, **args):
self.items = None
self.ignore_view_activations = False
super().run(edit, **args)
def parse_args(self):
super().parse_args()
global on_modified_callbacks
on_modified_callbacks[self.view.id()] = lambda view: self.on_modified_async(view)
def close_quick_panel(self):
"""Close existing quick panel."""
sublime.active_window().run_command('hide_overlay', { 'cancel': True })
def process_current_input(self):
items = self.get_items_from_input()
if items is not None:
self.items = items
else:
if self.get_value_from_args('use_previous_when_none', False):
return
else:
self.items = None
self.ignore_view_activations = True
self.close_quick_panel()
if items is not None:
flags = 0
if self.live_mode:
flags = sublime.KEEP_OPEN_ON_FOCUS_LOST
index = -1
if self.highlighted_result is not None:
if self.highlighted_index < len(items) and items[self.highlighted_index] == self.highlighted_result: # if the value at the previously highlighted index matches the previously highlighted item, highlight it
index = self.highlighted_index
else: # otherwise try to find the previously highlighted item
try:
index = items.index(self.highlighted_result)
except ValueError:
pass
self.view.window().show_quick_panel(self.get_items_to_show_in_quickpanel(), self.quickpanel_selection_done, flags, index, self.quickpanel_selection_changed)
if self.live_mode and self.input_panel is not None:
self.input_panel.window().focus_view(self.input_panel)
def on_activated_async(self, view):
if self.ignore_view_activations and view is not None:
if view not in self.associated_views():
self.ignore_view_activations = False
else:
super().on_activated_async(view)
def on_modified_async(self, view):
if not self.input_panel_hidden:
self.close_quick_panel()
def get_items_from_input(self):
return None
def get_items_to_show_in_quickpanel(self):
return self.items
def quickpanel_selection_changed(self, selected_index):
self.highlighted_index = selected_index
if selected_index > -1:
self.highlighted_result = self.items[selected_index]
def quickpanel_selection_done(self, selected_index):
if selected_index > -1: # if it wasn't cancelled
self.close_input_panel()
if self.live_mode:
self.commit_input()
if not self.live_mode:
self.command_complete(selected_index == -1)
def associated_views(self):
return super().associated_views() + [] # NOTE: ideally we would be able to return the quick panel view here, but as it is not exposed by the Sublime API, we instead use "ignore_view_activations"
def input_cancelled(self):
self.close_quick_panel()
super().input_cancelled()
def command_complete(self, cancelled):
super().command_complete(cancelled)
self.close_quick_panel()
if not cancelled and self.live_mode:
self.commit_input()
self.items = None
def unregister_callback(self):
global on_modified_callbacks
on_modified_callbacks.pop(self.view.id(), None)
super().unregister_callback()
def commit_input(self):
pass
class QuickPanelInputViewListener(sublime_plugin.EventListener):
def on_modified_async(self, view):
global on_modified_callbacks
for callback in on_modified_callbacks.values():
callback(view)
def on_pre_close(self, view):
global on_modified_callbacks
on_modified_callbacks.pop(view.id(), None) # remove callback if present