forked from SublimeText/TrailingSpaces
-
Notifications
You must be signed in to change notification settings - Fork 3
/
trailing_spaces.py
453 lines (375 loc) · 16.2 KB
/
trailing_spaces.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
'''
Provides both a trailing spaces highlighter and a deletion command.
See README.md for details.
@author: Jean-Denis Vauguet <jd@vauguet.fr>, Oktay Acikalin <ok@ryotic.de>
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
@since: 2011-02-25
'''
import sublime
import sublime_plugin
import difflib
import codecs
DEFAULT_MAX_FILE_SIZE = 1048576
DEFAULT_IS_ENABLED = True
DEFAULT_MODIFIED_LINES_ONLY = False
# Global settings object and flags.
# Flags duplicate some of the (core) JSON settings, in case the settings file has
# been corrupted or is empty (ST2 really dislikes that!)
ts_settings_filename = "trailing_spaces.sublime-settings"
ts_settings = None
trailing_spaces_live_matching = DEFAULT_IS_ENABLED
trim_modified_lines_only = DEFAULT_MODIFIED_LINES_ONLY
startup_queue = []
on_disk = None
# Private: Loads settings and sets whether the plugin (live matching) is enabled.
#
# Returns nothing.
def plugin_loaded():
global ts_settings_filename, ts_settings, trailing_spaces_live_matching
global current_highlighting_scope, trim_modified_lines_only, startup_queue
global DEFAULT_COLOR_SCOPE_NAME, on_disk
ts_settings = sublime.load_settings(ts_settings_filename)
trailing_spaces_live_matching = bool(ts_settings.get("trailing_spaces_enabled",
DEFAULT_IS_ENABLED))
current_highlighting_scope = ts_settings.get("trailing_spaces_highlight_color",
"invalid")
DEFAULT_COLOR_SCOPE_NAME = current_highlighting_scope
trim_modified_lines_only = bool(ts_settings.get("trailing_spaces_modified_lines_only",
DEFAULT_MODIFIED_LINES_ONLY))
if trailing_spaces_live_matching:
for view in startup_queue:
match_trailing_spaces(view)
else:
current_highlighting_scope = ""
if ts_settings.get("trailing_spaces_highlight_color") != current_highlighting_scope:
persist_settings()
# Private: Updates user's settings with in-memory values.
#
# Allows for persistent settings from the menu.
#
# Returns nothing.
def persist_settings():
sublime.save_settings(ts_settings_filename)
# Private: Determine if the view is a "Find results" view.
#
# view - the view, you know
#
# Returns True or False.
def is_find_results(view):
return view.settings().get('syntax') and "Find Results" in view.settings().get('syntax')
# Private: Get the regions matching trailing spaces.
#
# As the core regexp matches lines, the regions are, well, "per lines".
#
# view - the view, you know
#
# Returns both the list of regions which map to trailing spaces and the list of
# regions which are to be highlighted, as a list [matched, highlightable].
def find_trailing_spaces(view):
sel = view.sel()[0]
line = view.line(sel.b)
include_empty_lines = bool(ts_settings.get("trailing_spaces_include_empty_lines",
DEFAULT_IS_ENABLED))
include_current_line = bool(ts_settings.get("trailing_spaces_include_current_line",
DEFAULT_IS_ENABLED))
regexp = ts_settings.get("trailing_spaces_regexp") + "$"
no_empty_lines_regexp = "(?<=\S)%s$" % regexp
offending_lines = view.find_all(regexp if include_empty_lines else no_empty_lines_regexp)
if include_current_line:
return [offending_lines, offending_lines]
else:
current_offender = view.find(regexp if include_empty_lines else no_empty_lines_regexp, line.a)
removal = False if current_offender == None else line.intersects(current_offender)
highlightable = [i for i in offending_lines if i != current_offender] if removal else offending_lines
return [offending_lines, highlightable]
# Private: Find the fraking trailing spaces in the view and flags them as such!
#
# It will refresh highlighted regions as well. Does not execute if the
# document's size exceeds the file_max_size setting, or if the fired in a view
# which is not a legacy document (helper/build views and so on).
#
# view - the view, you know
#
# Returns nothing.
def match_trailing_spaces(view):
if ts_settings is None:
startup_queue.append(view)
return
# Silently pass if file is too big.
if max_size_exceeded(view):
return
if not is_find_results(view):
(matched, highlightable) = find_trailing_spaces(view)
add_trailing_spaces_regions(view, matched)
highlight_trailing_spaces_regions(view, highlightable)
# Private: Checks whether the document is bigger than the max_size setting.
#
# view - the view, you know
#
# Returns True or False.
def max_size_exceeded(view):
return view.size() > ts_settings.get('trailing_spaces_file_max_size',
DEFAULT_MAX_FILE_SIZE)
# Private: Marks specified regions as trailing spaces.
#
# view - the view, you know
# regions - regions qualified as trailing spaces
#
# Returns nothing.
def add_trailing_spaces_regions(view, regions):
view.erase_regions('TrailingSpacesMatchedRegions')
view.add_regions('TrailingSpacesMatchedRegions',
regions,
"",
"",
sublime.HIDE_ON_MINIMAP)
# Private: Highlights specified regions as trailing spaces.
#
# It will use the scope enforced by the state of the toggable highlighting.
#
# view - the view, you know
# regions - regions qualified as trailing spaces
#
# Returns nothing.
def highlight_trailing_spaces_regions(view, regions):
view.erase_regions("TrailingSpacesHighlightedRegions")
view.add_regions('TrailingSpacesHighlightedRegions',
regions,
current_highlighting_scope or "",
"",
sublime.HIDE_ON_MINIMAP)
# Private: Toggles highlighting of all trailing spaces in the view.
#
# It has no effect is the plugin is disabled.
#
# view - the view, you know
#
# Returns True (highlighting was turned on) or False (turned off).
def toggle_highlighting(view):
global current_highlighting_scope
# If the scope is that of an invisible, there is nothing to toggle.
if DEFAULT_COLOR_SCOPE_NAME == "":
return "disabled!"
# If performing live, highlighted trailing regions must be updated
# internally.
if not trailing_spaces_live_matching:
(matched, highlightable) = find_trailing_spaces(view)
highlight_trailing_spaces_regions(view, highlightable)
scope = DEFAULT_COLOR_SCOPE_NAME if current_highlighting_scope == "" else ""
current_highlighting_scope = scope
highlight_trailing_spaces_regions(view, view.get_regions('TrailingSpacesHighlightedRegions'))
return "off" if current_highlighting_scope == "" else "on"
# Clear all the highlighted regions in all views.
#
# FIXME: this is not used! Delete?
#
# window - the window, you know
#
# Returns nothing.
def clear_trailing_spaces_highlight(window):
for view in window.views():
view.erase_regions('TrailingSpacesMatchedRegions')
# Find edited lines since last save, as line numbers, based on diff.
#
# It uses a Differ object to compute the diff between the file as red on the
# disk, and the current buffer (which may differ from the disk's state). See
# http://docs.python.org/2/library/difflib.html for details about diff codes.
#
# It relies on a full diff, so it may be expensive computation for very large
# files (diff generation + looping through all lines).
#
# old - a buffer of lines, as in "old version"
# new - a buffer of lines, as in "new version"
#
# Returns the list of edited line numbers.
def modified_lines_as_numbers(old, new):
d = difflib.Differ()
diffs = d.compare(old, new)
# Pretty Naive Algorithm (tm):
# - split off the "Differ code", to check whether:
# - the line is in either in both files or just b: increment the line number
# - the line is only in b: it qualifies as an edited line!
# Starting from -1 as ST2 is internally 0-based for lines.
lineNum = -1
edited_lines = []
for line in diffs:
code = line[:2]
# those lines with "? " are not real! watch out!
if code in (" ", "+ "):
lineNum += 1
if code == "+ ":
edited_lines.append(lineNum)
return False if not edited_lines else edited_lines
# Private: Find the dirty lines.
#
# view - the view, you know
#
# Returns the list of regions matching dirty lines.
def get_modified_lines(view):
try:
on_disk
on_buffer = view.substr(sublime.Region(0, view.size())).splitlines()
except UnicodeDecodeError:
sublime.status_message("File format incompatible with this feature (UTF-8 files only)")
return
lines = []
line_numbers = modified_lines_as_numbers(on_disk, on_buffer)
if line_numbers:
lines = [view.full_line(view.text_point(number,0)) for number in line_numbers]
return lines
# Private: Finds the trailing spaces regions to be deleted.
#
# It abides by the user settings: while in mode "Only Modified Lines", it returns
# the subset of trailing spaces regions which are within dirty lines; otherwise, it
# returns all trailing spaces regions for the document.
#
# view - the view, you know
#
# Returns a list of regions to be deleted.
def find_regions_to_delete(view):
# If the plugin has been running in the background, regions have been matched.
# Otherwise, we must find trailing spaces right now!
if trailing_spaces_live_matching:
regions = view.get_regions('TrailingSpacesMatchedRegions')
else:
(regions, highlightable) = find_trailing_spaces(view)
# Filtering is required in case triming is restricted to dirty regions only.
if trim_modified_lines_only:
modified_lines = get_modified_lines(view)
# If there are no dirty lines, don't do nothing.
if not modified_lines:
return
# Super-private: filters trailing spaces regions to dirty lines only.
#
# As one cannot perform a smart find_all within arbitrary boundaries, we must do some
# extra work:
# - we want to loop through the modified lines set, not the whole trailing regions
# - but we need a way to match modified lines with trailings to those very regions
#
# Hence the reversed dict on regions: keys are the text_point of the begining of
# each region, values are the region's actual boundaries. As a Region is unhashable,
# trailing regions are being recreated later on from those two values.
#
# We loop then loop through the modified lines: for each line, we get its begining
# text_point, and check whether it matches a line with trailing spaces in the
# reversed dict. If so, this is a match (a modified line with trailing spaces), so
# we can re-create and store a Region for the relevant trailing spaces boundaries.
#
# Returns the filtered list of trailing spaces regions for the modified lines set.
def only_those_with_trailing_spaces():
regions_by_begin = {}
matches = []
for region in regions:
begin = view.line(region).begin()
regions_by_begin[begin] = (region.begin(), region.end())
for line in modified_lines:
text_point = line.begin()
if text_point in regions_by_begin:
matches.append(sublime.Region(regions_by_begin[text_point][0], regions_by_begin[text_point][1]))
return matches
regions = only_those_with_trailing_spaces()
return regions
# Private: Deletes the trailing spaces regions.
#
# view - the view, you know
# edit - the Edit object spawned by the deletion command
#
# Returns the number of deleted regions.
def delete_trailing_regions(view, edit):
regions = find_regions_to_delete(view)
if regions:
# Trick: reversing the regions takes care of the growing offset while
# deleting the successive regions.
regions.reverse()
for r in regions:
view.erase(edit, r)
return len(regions)
else:
return 0
# Public: Toggles the highlighting on or off.
class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
if max_size_exceeded(view):
sublime.status_message("File is too big, trailing spaces handling disabled.")
return
state = toggle_highlighting(view)
ts_settings.set("trailing_spaces_highlight_color", current_highlighting_scope)
persist_settings()
sublime.status_message('Highlighting of trailing spaces is %s' % state)
def is_checked(self):
return current_highlighting_scope != ""
# Public: Toggles "Modified Lines Only" mode on or off.
class ToggleTrailingSpacesModifiedLinesOnlyCommand(sublime_plugin.WindowCommand):
def run(self):
global trim_modified_lines_only
was_on = ts_settings.get("trailing_spaces_modified_lines_only")
ts_settings.set("trailing_spaces_modified_lines_only", not was_on)
persist_settings()
# TODO: use ts_settings.add_on_change() when it lands in ST3
trim_modified_lines_only = ts_settings.get('trailing_spaces_modified_lines_only')
message = "Let's trim trailing spaces everywhere" if was_on \
else "Let's trim trailing spaces only on modified lines"
sublime.status_message(message)
def is_checked(self):
return ts_settings.get("trailing_spaces_modified_lines_only")
# Public: Matches and highlights trailing spaces on key events, according to the
# current settings.
class TrailingSpacesListener(sublime_plugin.EventListener):
def on_modified(self, view):
if trailing_spaces_live_matching:
match_trailing_spaces(view)
def on_activated(self, view):
if trailing_spaces_live_matching:
match_trailing_spaces(view)
def on_selection_modified(self, view):
if trailing_spaces_live_matching:
match_trailing_spaces(view)
def on_activated(self, view):
self.freeze_last_version(view)
if trailing_spaces_live_matching:
match_trailing_spaces(view)
def on_pre_save(self, view):
self.freeze_last_version(view)
if ts_settings.get("trailing_spaces_trim_on_save"):
view.run_command("delete_trailing_spaces")
# Toggling messes with what is red from the disk, and it breaks the diff
# used when modified_lines_only is true. Honestly, I don't know why (yet).
# Anyway, let's cache the persisted version of the document's buffer for
# later use on specific event, so that we always have a decent version of
# "what's on the disk" to work with.
def freeze_last_version(self, view):
global on_disk
file_name = view.file_name()
# For some reasons, the on_activated hook gets fired on a ghost document
# from time to time.
if file_name:
on_disk = codecs.open(file_name, "r", "utf-8").read().splitlines()
# Public: Deletes the trailing spaces.
class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand):
def run(self, edit):
if max_size_exceeded(self.view):
sublime.status_message("File is too big, trailing spaces handling disabled.")
return
deleted = delete_trailing_regions(self.view, edit)
if deleted:
if ts_settings.get("trailing_spaces_save_after_trim") \
and not ts_settings.get("trailing_spaces_trim_on_save"):
sublime.set_timeout(lambda: self.save(self.view), 10)
msg_parts = {"nbRegions": deleted,
"plural": 's' if deleted > 1 else ''}
message = "Deleted %(nbRegions)s trailing spaces region%(plural)s" % msg_parts
else:
message = "No trailing spaces to delete!"
sublime.status_message(message)
def save(self, view):
if view.file_name() is None:
view.run_command('prompt_save_as')
else:
view.run_command('save')
# ST3 features a plugin_loaded hook which is called when ST's API is ready.
#
# We must therefore call our init callback manually on ST2. It must be the last
# thing in this plugin (thanks, beloved contributors!).
if not int(sublime.version()) > 3000:
plugin_loaded()