Skip to content

Commit 299333d

Browse files
authored
Fix ignoring trailing spaces on current line with multiple cursors (#137)
Pass all selection regions to `view_find_all_in_regions` and match trailing spaces in the so that we can exclude them all when `trailing_spaces_include_current_line` is `false`. Also renamed some variables for clarity and use more concise code. Resolves #111
1 parent d8cbb1e commit 299333d

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

trailing_spaces.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,21 @@ def persist_settings():
9191
# Private: Returns all regions within region that match regex.
9292
#
9393
# view - the view, you know
94-
# region - the region to search
94+
# regions - a list of regions to search
9595
# regex - the regex pattern to search for
9696
#
97-
# Returns all matching regions within region.
98-
def view_find_all_in_region(view, region, regex):
97+
# Returns all matching trailing regions within regions.
98+
def view_find_all_in_regions(view, regions, regex):
99+
found = []
100+
99101
# find all matches in the region's text
100-
text = view.substr(region)
101-
matches = re.finditer(regex, text, re.MULTILINE)
102+
for region in regions:
103+
text = view.substr(region)
104+
# translate positions to the region's starting position
105+
matches = re.finditer(regex, text, re.MULTILINE)
106+
found.extend(sublime.Region(m.start() + region.begin(), m.end() + region.begin()) for m in matches)
102107

103-
# return the found positions translated to the region's starting position
104-
return [sublime.Region(m.start() + region.begin(), m.end() + region.begin()) for m in matches]
108+
return found
105109

106110

107111
# Private: Get the regions matching trailing spaces.
@@ -123,7 +127,7 @@ def find_trailing_spaces(view, scan_only_visible=True):
123127
if not include_empty_lines:
124128
regexp = "(?<=\\S)%s$" % regexp
125129

126-
offending_lines = []
130+
trailing_regions = []
127131

128132
if scan_only_visible:
129133
# find all matches in the currently visible region plus a little before and after
@@ -132,28 +136,27 @@ def find_trailing_spaces(view, scan_only_visible=True):
132136
searched_region.b = min(searched_region.b + trailing_spaces_non_visible_highlighting, view.size())
133137

134138
searched_region = view.line(searched_region) # align to line start and end
135-
offending_lines = view_find_all_in_region(view, searched_region, regexp)
139+
trailing_regions = view_find_all_in_regions(view, [searched_region], regexp)
136140
else:
137-
offending_lines = view.find_all(regexp)
141+
trailing_regions = view.find_all(regexp)
138142

139143
ignored_scopes = ",".join(ts_settings.get("trailing_spaces_scope_ignore", []))
140-
filtered_lines = []
141-
for region in offending_lines:
142-
if ignored_scopes and view.match_selector(region.begin(), ignored_scopes):
143-
continue
144-
filtered_lines.append(region)
144+
# filter out ignored scopes
145+
trailing_regions = [
146+
region for region in trailing_regions
147+
if not ignored_scopes or not view.match_selector(region.begin(), ignored_scopes)
148+
]
145149

146150
sel = view.sel()
147-
line = len(sel) and view.line(sel[0].b)
148151

149-
if include_current_line or not line:
150-
return [filtered_lines, filtered_lines]
152+
if include_current_line or len(sel) == 0:
153+
return [trailing_regions, trailing_regions]
151154
else:
155+
selection_lines = [view.line(region.b) for region in sel]
152156
# find all matches in the current line and exclude them from highlighting
153-
current_offenders = view_find_all_in_region(view, line, regexp)
154-
highlightable = [r for r in filtered_lines if r not in current_offenders]
155-
156-
return [filtered_lines, highlightable]
157+
selection_offenders = view_find_all_in_regions(view, selection_lines, regexp)
158+
highlightable = [r for r in trailing_regions if r not in selection_offenders]
159+
return [trailing_regions, highlightable]
157160

158161

159162
# Private: Find the freaking trailing spaces in the view and flags them as such!

0 commit comments

Comments
 (0)