forked from SublimeText/TrailingSpaces
-
Notifications
You must be signed in to change notification settings - Fork 14
/
highlight_whitespaces.py
234 lines (175 loc) · 6.81 KB
/
highlight_whitespaces.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'''
Marks all tabs and two or more spaces in each line with separate colors
Config summary (see README.md for details):
# key binding
{ "keys": ["ctrl+alt+w"], "command": "hws_toggle_whitespaces" }
# file settings
{
"highlight_whitespaces_space_highlight_scope_name": "invalid",
"highlight_whitespaces_tab_highlight_scope_name": "invalid",
"highlight_whitespaces_file_max_size": 1048576,
"highlight_whitespaces_enabled": true,
"highlight_whitespaces_check_spaces": true,
"highlight_whitespaces_check_tabs": true,
"highlight_whitespaces_single_space": false,
"highlight_last_whitespace": true
}
Forked from https://github.com/SublimeText/TrailingSpaces/
by Jean-Denis Vauguet <[email protected]>, Oktay Acikalin <[email protected]>
@author: Kemal Hadimli <[email protected]>
@contrib: Salvatore Poliandro III <[email protected]>
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
@since: 2015-06-05
'''
import sublime
import sublime_plugin
DEFAULT_MAX_FILE_SIZE = 1048576
DEFAULT_COLOR_SCOPE_NAME = "invalid"
DEFAULT_IS_ENABLED = True
DEFAULT_CHECK_SPACES = True
DEFAULT_SINGLE_SPACE = False
DEFAULT_CHECK_EOL = True
DEFAULT_CHECK_TABS = True
DEFAULT_CHECK_MIXED = True
DEFAULT_LAST_WHITESPACE = False
hws_toggled = False
hws_v3 = (int(sublime.version()) >= 3000)
def plugin_loaded():
global hws_v3
if is_enabled(True) and hws_v3:
highlight_whitespaces(sublime.active_window().active_view())
def is_enabled(init=False):
global hws_toggled
if init:
hws_toggled = bool(get_settings().get('highlight_whitespaces_enabled',
DEFAULT_IS_ENABLED))
return hws_toggled
def get_settings():
return sublime.load_settings('highlight_whitespaces.sublime-settings')
# Determine if the view is a find results view
def is_find_results(view):
return (view.settings().get('syntax') and
"Find Results" in view.settings().get('syntax'))
# Return an array of regions matching whitespaces.
def find_whitespaces_spaces(view):
hws_settings = get_settings()
last_whitespace = bool(hws_settings.get('highlight_last_whitespace',
DEFAULT_LAST_WHITESPACE))
single_space = bool(hws_settings.get('highlight_whitespaces_single_space',
DEFAULT_SINGLE_SPACE))
if single_space:
regex = ' +'
else:
regex = ' {2,}|\t | \t'
if last_whitespace:
regex += '| {1,}$'
return view.find_all(regex)
def find_whitespaces_tabs(view):
return view.find_all('\t+')
def find_whitespaces_eol(view):
return view.find_all('[\t ]+$')
def find_whitespaces_mixed(view):
return view.find_all('(\t )|( \t)')
# Highlight whitespaces
def highlight_whitespaces(view):
hws_settings = get_settings()
max_size = hws_settings.get('highlight_whitespaces_file_max_size',
DEFAULT_MAX_FILE_SIZE)
space_scope_name = \
hws_settings.get('highlight_whitespaces_space_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
tab_scope_name = \
hws_settings.get('highlight_whitespaces_tab_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
eol_scope_name = \
hws_settings.get('highlight_whitespaces_eol_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
mixed_scope_name = \
hws_settings.get('highlight_whitespaces_mixed_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
if view.size() <= max_size and not is_find_results(view):
if hws_settings.get('highlight_whitespaces_check_spaces',
DEFAULT_CHECK_SPACES):
space_regions = find_whitespaces_spaces(view)
view.add_regions('WhitespacesHighlightListener',
space_regions, space_scope_name, '',
sublime.DRAW_EMPTY)
if hws_settings.get('highlight_whitespaces_check_tabs',
DEFAULT_CHECK_TABS):
tab_regions = find_whitespaces_tabs(view)
view.add_regions('WhitespacesHighlightListener2',
tab_regions, tab_scope_name, '',
sublime.DRAW_EMPTY)
if hws_settings.get('highlight_whitespaces_check_eol', DEFAULT_CHECK_EOL):
eol_regions = find_whitespaces_eol(view)
view.add_regions('WhitespacesHighlightListener3',
eol_regions, eol_scope_name, '',
sublime.DRAW_EMPTY)
if hws_settings.get('highlight_whitespaces_check_mixed',
DEFAULT_CHECK_MIXED):
mixed_regions = find_whitespaces_mixed(view)
view.add_regions('WhitespacesHighlightListener4',
mixed_regions, mixed_scope_name, '',
sublime.DRAW_EMPTY)
# Clear all white spaces
def clear_whitespaces_highlight(window):
for view in window.views():
view.erase_regions('WhitespacesHighlightListener')
view.erase_regions('WhitespacesHighlightListener2')
view.erase_regions('WhitespacesHighlightListener3')
view.erase_regions('WhitespacesHighlightListener4')
# Toggle the event listner on or off
class HwsToggleWhitespacesCommand(sublime_plugin.WindowCommand):
def run(self):
global hws_toggled
hws_toggled = False if hws_toggled else True
# If toggling on, go ahead and perform a pass,
# else clear the highlighting in all views
if hws_toggled:
highlight_whitespaces(self.window.active_view())
else:
clear_whitespaces_highlight(self.window)
# Highlight matching regions.
class WhitespacesHighlightListener(sublime_plugin.EventListener):
def on_modified(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_activated(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_load(self, view):
if hws_toggled:
highlight_whitespaces(view)
class WhitespacesHighlightListener2(sublime_plugin.EventListener):
def on_modified(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_activated(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_load(self, view):
if hws_toggled:
highlight_whitespaces(view)
class WhitespacesHighlightListener3(sublime_plugin.EventListener):
def on_modified(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_activated(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_load(self, view):
if hws_toggled:
highlight_whitespaces(view)
class WhitespacesHighlightListener4(sublime_plugin.EventListener):
def on_modified(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_activated(self, view):
if hws_toggled:
highlight_whitespaces(view)
def on_load(self, view):
if hws_toggled:
highlight_whitespaces(view)
# pluging init for ST2
if not hws_v3:
plugin_loaded()