@@ -91,17 +91,21 @@ def persist_settings():
91
91
# Private: Returns all regions within region that match regex.
92
92
#
93
93
# view - the view, you know
94
- # region - the region to search
94
+ # regions - a list of regions to search
95
95
# regex - the regex pattern to search for
96
96
#
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
+
99
101
# 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 )
102
107
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
105
109
106
110
107
111
# Private: Get the regions matching trailing spaces.
@@ -123,7 +127,7 @@ def find_trailing_spaces(view, scan_only_visible=True):
123
127
if not include_empty_lines :
124
128
regexp = "(?<=\\ S)%s$" % regexp
125
129
126
- offending_lines = []
130
+ trailing_regions = []
127
131
128
132
if scan_only_visible :
129
133
# 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):
132
136
searched_region .b = min (searched_region .b + trailing_spaces_non_visible_highlighting , view .size ())
133
137
134
138
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 )
136
140
else :
137
- offending_lines = view .find_all (regexp )
141
+ trailing_regions = view .find_all (regexp )
138
142
139
143
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
+ ]
145
149
146
150
sel = view .sel ()
147
- line = len (sel ) and view .line (sel [0 ].b )
148
151
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 ]
151
154
else :
155
+ selection_lines = [view .line (region .b ) for region in sel ]
152
156
# 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 ]
157
160
158
161
159
162
# Private: Find the freaking trailing spaces in the view and flags them as such!
0 commit comments