Skip to content

Commit d8cbb1e

Browse files
authored
Include whole file when running "Delete Trailing Spaces" command (#136)
Due to the optimization of only matching in the visible region, the command to delete trailing spaces in a file only worked on the visible region. Fix by adding a flag to specify that the whole document should be scanned and use that from the command.
1 parent c07321e commit d8cbb1e

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

trailing_spaces.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ def view_find_all_in_region(view, region, regex):
109109
# As the core regexp matches lines, the regions are, well, "per lines".
110110
#
111111
# view - the view, you know
112+
# scan_only_visible - whether to limit scanning to only visible region
112113
#
113114
# Returns both the list of regions which map to trailing spaces and the list of
114115
# regions which are to be highlighted, as a list [matched, highlightable].
115-
def find_trailing_spaces(view):
116+
def find_trailing_spaces(view, scan_only_visible=True):
116117
include_empty_lines = bool(ts_settings.get("trailing_spaces_include_empty_lines",
117118
DEFAULT_IS_ENABLED))
118119
include_current_line = bool(ts_settings.get("trailing_spaces_include_current_line",
@@ -122,13 +123,18 @@ def find_trailing_spaces(view):
122123
if not include_empty_lines:
123124
regexp = "(?<=\\S)%s$" % regexp
124125

125-
# find all matches in the currently visible region plus a little before and after
126-
searched_region = view.visible_region()
127-
searched_region.a = max(searched_region.a - trailing_spaces_non_visible_highlighting, 0)
128-
searched_region.b = min(searched_region.b + trailing_spaces_non_visible_highlighting, view.size())
126+
offending_lines = []
129127

130-
searched_region = view.line(searched_region) # align to line start and end
131-
offending_lines = view_find_all_in_region(view, searched_region, regexp)
128+
if scan_only_visible:
129+
# find all matches in the currently visible region plus a little before and after
130+
searched_region = view.visible_region()
131+
searched_region.a = max(searched_region.a - trailing_spaces_non_visible_highlighting, 0)
132+
searched_region.b = min(searched_region.b + trailing_spaces_non_visible_highlighting, view.size())
133+
134+
searched_region = view.line(searched_region) # align to line start and end
135+
offending_lines = view_find_all_in_region(view, searched_region, regexp)
136+
else:
137+
offending_lines = view.find_all(regexp)
132138

133139
ignored_scopes = ",".join(ts_settings.get("trailing_spaces_scope_ignore", []))
134140
filtered_lines = []
@@ -173,7 +179,6 @@ def match_trailing_spaces(view):
173179
return
174180

175181
(matched, highlightable) = find_trailing_spaces(view)
176-
add_trailing_spaces_regions(view, matched)
177182
highlight_trailing_spaces_regions(view, highlightable)
178183

179184

@@ -208,21 +213,6 @@ def max_size_exceeded(view):
208213
DEFAULT_MAX_FILE_SIZE)
209214

210215

211-
# Private: Marks specified regions as trailing spaces.
212-
#
213-
# view - the view, you know
214-
# regions - regions qualified as trailing spaces
215-
#
216-
# Returns nothing.
217-
def add_trailing_spaces_regions(view, regions):
218-
view.erase_regions('TrailingSpacesMatchedRegions')
219-
view.add_regions('TrailingSpacesMatchedRegions',
220-
regions,
221-
"",
222-
"",
223-
sublime.HIDE_ON_MINIMAP)
224-
225-
226216
# Private: Highlights specified regions as trailing spaces.
227217
#
228218
# It will use the scope enforced by the state of the toggable highlighting.
@@ -343,12 +333,7 @@ def get_modified_lines(view):
343333
#
344334
# Returns a list of regions to be deleted.
345335
def find_regions_to_delete(view):
346-
# If the plugin has been running in the background, regions have been matched.
347-
# Otherwise, we must find trailing spaces right now!
348-
if trailing_spaces_live_matching:
349-
regions = view.get_regions('TrailingSpacesMatchedRegions')
350-
else:
351-
(regions, highlightable) = find_trailing_spaces(view)
336+
(regions, highlightable) = find_trailing_spaces(view, scan_only_visible=False)
352337

353338
# Filtering is required in case triming is restricted to dirty regions only.
354339
if trim_modified_lines_only:

0 commit comments

Comments
 (0)