highlight tabs and double-spaces, modify default settings

This commit is contained in:
Kemal Hadimli 2012-10-05 00:28:45 +03:00
parent b20299a0ea
commit af12f19988
2 changed files with 71 additions and 67 deletions

View File

@ -1,20 +1,24 @@
''' '''
Provides both a trailing spaces highlighter and a deletion command. Marks all tabs and two or more spaces in each line with separate colors
Config summary (see README.md for details): Config summary (see README.md for details):
# key binding # key binding
{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" } { "keys": ["ctrl+alt+w"], "command": "hws_toggle_whitespaces" }
# file settings # file settings
{ {
"trailing_spaces_highlight_color": "invalid", "highlight_whitespaces_space_highlight_scope_name": "invalid",
"trailing_spaces_file_max_size": 1000 "highlight_whitespaces_tab_highlight_scope_name": "invalid",
"highlight_whitespaces_file_max_size": 1048576,
"highlight_whitespaces_enabled": true
} }
@author: Jean-Denis Vauguet <jd@vauguet.fr>, Oktay Acikalin <ok@ryotic.de> Forked from https://github.com/SublimeText/TrailingSpaces/ by Jean-Denis Vauguet <jd@vauguet.fr>, Oktay Acikalin <ok@ryotic.de>
@author: Kemal Hadimli <disq@sf.net>
@license: MIT (http://www.opensource.org/licenses/mit-license.php) @license: MIT (http://www.opensource.org/licenses/mit-license.php)
@since: 2011-02-25 @since: 2012-10-05
''' '''
import sublime import sublime
@ -25,86 +29,86 @@ DEFAULT_COLOR_SCOPE_NAME = "invalid"
DEFAULT_IS_ENABLED = True DEFAULT_IS_ENABLED = True
#Set whether the plugin is on or off #Set whether the plugin is on or off
ts_settings = sublime.load_settings('trailing_spaces.sublime-settings') hws_settings = sublime.load_settings('highlight_whitespaces.sublime-settings')
trailing_spaces_enabled = bool(ts_settings.get('trailing_spaces_enabled', hws_enabled = bool(hws_settings.get('highlight_whitespaces_enabled',
DEFAULT_IS_ENABLED)) DEFAULT_IS_ENABLED))
# Determine if the view is a find results view # Determine if the view is a find results view
def is_find_results(view): def is_find_results(view):
return view.settings().get('syntax') and "Find Results" in view.settings().get('syntax') return view.settings().get('syntax') and "Find Results" in view.settings().get('syntax')
# Return an array of regions matching trailing spaces. # Return an array of regions matching whitespaces.
def find_trailing_spaces(view): def find_whitespaces_spaces(view):
include_empty_lines = bool(ts_settings.get('trailing_spaces_include_empty_lines', return view.find_all(' {2,}')
DEFAULT_IS_ENABLED))
return view.find_all('[ \t]+$' if include_empty_lines else '(?<=\S)[\t ]+$') def find_whitespaces_tabs(view):
return view.find_all('\t+')
# Highlight trailing spaces # Highlight whitespaces
def highlight_trailing_spaces(view): def highlight_whitespaces(view):
max_size = ts_settings.get('trailing_spaces_file_max_size', max_size = hws_settings.get('highlight_whitespaces_file_max_size',
DEFAULT_MAX_FILE_SIZE) DEFAULT_MAX_FILE_SIZE)
color_scope_name = ts_settings.get('trailing_spaces_highlight_color', space_scope_name = hws_settings.get('highlight_whitespaces_space_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
tab_scope_name = hws_settings.get('highlight_whitespaces_tab_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME) DEFAULT_COLOR_SCOPE_NAME)
if view.size() <= max_size and not is_find_results(view): if view.size() <= max_size and not is_find_results(view):
regions = find_trailing_spaces(view) space_regions = find_whitespaces_spaces(view)
view.add_regions('TrailingSpacesHighlightListener', view.add_regions('WhitespacesHighlightListener',
regions, color_scope_name, space_regions, space_scope_name,
sublime.DRAW_EMPTY)
tab_regions = find_whitespaces_tabs(view)
view.add_regions('WhitespacesHighlightListener2',
tab_regions, tab_scope_name,
sublime.DRAW_EMPTY) sublime.DRAW_EMPTY)
# Clear all trailing spaces # Clear all white spaces
def clear_trailing_spaces_highlight(window): def clear_whitespaces_highlight(window):
for view in window.views(): for view in window.views():
view.erase_regions('TrailingSpacesHighlightListener') view.erase_regions('WhitespacesHighlightListener')
view.erase_regions('WhitespacesHighlightListener2')
# Toggle the event listner on or off # Toggle the event listner on or off
class ToggleTrailingSpacesCommand(sublime_plugin.WindowCommand): class HwsToggleWhitespacesCommand(sublime_plugin.WindowCommand):
def run(self): def run(self):
global trailing_spaces_enabled global hws_enabled
trailing_spaces_enabled = False if trailing_spaces_enabled else True hws_enabled = False if hws_enabled else True
# If toggling on, go ahead and perform a pass, # If toggling on, go ahead and perform a pass,
# else clear the highlighting in all views # else clear the highlighting in all views
if trailing_spaces_enabled: if hws_enabled:
highlight_trailing_spaces(self.window.active_view()) highlight_whitespaces(self.window.active_view())
else: else:
clear_trailing_spaces_highlight(self.window) clear_whitespaces_highlight(self.window)
# Highlight matching regions. # Highlight matching regions.
class TrailingSpacesHighlightListener(sublime_plugin.EventListener): class WhitespacesHighlightListener(sublime_plugin.EventListener):
def on_modified(self, view): def on_modified(self, view):
if trailing_spaces_enabled: if hws_enabled:
highlight_trailing_spaces(view) highlight_whitespaces(view)
def on_activated(self, view): def on_activated(self, view):
if trailing_spaces_enabled: if hws_enabled:
highlight_trailing_spaces(view) highlight_whitespaces(view)
def on_load(self, view): def on_load(self, view):
if trailing_spaces_enabled: if hws_enabled:
highlight_trailing_spaces(view) highlight_whitespaces(view)
class WhitespacesHighlightListener2(sublime_plugin.EventListener):
def on_modified(self, view):
if hws_enabled:
highlight_whitespaces(view)
# Allows to erase matching regions. def on_activated(self, view):
class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand): if hws_enabled:
def run(self, edit): highlight_whitespaces(view)
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), def on_load(self, view):
"plural": 's' if len(regions) > 1 else ''} if hws_enabled:
msg = "Deleted %(nbRegions)s trailing spaces region%(plural)s" % msg_parts highlight_whitespaces(view)
else:
msg = "No trailing spaces to delete!"
sublime.status_message(msg)

View File

@ -1,13 +1,13 @@
{ {
// Color is determined by scope // Spaces color is determined by scope (default, "invalid")
"trailing_spaces_highlight_color" : "invalid", // "highlight_whitespaces_space_highlight_scope_name": "Whitespaces.space.highlight",
// Max file size to search // Tabs color is determined by scope (default, "invalid")
"trailing_spaces_file_max_size" : 1048576, // "highlight_whitespaces_tab_highlight_scope_name": "Whitespaces.tab.highlight",
// By default plugin is enabled or disabled (true|false) // Max file size to search
"trailing_spaces_enabled" : true, "highlight_whitespaces_file_max_size": 1048576,
// By default empty lines are cleared // By default plugin is enabled or disabled (true|false)
"trailing_spaces_include_empty_lines" : true "highlight_whitespaces_enabled": true
} }