From 488e793fc56f9aa3282f6ba002e81f1402a09242 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Tue, 18 Oct 2011 17:00:42 -0600 Subject: [PATCH 1/4] -Add on_load and on_activated to find trailing spaces without being forced to edit file -Add Command to toggle on/off searching for trailing spaces -Initiate trailing space search on toggle "on" -Clear highlights on toggle "off" -Simplify find_trailing_spaces function --- trailing_spaces.py | 61 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/trailing_spaces.py b/trailing_spaces.py index 2ec6e99..72b1beb 100644 --- a/trailing_spaces.py +++ b/trailing_spaces.py @@ -19,35 +19,64 @@ Config summary (see README.md for details): import sublime, sublime_plugin -DEFAULT_MAX_FILE_SIZE = 1048576 +DEFAULT_MAX_FILE_SIZE = 1048576 DEFAULT_COLOR_SCOPE_NAME = "invalid" +DEFAULT_IS_ENABLED = False + +#Set whether the plugin is on or off +TrailingSpacesEnabled = DEFAULT_IS_ENABLED # Return an array of regions matching trailing spaces. def find_trailing_spaces(view): - trails = view.find_all('[ \t]+$') - regions = [] - for trail in trails: - regions.append(trail) - return regions + return view.find_all('[ \t]+$') + +# Highlight trailing spaces +def highlight_trailing_spaces(view): + max_size = view.settings().get('trailing_spaces_file_max_size', + DEFAULT_MAX_FILE_SIZE) + color_scope_name = view.settings().get('trailing_spaces_highlight_color', + DEFAULT_COLOR_SCOPE_NAME) + if view.size() <= max_size: + regions = find_trailing_spaces(view) + view.add_regions('TrailingSpacesHighlightListener', + regions, color_scope_name, + sublime.DRAW_EMPTY) + +# Clear all trailing spaces +def clear_trailing_spaces_highlight(window): + for view in window.views(): + view.erase_regions('TrailingSpacesHighlightListener') + +# Toggle the event listner on or off +class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand): + def run(self): + global TrailingSpacesEnabled + TrailingSpacesEnabled = False if(TrailingSpacesEnabled) else True + + # If toggling on go ahead and perform a pass, if not clear the highlighting in all views + if(TrailingSpacesEnabled): + highlight_trailing_spaces(self.window.active_view()) + else: + clear_trailing_spaces_highlight(self.window) # Highlight matching regions. class TrailingSpacesHighlightListener(sublime_plugin.EventListener): def on_modified(self, view): - max_size = view.settings().get('trailing_spaces_file_max_size', - DEFAULT_MAX_FILE_SIZE) - color_scope_name = view.settings().get('trailing_spaces_highlight_color', - DEFAULT_COLOR_SCOPE_NAME) - if view.size() <= max_size: - regions = find_trailing_spaces(view) - view.add_regions('TrailingSpacesHighlightListener', - regions, color_scope_name, - sublime.DRAW_EMPTY) + if(TrailingSpacesEnabled): + highlight_trailing_spaces(view) + + def on_activated(self,view): + if(TrailingSpacesEnabled): + highlight_trailing_spaces(view) + + def on_load(self,view): + if(TrailingSpacesEnabled): + highlight_trailing_spaces(view) # Allows to erase matching regions. class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand): def run(self, edit): regions = find_trailing_spaces(self.view) - if regions: # deleting a region changes the other regions positions, so we # handle this maintaining an offset From 40dcc62448a9264736c1a25a2b5d3ee23edf17b9 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Tue, 18 Oct 2011 17:08:29 -0600 Subject: [PATCH 2/4] -Change the enabling to True --- trailing_spaces.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trailing_spaces.py b/trailing_spaces.py index 72b1beb..a464fa4 100644 --- a/trailing_spaces.py +++ b/trailing_spaces.py @@ -21,7 +21,7 @@ import sublime, sublime_plugin DEFAULT_MAX_FILE_SIZE = 1048576 DEFAULT_COLOR_SCOPE_NAME = "invalid" -DEFAULT_IS_ENABLED = False +DEFAULT_IS_ENABLED = True #Set whether the plugin is on or off TrailingSpacesEnabled = DEFAULT_IS_ENABLED From 710a69fd87e52bdade66b3c3d6b81b593adbf2fd Mon Sep 17 00:00:00 2001 From: facelessuser Date: Tue, 18 Oct 2011 18:14:17 -0600 Subject: [PATCH 3/4] -ran pep8 and fixed suggestions except for a long line issue --- trailing_spaces.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/trailing_spaces.py b/trailing_spaces.py index a464fa4..ab07ad4 100644 --- a/trailing_spaces.py +++ b/trailing_spaces.py @@ -17,19 +17,22 @@ Config summary (see README.md for details): @since: 2011-02-25 ''' -import sublime, sublime_plugin +import sublime +import sublime_plugin -DEFAULT_MAX_FILE_SIZE = 1048576 +DEFAULT_MAX_FILE_SIZE = 1048576 DEFAULT_COLOR_SCOPE_NAME = "invalid" -DEFAULT_IS_ENABLED = True +DEFAULT_IS_ENABLED = True #Set whether the plugin is on or off TrailingSpacesEnabled = DEFAULT_IS_ENABLED + # Return an array of regions matching trailing spaces. def find_trailing_spaces(view): return view.find_all('[ \t]+$') + # Highlight trailing spaces def highlight_trailing_spaces(view): max_size = view.settings().get('trailing_spaces_file_max_size', @@ -42,37 +45,42 @@ def highlight_trailing_spaces(view): regions, color_scope_name, sublime.DRAW_EMPTY) + # Clear all trailing spaces def clear_trailing_spaces_highlight(window): for view in window.views(): view.erase_regions('TrailingSpacesHighlightListener') + # Toggle the event listner on or off class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand): def run(self): global TrailingSpacesEnabled - TrailingSpacesEnabled = False if(TrailingSpacesEnabled) else True + TrailingSpacesEnabled = False if TrailingSpacesEnabled else True - # If toggling on go ahead and perform a pass, if not clear the highlighting in all views - if(TrailingSpacesEnabled): + # If toggling on go ahead and perform a pass, + # if not clear the highlighting in all views + if TrailingSpacesEnabled: highlight_trailing_spaces(self.window.active_view()) else: clear_trailing_spaces_highlight(self.window) + # Highlight matching regions. class TrailingSpacesHighlightListener(sublime_plugin.EventListener): def on_modified(self, view): - if(TrailingSpacesEnabled): + if TrailingSpacesEnabled: highlight_trailing_spaces(view) - def on_activated(self,view): - if(TrailingSpacesEnabled): + def on_activated(self, view): + if TrailingSpacesEnabled: highlight_trailing_spaces(view) - def on_load(self,view): - if(TrailingSpacesEnabled): + def on_load(self, view): + if TrailingSpacesEnabled: highlight_trailing_spaces(view) + # Allows to erase matching regions. class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand): def run(self, edit): From 0c7c1e4ca4c13057a5c166253d91c12a3808650e Mon Sep 17 00:00:00 2001 From: facelessuser Date: Wed, 19 Oct 2011 19:30:30 -0600 Subject: [PATCH 4/4] -Add settings file and load it from main script -Change enable variable name so that it matches current naming convention -Add Trailing Space commands to the command palette so that delete and toggle can be called even without shortcuts defined --- Default.sublime-commands | 10 ++++++++++ trailing_spaces.py | 28 +++++++++++++++------------- trailing_spaces.sublime-settings | 10 ++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 Default.sublime-commands create mode 100644 trailing_spaces.sublime-settings diff --git a/Default.sublime-commands b/Default.sublime-commands new file mode 100644 index 0000000..dbc3c7b --- /dev/null +++ b/Default.sublime-commands @@ -0,0 +1,10 @@ +[ + { + "caption": "Trailing Spaces: Toggle Trailing Spaces", + "command": "toggle_trailing_spaces" + }, + { + "caption": "Trailing Spaces: Delete Trailing Spaces", + "command": "delete_trailing_spaces" + } +] diff --git a/trailing_spaces.py b/trailing_spaces.py index ab07ad4..003c2de 100644 --- a/trailing_spaces.py +++ b/trailing_spaces.py @@ -25,7 +25,9 @@ DEFAULT_COLOR_SCOPE_NAME = "invalid" DEFAULT_IS_ENABLED = True #Set whether the plugin is on or off -TrailingSpacesEnabled = DEFAULT_IS_ENABLED +ts_settings = sublime.load_settings('trailing_spaces.sublime-settings') +trailing_spaces_enabled = bool(ts_settings.get('trailing_spaces_enabled', + DEFAULT_IS_ENABLED)) # Return an array of regions matching trailing spaces. @@ -35,10 +37,10 @@ def find_trailing_spaces(view): # Highlight trailing spaces def highlight_trailing_spaces(view): - max_size = view.settings().get('trailing_spaces_file_max_size', - DEFAULT_MAX_FILE_SIZE) - color_scope_name = view.settings().get('trailing_spaces_highlight_color', - DEFAULT_COLOR_SCOPE_NAME) + max_size = ts_settings.get('trailing_spaces_file_max_size', + DEFAULT_MAX_FILE_SIZE) + color_scope_name = ts_settings.get('trailing_spaces_highlight_color', + DEFAULT_COLOR_SCOPE_NAME) if view.size() <= max_size: regions = find_trailing_spaces(view) view.add_regions('TrailingSpacesHighlightListener', @@ -55,12 +57,12 @@ def clear_trailing_spaces_highlight(window): # Toggle the event listner on or off class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand): def run(self): - global TrailingSpacesEnabled - TrailingSpacesEnabled = False if TrailingSpacesEnabled else True + global trailing_spaces_enabled + trailing_spaces_enabled = False if trailing_spaces_enabled else True - # If toggling on go ahead and perform a pass, - # if not clear the highlighting in all views - if TrailingSpacesEnabled: + # If toggling on, go ahead and perform a pass, + # else clear the highlighting in all views + if trailing_spaces_enabled: highlight_trailing_spaces(self.window.active_view()) else: clear_trailing_spaces_highlight(self.window) @@ -69,15 +71,15 @@ class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand): # Highlight matching regions. class TrailingSpacesHighlightListener(sublime_plugin.EventListener): def on_modified(self, view): - if TrailingSpacesEnabled: + if trailing_spaces_enabled: highlight_trailing_spaces(view) def on_activated(self, view): - if TrailingSpacesEnabled: + if trailing_spaces_enabled: highlight_trailing_spaces(view) def on_load(self, view): - if TrailingSpacesEnabled: + if trailing_spaces_enabled: highlight_trailing_spaces(view) diff --git a/trailing_spaces.sublime-settings b/trailing_spaces.sublime-settings new file mode 100644 index 0000000..1db0b4e --- /dev/null +++ b/trailing_spaces.sublime-settings @@ -0,0 +1,10 @@ +{ + // Color is determined by scope + "trailing_spaces_highlight_color" : "invalid", + + // Max file size to search + "trailing_spaces_file_max_size" : 1048576, + + // By default plugin is enabled or disabled (true|false) + "trailing_spaces_enabled" : true +}