-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
125 lines (93 loc) · 4.04 KB
/
plugin.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
119
120
121
122
123
124
125
import sublime
import sublime_plugin
from SublimeLinter.lint import persist, linter
LAST_TOGGLED_LINTER_CLASS = None
class sublime_linter_addon_toggle_linters(sublime_plugin.WindowCommand):
def run(self, persist=None):
view = self.window.active_view()
linters = collect_possible_linters(view)
if not linters:
self.window.status_message(
'No possible linters registered for this view. :-(')
return
items = [text for _, _, text in linters]
try:
selected = next(
i for i, (_, linter_class, _) in enumerate(linters)
if linter_class == LAST_TOGGLED_LINTER_CLASS
)
except StopIteration:
selected = -1
def on_done(result):
global LAST_TOGGLED_LINTER_CLASS
if result == -1:
return # Canceled
disable, linter_class, _ = linters[result]
if persist == "project" and self.window.project_file_name():
linter_class.disabled = None
project_data = self.window.project_data() or {}
key = "SublimeLinter.linters.{}.disable".format(linter_class.name)
project_data.setdefault("settings", {})[key] = disable
self.window.set_project_data(project_data)
wid_to_relint = self.window.id()
else:
linter_class.disabled = disable
wid_to_relint = None # `None` means *all*
LAST_TOGGLED_LINTER_CLASS = linter_class
sublime.run_command('sublime_linter_config_changed', {
'hint': 'relint',
'wid': wid_to_relint
})
self.window.status_message(
'{} {}.'.format(linter_class.name,
'disabled' if disable else 'enabled'))
self.window.show_quick_panel(items, on_done, 0, selected)
def collect_possible_linters(view):
rv = []
for name, linter_class in persist.linter_classes.items():
settings = linter.get_linter_settings(linter_class, view)
could_lint_view = linter_class.matches_selector(view, settings)
if not could_lint_view:
continue
if linter_class.disabled is not None:
disabled = linter_class.disabled
else:
disabled = settings.get('disable')
action = not disabled
text = ('Enable: ' if disabled else 'Disable: ') + name
rv.append((action, linter_class, text))
return sorted(rv, key=lambda item: item[2])
class sublime_linter_addon_toggle_debug(sublime_plugin.WindowCommand):
def run(self):
current_mode = persist.settings.get('debug')
next_mode = not current_mode
self.window.status_message(
"{} debug mode".format('Enabling' if next_mode else 'Disabling'))
sublime.load_settings(
"SublimeLinter.sublime-settings").set('debug', next_mode)
class sublime_linter_addon_choose_lint_mode(sublime_plugin.WindowCommand):
def run(self, lint_mode=None):
if lint_mode not in ('background', 'load_save', 'manual', 'save'):
self.window.status_message(
"'{}' is not a valid lint_mode".format(lint_mode))
return
current_mode = persist.settings.get('lint_mode')
if lint_mode == current_mode:
self.window.status_message(
"Already in '{}' mode".format(lint_mode))
return
sublime.load_settings(
"SublimeLinter.sublime-settings").set('lint_mode', lint_mode)
def input(self, args):
if 'lint_mode' in args:
return None
return LintModeInputHandler()
class LintModeInputHandler(sublime_plugin.ListInputHandler):
def list_items(self):
current_mode = persist.settings.get('lint_mode')
all_modes = ['background', 'load_save', 'manual', 'save']
try:
selected = all_modes.index(current_mode)
except ValueError:
selected = 0
return (all_modes, selected)