2011-10-17 21:15:03 +00:00
|
|
|
'''
|
|
|
|
Provides both a trailing spaces highlighter and a deletion command.
|
|
|
|
|
|
|
|
Config summary (see README.md for details):
|
|
|
|
|
|
|
|
# key binding
|
|
|
|
{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" }
|
|
|
|
|
|
|
|
# file settings
|
|
|
|
{
|
|
|
|
"trailing_spaces_highlight_color": "invalid",
|
|
|
|
"trailing_spaces_file_max_size": 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
@author: Jean-Denis Vauguet <jd@vauguet.fr>, Oktay Acikalin <ok@ryotic.de>
|
|
|
|
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
@since: 2011-02-25
|
|
|
|
'''
|
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
2011-10-17 21:15:03 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
DEFAULT_MAX_FILE_SIZE = 1048576
|
2011-10-17 21:15:03 +00:00
|
|
|
DEFAULT_COLOR_SCOPE_NAME = "invalid"
|
2011-10-19 00:14:17 +00:00
|
|
|
DEFAULT_IS_ENABLED = True
|
2011-10-18 23:00:42 +00:00
|
|
|
|
|
|
|
#Set whether the plugin is on or off
|
2011-10-20 01:30:30 +00:00
|
|
|
ts_settings = sublime.load_settings('trailing_spaces.sublime-settings')
|
|
|
|
trailing_spaces_enabled = bool(ts_settings.get('trailing_spaces_enabled',
|
|
|
|
DEFAULT_IS_ENABLED))
|
2011-10-17 21:15:03 +00:00
|
|
|
|
2012-06-18 10:01:47 +00:00
|
|
|
# Determine if the view is a find results view
|
|
|
|
def is_find_results(view):
|
2012-06-22 13:23:16 +00:00
|
|
|
return view.settings().get('syntax') and "Find Results" in view.settings().get('syntax')
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-17 21:15:03 +00:00
|
|
|
# Return an array of regions matching trailing spaces.
|
|
|
|
def find_trailing_spaces(view):
|
2012-05-01 02:38:29 +00:00
|
|
|
include_empty_lines = bool(ts_settings.get('trailing_spaces_include_empty_lines',
|
|
|
|
DEFAULT_IS_ENABLED))
|
|
|
|
return view.find_all('[ \t]+$' if include_empty_lines else '(?<=\S)[\t ]+$')
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-18 23:00:42 +00:00
|
|
|
# Highlight trailing spaces
|
|
|
|
def highlight_trailing_spaces(view):
|
2011-10-20 01:30:30 +00:00
|
|
|
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)
|
2012-06-18 10:01:47 +00:00
|
|
|
if view.size() <= max_size and not is_find_results(view):
|
2011-10-18 23:00:42 +00:00
|
|
|
regions = find_trailing_spaces(view)
|
|
|
|
view.add_regions('TrailingSpacesHighlightListener',
|
|
|
|
regions, color_scope_name,
|
|
|
|
sublime.DRAW_EMPTY)
|
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-18 23:00:42 +00:00
|
|
|
# Clear all trailing spaces
|
|
|
|
def clear_trailing_spaces_highlight(window):
|
|
|
|
for view in window.views():
|
|
|
|
view.erase_regions('TrailingSpacesHighlightListener')
|
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-18 23:00:42 +00:00
|
|
|
# Toggle the event listner on or off
|
|
|
|
class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand):
|
|
|
|
def run(self):
|
2011-10-20 01:30:30 +00:00
|
|
|
global trailing_spaces_enabled
|
|
|
|
trailing_spaces_enabled = False if trailing_spaces_enabled else True
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-20 01:30:30 +00:00
|
|
|
# If toggling on, go ahead and perform a pass,
|
|
|
|
# else clear the highlighting in all views
|
|
|
|
if trailing_spaces_enabled:
|
2011-10-18 23:00:42 +00:00
|
|
|
highlight_trailing_spaces(self.window.active_view())
|
|
|
|
else:
|
|
|
|
clear_trailing_spaces_highlight(self.window)
|
2011-10-17 21:15:03 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-17 21:15:03 +00:00
|
|
|
# Highlight matching regions.
|
|
|
|
class TrailingSpacesHighlightListener(sublime_plugin.EventListener):
|
|
|
|
def on_modified(self, view):
|
2011-10-20 01:30:30 +00:00
|
|
|
if trailing_spaces_enabled:
|
2011-10-18 23:00:42 +00:00
|
|
|
highlight_trailing_spaces(view)
|
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
def on_activated(self, view):
|
2011-10-20 01:30:30 +00:00
|
|
|
if trailing_spaces_enabled:
|
2011-10-18 23:00:42 +00:00
|
|
|
highlight_trailing_spaces(view)
|
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
def on_load(self, view):
|
2011-10-20 01:30:30 +00:00
|
|
|
if trailing_spaces_enabled:
|
2011-10-18 23:00:42 +00:00
|
|
|
highlight_trailing_spaces(view)
|
2011-10-17 21:15:03 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-17 21:15:03 +00:00
|
|
|
# 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
|
|
|
|
offset = 0
|
|
|
|
for region in regions:
|
|
|
|
r = sublime.Region(region.a + offset, region.b + offset)
|
|
|
|
self.view.erase(edit, sublime.Region(r.a, r.b))
|
|
|
|
offset -= r.size()
|
|
|
|
|
|
|
|
msg_parts = {"nbRegions": len(regions),
|
|
|
|
"plural": 's' if len(regions) > 1 else ''}
|
|
|
|
msg = "Deleted %(nbRegions)s trailing spaces region%(plural)s" % msg_parts
|
|
|
|
else:
|
|
|
|
msg = "No trailing spaces to delete!"
|
|
|
|
|
|
|
|
sublime.status_message(msg)
|