Skip to content

Commit 5f19dca

Browse files
committed
unhilight_editing_line: unhilight current line when in "edit mode"
Set "trailing_spaces_unhilight_editing_line": true to enable. Does its thing only if "trailing_spaces_include_current_line" is true. Fixes SublimeText#42.
1 parent 14223f6 commit 5f19dca

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

trailing_spaces.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
startup_queue = []
2828
on_disk = None
2929

30+
# NOTE: these are for trailing_spaces_unhilight_editing_line
31+
currently_modifying = {}
32+
last_modified_selection = {}
3033

3134
# Private: Loads settings and sets whether the plugin (live matching) is enabled.
3235
#
@@ -87,18 +90,26 @@ def find_trailing_spaces(view):
8790
DEFAULT_IS_ENABLED))
8891
include_current_line = bool(ts_settings.get("trailing_spaces_include_current_line",
8992
DEFAULT_IS_ENABLED))
93+
unhilight_editing_line = bool(ts_settings.get("trailing_spaces_unhilight_editing_line",
94+
False))
95+
9096
regexp = ts_settings.get("trailing_spaces_regexp") + "$"
9197
no_empty_lines_regexp = "(?<=\S)%s$" % regexp
9298

9399
offending_lines = view.find_all(regexp if include_empty_lines else no_empty_lines_regexp)
100+
highlightable = offending_lines
94101

95-
if include_current_line:
96-
return [offending_lines, offending_lines]
97-
else:
102+
if include_current_line and unhilight_editing_line:
103+
# remove current line(s) from hilightings if editing
104+
highlightable = [region for region in highlightable if
105+
view.rowcol(region.a)[0] not in currently_modifying[view.buffer_id()]]
106+
107+
elif not include_current_line:
98108
current_offender = view.find(regexp if include_empty_lines else no_empty_lines_regexp, line.a)
99109
removal = False if current_offender == None else line.intersects(current_offender)
100110
highlightable = [i for i in offending_lines if i != current_offender] if removal else offending_lines
101-
return [offending_lines, highlightable]
111+
112+
return [offending_lines, highlightable]
102113

103114

104115
# Private: Find the fraking trailing spaces in the view and flags them as such!
@@ -380,6 +391,9 @@ def is_checked(self):
380391
# current settings.
381392
class TrailingSpacesListener(sublime_plugin.EventListener):
382393
def on_modified(self, view):
394+
last_modified_selection[view.buffer_id()] = [s for s in view.sel()]
395+
currently_modifying[view.buffer_id()] = [view.rowcol(pos.a)[0] for pos in view.sel()]
396+
383397
if trailing_spaces_live_matching:
384398
match_trailing_spaces(view)
385399

@@ -388,10 +402,17 @@ def on_activated(self, view):
388402
match_trailing_spaces(view)
389403

390404
def on_selection_modified(self, view):
405+
# reset currently modifying lines if cursor changed place ("exit edit mode")
406+
if [s for s in view.sel()] != last_modified_selection[view.buffer_id()]:
407+
currently_modifying[view.buffer_id()] = set()
391408
if trailing_spaces_live_matching:
392409
match_trailing_spaces(view)
393410

394411
def on_activated(self, view):
412+
# initialize current buffer's important sets
413+
currently_modifying[view.buffer_id()] = set()
414+
last_modified_selection[view.buffer_id()] = set()
415+
395416
self.freeze_last_version(view)
396417
if trailing_spaces_live_matching:
397418
match_trailing_spaces(view)

0 commit comments

Comments
 (0)