-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
This commit is contained in:
facelessuser 2011-10-18 17:00:42 -06:00
parent 2b6eab2236
commit 488e793fc5

View File

@ -21,18 +21,17 @@ import sublime, sublime_plugin
DEFAULT_MAX_FILE_SIZE = 1048576 DEFAULT_MAX_FILE_SIZE = 1048576
DEFAULT_COLOR_SCOPE_NAME = "invalid" 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. # Return an array of regions matching trailing spaces.
def find_trailing_spaces(view): def find_trailing_spaces(view):
trails = view.find_all('[ \t]+$') return view.find_all('[ \t]+$')
regions = []
for trail in trails:
regions.append(trail)
return regions
# Highlight matching regions. # Highlight trailing spaces
class TrailingSpacesHighlightListener(sublime_plugin.EventListener): def highlight_trailing_spaces(view):
def on_modified(self, view):
max_size = view.settings().get('trailing_spaces_file_max_size', max_size = view.settings().get('trailing_spaces_file_max_size',
DEFAULT_MAX_FILE_SIZE) DEFAULT_MAX_FILE_SIZE)
color_scope_name = view.settings().get('trailing_spaces_highlight_color', color_scope_name = view.settings().get('trailing_spaces_highlight_color',
@ -43,11 +42,41 @@ class TrailingSpacesHighlightListener(sublime_plugin.EventListener):
regions, color_scope_name, regions, color_scope_name,
sublime.DRAW_EMPTY) 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):
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. # Allows to erase matching regions.
class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand): class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand):
def run(self, edit): def run(self, edit):
regions = find_trailing_spaces(self.view) regions = find_trailing_spaces(self.view)
if regions: if regions:
# deleting a region changes the other regions positions, so we # deleting a region changes the other regions positions, so we
# handle this maintaining an offset # handle this maintaining an offset