From 6cfb029215e1a54a4cc6cfa40c81ce87444178c3 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Tue, 17 Feb 2015 13:50:00 -0700 Subject: [PATCH] Adding end-of-line highlighting as an option --- README.md | 3 ++- highlight_whitespaces.py | 25 +++++++++++++++++++++++++ highlight_whitespaces.sublime-settings | 6 ++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 91214e0..e0dc593 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,8 @@ as "invalid", "comment"... in "File Settings - User": ``` { "highlight_whitespaces_space_highlight_scope_name": "invalid", - "highlight_whitespaces_tab_highlight_scope_name": "invalid" + "highlight_whitespaces_tab_highlight_scope_name": "invalid", + "highlight_whitespaces_eol_highlight_scope_name": "invalid" } ``` diff --git a/highlight_whitespaces.py b/highlight_whitespaces.py index 7d9a467..208ceaa 100644 --- a/highlight_whitespaces.py +++ b/highlight_whitespaces.py @@ -10,10 +10,12 @@ Config summary (see README.md for details): { "highlight_whitespaces_space_highlight_scope_name": "invalid", "highlight_whitespaces_tab_highlight_scope_name": "invalid", + "highlight_whitespaces_eol_highlight_scope_name": "invalid", "highlight_whitespaces_file_max_size": 1048576, "highlight_whitespaces_enabled": true, "highlight_whitespaces_check_spaces": true, "highlight_whitespaces_check_tabs": true, + "highlight_whitespaces_check_eol": true, "highlight_whitespaces_single_space": false, "highlight_last_whitespace": true } @@ -33,6 +35,7 @@ DEFAULT_COLOR_SCOPE_NAME = "invalid" DEFAULT_IS_ENABLED = True DEFAULT_CHECK_SPACES = True DEFAULT_SINGLE_SPACE = False +DEFAULT_CHECK_EOL = True DEFAULT_CHECK_TABS = True DEFAULT_LAST_WHITESPACE = False @@ -66,6 +69,8 @@ def find_whitespaces_spaces(view): def find_whitespaces_tabs(view): return view.find_all('\t+') +def find_whitespaces_eol(view): + return view.find_all('[\t ]+$') # Highlight whitespaces def highlight_whitespaces(view): @@ -77,6 +82,8 @@ def highlight_whitespaces(view): DEFAULT_COLOR_SCOPE_NAME) tab_scope_name = hws_settings.get('highlight_whitespaces_tab_highlight_scope_name', DEFAULT_COLOR_SCOPE_NAME) + eol_scope_name = hws_settings.get('highlight_whitespaces_eol_highlight_scope_name', + DEFAULT_COLOR_SCOPE_NAME) if view.size() <= max_size and not is_find_results(view): if hws_settings.get('highlight_whitespaces_check_spaces', DEFAULT_CHECK_SPACES): space_regions = find_whitespaces_spaces(view) @@ -88,6 +95,11 @@ def highlight_whitespaces(view): view.add_regions('WhitespacesHighlightListener2', tab_regions, tab_scope_name, '', sublime.DRAW_EMPTY) + if hws_settings.get('highlight_whitespaces_check_eol', DEFAULT_CHECK_EOL): + eol_regions = find_whitespaces_eol(view) + view.add_regions('WhitespacesHighlightListener3', + eol_regions, eol_scope_name, '', + sublime.DRAW_EMPTY) # Clear all white spaces @@ -95,6 +107,7 @@ def clear_whitespaces_highlight(window): for view in window.views(): view.erase_regions('WhitespacesHighlightListener') view.erase_regions('WhitespacesHighlightListener2') + view.erase_regions('WhitespacesHighlightListener3') # Toggle the event listner on or off @@ -138,3 +151,15 @@ class WhitespacesHighlightListener2(sublime_plugin.EventListener): if hws_enabled: highlight_whitespaces(view) +class WhitespacesHighlightListener3(sublime_plugin.EventListener): + def on_modified(self, view): + if hws_enabled: + highlight_whitespaces(view) + + def on_activated(self, view): + if hws_enabled: + highlight_whitespaces(view) + + def on_load(self, view): + if hws_enabled: + highlight_whitespaces(view) diff --git a/highlight_whitespaces.sublime-settings b/highlight_whitespaces.sublime-settings index bfce20c..a1c44b3 100644 --- a/highlight_whitespaces.sublime-settings +++ b/highlight_whitespaces.sublime-settings @@ -5,6 +5,9 @@ // Tabs color is determined by scope (default, "invalid") // "highlight_whitespaces_tab_highlight_scope_name": "Whitespaces.tab.highlight", +// End-of-line color is determined by scope (default, "invalid") +// "highlight_whitespaces_eol_highlight_scope_name": "Whitespaces.eol.highlight", + // Max file size to search "highlight_whitespaces_file_max_size": 1048576, @@ -17,6 +20,9 @@ // Whether to check for tabs "highlight_whitespaces_check_tabs": true, +// Whether to check for end-of-line whitespace + "highlight_whitespaces_check_eol": true, + // Allow the highlighting of the last whitespace, one or more time "highlight_last_whitespace": true }