Skip to content

Commit c07321e

Browse files
mheinzlerrchl
authored andcommitted
Extend the highlighting to non-visible text
Include some parts of the text outside the visible region in the highlighting to make highlights appear immediately after only a small amount of scrolling.
1 parent b502acb commit c07321e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

trailing_spaces.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
DEFAULT_MAX_FILE_SIZE = 1048576
2020
DEFAULT_IS_ENABLED = True
21+
DEFAULT_NON_VISIBLE_HIGHLIGHTING = 500
2122
DEFAULT_UPDATE_INTERVAL = 250
2223
DEFAULT_MODIFIED_LINES_ONLY = False
2324

@@ -27,6 +28,7 @@
2728
ts_settings_filename = "trailing_spaces.sublime-settings"
2829
ts_settings = None
2930
trailing_spaces_live_matching = DEFAULT_IS_ENABLED
31+
trailing_spaces_non_visible_highlighting = DEFAULT_NON_VISIBLE_HIGHLIGHTING
3032
trailing_spaces_update_interval = DEFAULT_UPDATE_INTERVAL
3133
trim_modified_lines_only = DEFAULT_MODIFIED_LINES_ONLY
3234
trailing_spaces_syntax_ignore = []
@@ -42,13 +44,15 @@
4244
# Returns nothing.
4345
def plugin_loaded():
4446
global ts_settings_filename, ts_settings, trailing_spaces_live_matching
45-
global trailing_spaces_update_interval
47+
global trailing_spaces_non_visible_highlighting, trailing_spaces_update_interval
4648
global current_highlighting_scope, trim_modified_lines_only, startup_queue
4749
global DEFAULT_COLOR_SCOPE_NAME, trailing_spaces_syntax_ignore
4850

4951
ts_settings = sublime.load_settings(ts_settings_filename)
5052
trailing_spaces_live_matching = bool(ts_settings.get("trailing_spaces_enabled",
5153
DEFAULT_IS_ENABLED))
54+
trailing_spaces_non_visible_highlighting = int(ts_settings.get("trailing_spaces_non_visible_highlighting",
55+
DEFAULT_UPDATE_INTERVAL))
5256
trailing_spaces_update_interval = int(ts_settings.get("trailing_spaces_update_interval",
5357
DEFAULT_UPDATE_INTERVAL))
5458
current_highlighting_scope = ts_settings.get("trailing_spaces_highlight_color",
@@ -118,8 +122,14 @@ def find_trailing_spaces(view):
118122
if not include_empty_lines:
119123
regexp = "(?<=\\S)%s$" % regexp
120124

121-
# find all matches in the currently visible region
122-
offending_lines = view_find_all_in_region(view, view.visible_region(), regexp)
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())
129+
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)
132+
123133
ignored_scopes = ",".join(ts_settings.get("trailing_spaces_scope_ignore", []))
124134
filtered_lines = []
125135
for region in offending_lines:

trailing_spaces.sublime-settings

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747

4848
// ---- NEXT SETTINGS ARE FOR POWER USERS ONLY! ----
4949

50+
// The number of characters before and after the visible region of text to
51+
// include in the highlighting. This is useful to also show the highlighting
52+
// immediately for text that just became visible through scrolling.
53+
// Adjust the value (in the number of characters) to whatever fits your
54+
// needs and performance.
55+
"trailing_spaces_non_visible_highlighting" : 500,
56+
5057
// This is the interval at which the active view is tested for changes
5158
// (due to scrolling) to update the highlighting of the currently visible
5259
// region of text.

0 commit comments

Comments
 (0)