-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommands_base.py
80 lines (61 loc) · 2.85 KB
/
commands_base.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
#coding: utf8
#################################### IMPORTS ###################################
# Sublime Libs
import sublime_plugin
from .quick_panel_cols import format_for_display
from .helpers import temporary_event_handler
from .package_resources import glob_packages, package_file_contents, \
package_file_path_to_open_file_path
from .jsonix import decode_with_ix, strip_json_comments, loads as loadsj
def glob_and_parse_package_json(pattern):
for pkg, name, f in glob_packages(
pattern,
ignored_packages=True,
package_sort_key=lambda p: (p!="User", p=="Default", p)):
text = package_file_contents(f)
text = strip_json_comments(text)
if not text: continue
with decode_with_ix():
setting_dict = loadsj(text)
yield pkg, name, f, text, setting_dict
class IEditJSONPreference:
"Just factored out all overideables into this class for visibility"
format_cols = () # MUST_IMPLEMENT
extra_rows = ()
settings_pattern = "*.sublime-json" # MUST OVERRIDE
def on_settings_json(self, pkg, name, f, text, setting_dict, completions):
"must implement"
def on_selection(self, setting):
"must implement"
def format_for_display(self, settings):
return settings # for quick panel
class EditJSONPreferenceBase(sublime_plugin.WindowCommand, IEditJSONPreference):
def format_for_display(self, rows):
display = format_for_display(rows, cols=self.format_cols)
if self.extra_rows:
display = list(map(list, zip(display, *[[m[i] for m in rows]
for i in self.extra_rows])))
return display
def run(self):
window = self.window
settings = []
completions = set()
for pkg, name, f, text, setting_dict in \
glob_and_parse_package_json(self.settings_pattern):
settings.extend(self.on_settings_json (
pkg, name, f, text, setting_dict, completions))
display = self.format_for_display(settings)
ch = temporary_event_handler( lambda *a: [(c,c) for c in completions],
'on_query_completions')
def on_select(i, on_highlight=False):
if not on_highlight:
ch.remove()
if i != -1:
fn, lineno, regions = self.on_selection(settings[i])
fn = package_file_path_to_open_file_path(fn)
window.run_command("open_file_enhanced", {"file" : (fn),
"regions" : regions})
on_highlight=lambda i: i
window.show_quick_panel(display, on_select,
on_highlight=on_highlight,
flags=1)