8
8
@since: 2011-02-25
9
9
'''
10
10
11
- import sublime
12
- import sublime_plugin
13
- import difflib
11
+ from .settings import TrailingSpacesSettings
12
+ from os .path import isfile
14
13
import codecs
14
+ import difflib
15
15
import re
16
-
17
- from os .path import isfile
18
- from collections import ChainMap
19
- from sublime_lib import NamedSettingsDict
20
-
21
- SETTINGS_FILENAME = 'trailing_spaces.sublime-settings'
22
- # Only need defaults for settings that are not exposed in default settings.
23
- DEFAULT_SETTINGS = {
24
- 'syntax_ignore' : [],
25
- }
16
+ import sublime
17
+ import sublime_plugin
26
18
27
19
# dictionary of currently active view ids and last visible regions
28
20
active_views = {}
29
- current_highlight_color = None
21
+ current_highlight_color = ''
30
22
on_disk = None
31
23
# Highlight color as defined in settings. Plugin mutates that setting when disabled so
32
24
# that has to be stored.
33
- INITIAL_HIGHLIGHT_COLOR = None
25
+ INITIAL_HIGHLIGHT_COLOR = ''
34
26
HIGHLIGHT_REGION_KEY = 'TrailingSpacesHighlightedRegions'
35
- settings = None
36
- named_settings = None
27
+ settings = TrailingSpacesSettings ()
37
28
38
29
39
30
def plugin_loaded ():
40
- global settings , named_settings , current_highlight_color , INITIAL_HIGHLIGHT_COLOR
41
-
42
- # A settings layer that handles settings with the `trailing_spaces_` prefix (now deprecated).
43
- class DeprecatedSettingsDict (NamedSettingsDict ):
44
- def __getitem__ (self , key ):
45
- return super ().__getitem__ ('trailing_spaces_%s' % key )
31
+ global current_highlight_color , INITIAL_HIGHLIGHT_COLOR
46
32
47
- deprecated_settings = DeprecatedSettingsDict (SETTINGS_FILENAME )
48
- named_settings = NamedSettingsDict (SETTINGS_FILENAME )
49
- settings = ChainMap (deprecated_settings , named_settings , DEFAULT_SETTINGS )
33
+ settings .load ()
50
34
51
- current_highlight_color = settings [ ' highlight_color' ]
35
+ current_highlight_color = settings . highlight_color
52
36
INITIAL_HIGHLIGHT_COLOR = current_highlight_color
53
37
54
- if not settings [ ' enabled' ] :
38
+ if not settings . enabled :
55
39
current_highlight_color = ""
56
- if settings [ ' highlight_color' ] != current_highlight_color :
57
- named_settings .save ()
40
+ if settings . highlight_color != current_highlight_color :
41
+ settings .save ()
58
42
59
43
60
44
# Private: Makes sure all timers are stopped.
@@ -98,16 +82,16 @@ def view_find_all_in_regions(view, regions, regex):
98
82
# Returns both the list of regions which map to trailing spaces and the list of
99
83
# regions which are to be highlighted, as a list [matched, highlightable].
100
84
def find_trailing_spaces (view , scan_only_visible = True ):
101
- include_empty_lines = settings [ ' include_empty_lines' ]
102
- include_current_line = settings [ ' include_current_line' ]
103
- regexp = settings [ ' regexp' ] + "$"
85
+ include_empty_lines = settings . include_empty_lines
86
+ include_current_line = settings . include_current_line
87
+ regexp = settings . regexp + "$"
104
88
105
89
if not include_empty_lines :
106
90
regexp = "(?<=\\ S)%s$" % regexp
107
91
108
92
trailing_regions = []
109
93
110
- non_visible_highlighting = settings [ ' non_visible_highlighting' ]
94
+ non_visible_highlighting = settings . non_visible_highlighting
111
95
112
96
if scan_only_visible :
113
97
# find all matches in the currently visible region plus a little before and after
@@ -120,7 +104,7 @@ def find_trailing_spaces(view, scan_only_visible=True):
120
104
else :
121
105
trailing_regions = view .find_all (regexp )
122
106
123
- ignored_scopes = "," .join (settings [ ' scope_ignore' ] )
107
+ ignored_scopes = "," .join (settings . scope_ignore )
124
108
# filter out ignored scopes
125
109
trailing_regions = [
126
110
region for region in trailing_regions
@@ -176,7 +160,7 @@ def ignore_view(view):
176
160
if not view_syntax or view_settings .get ('is_widget' ):
177
161
return False
178
162
179
- for syntax_ignore in settings [ ' syntax_ignore' ] :
163
+ for syntax_ignore in settings . syntax_ignore :
180
164
if syntax_ignore in view_syntax :
181
165
return True
182
166
@@ -189,7 +173,7 @@ def ignore_view(view):
189
173
#
190
174
# Returns True or False.
191
175
def max_size_exceeded (view ):
192
- return view .size () > settings [ ' file_max_size' ]
176
+ return view .size () > settings . file_max_size
193
177
194
178
195
179
# Private: Highlights specified regions as trailing spaces.
@@ -222,7 +206,7 @@ def toggle_highlighting(view):
222
206
223
207
# If performing live, highlighted trailing regions must be updated
224
208
# internally.
225
- if not settings [ ' enabled' ] :
209
+ if not settings . enabled :
226
210
(matched , highlightable ) = find_trailing_spaces (view )
227
211
highlight_trailing_spaces_regions (view , highlightable )
228
212
@@ -306,7 +290,7 @@ def find_regions_to_delete(view):
306
290
(regions , highlightable ) = find_trailing_spaces (view , scan_only_visible = False )
307
291
308
292
# Filtering is required in case triming is restricted to dirty regions only.
309
- if settings [ ' modified_lines_only' ] :
293
+ if settings . modified_lines_only :
310
294
modified_lines = get_modified_lines (view )
311
295
312
296
# If there are no dirty lines, don't do nothing.
@@ -378,8 +362,8 @@ def run(self):
378
362
return
379
363
380
364
state = toggle_highlighting (view )
381
- named_settings [ ' highlight_color' ] = current_highlight_color
382
- named_settings .save ()
365
+ settings . highlight_color = current_highlight_color
366
+ settings .save ()
383
367
sublime .status_message ('Highlighting of trailing spaces is %s' % state )
384
368
385
369
def is_checked (self ):
@@ -389,47 +373,47 @@ def is_checked(self):
389
373
# Public: Toggles "Modified Lines Only" mode on or off.
390
374
class ToggleTrailingSpacesModifiedLinesOnlyCommand (sublime_plugin .WindowCommand ):
391
375
def run (self ):
392
- was_on = settings [ ' modified_lines_only' ]
393
- named_settings [ ' modified_lines_only' ] = not was_on
394
- named_settings .save ()
376
+ was_on = settings . modified_lines_only
377
+ settings . modified_lines_only = not was_on
378
+ settings .save ()
395
379
396
380
message = "Let's trim trailing spaces everywhere" if was_on \
397
381
else "Let's trim trailing spaces only on modified lines"
398
382
sublime .status_message (message )
399
383
400
384
def is_checked (self ):
401
- return settings [ ' modified_lines_only' ]
385
+ return settings . modified_lines_only
402
386
403
387
404
388
# Public: Matches and highlights trailing spaces on key events, according to the
405
389
# current settings.
406
390
class TrailingSpacesListener (sublime_plugin .EventListener ):
407
391
def on_modified_async (self , view ):
408
- if settings [ ' enabled' ] :
392
+ if settings . enabled :
409
393
match_trailing_spaces (view )
410
394
411
395
def on_selection_modified_async (self , view ):
412
- if settings [ ' enabled' ] :
396
+ if settings . enabled :
413
397
match_trailing_spaces (view )
414
398
415
399
def on_activated_async (self , view ):
416
- if settings [ ' modified_lines_only' ] :
400
+ if settings . modified_lines_only :
417
401
self .freeze_last_version (view )
418
402
419
- if settings [ ' enabled' ] :
403
+ if settings . enabled :
420
404
match_trailing_spaces (view )
421
405
422
406
# continuously watch view for changes to the visible region
423
- if not view .id () in active_views :
407
+ if view .id () not in active_views :
424
408
# track
425
409
active_views [view .id ()] = view .visible_region ()
426
410
self .update_on_region_change (view )
427
411
428
412
def on_pre_save (self , view ):
429
- if settings [ ' modified_lines_only' ] :
413
+ if settings . modified_lines_only :
430
414
self .freeze_last_version (view )
431
415
432
- if settings [ ' trim_on_save' ] :
416
+ if settings . trim_on_save :
433
417
view .run_command ("delete_trailing_spaces" )
434
418
435
419
def on_close (self , view ):
@@ -449,9 +433,8 @@ def update_on_region_change(self, view):
449
433
active_views [view .id ()] = view .visible_region ()
450
434
451
435
# continue only if the view is still active
452
- if settings ['enabled' ] and view .id () in active_views :
453
- sublime .set_timeout_async (lambda : self .update_on_region_change (view ),
454
- settings ['update_interval' ])
436
+ if settings .enabled and view .id () in active_views :
437
+ sublime .set_timeout_async (lambda : self .update_on_region_change (view ), settings .update_interval )
455
438
456
439
# Toggling messes with what is red from the disk, and it breaks the diff
457
440
# used when modified_lines_only is true. Honestly, I don't know why (yet).
@@ -522,7 +505,7 @@ def run(self, edit):
522
505
deleted = delete_trailing_regions (self .view , edit )
523
506
524
507
if deleted :
525
- if settings [ ' save_after_trim' ] and not settings [ ' trim_on_save' ] :
508
+ if settings . save_after_trim and not settings . trim_on_save :
526
509
sublime .set_timeout (lambda : self .save (self .view ), 10 )
527
510
528
511
msg_parts = {"nbRegions" : deleted ,
0 commit comments