2011-10-17 21:15:03 +00:00
|
|
|
'''
|
2012-10-04 21:28:45 +00:00
|
|
|
Marks all tabs and two or more spaces in each line with separate colors
|
2011-10-17 21:15:03 +00:00
|
|
|
|
|
|
|
Config summary (see README.md for details):
|
|
|
|
|
|
|
|
# key binding
|
2012-10-04 21:28:45 +00:00
|
|
|
{ "keys": ["ctrl+alt+w"], "command": "hws_toggle_whitespaces" }
|
2011-10-17 21:15:03 +00:00
|
|
|
|
|
|
|
# file settings
|
|
|
|
{
|
2012-10-04 21:28:45 +00:00
|
|
|
"highlight_whitespaces_space_highlight_scope_name": "invalid",
|
|
|
|
"highlight_whitespaces_tab_highlight_scope_name": "invalid",
|
|
|
|
"highlight_whitespaces_file_max_size": 1048576,
|
2013-10-21 11:50:29 +00:00
|
|
|
"highlight_whitespaces_enabled": false,
|
2013-07-11 22:05:57 +00:00
|
|
|
"highlight_whitespaces_check_spaces": true,
|
2013-08-27 09:03:12 +00:00
|
|
|
"highlight_whitespaces_check_tabs": true,
|
|
|
|
"highlight_last_whitespace": true
|
2011-10-17 21:15:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 21:28:45 +00:00
|
|
|
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>
|
2011-10-17 21:15:03 +00:00
|
|
|
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
|
2012-10-04 21:28:45 +00:00
|
|
|
@since: 2012-10-05
|
2011-10-17 21:15:03 +00:00
|
|
|
'''
|
|
|
|
|
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"
|
2013-10-21 11:50:29 +00:00
|
|
|
DEFAULT_IS_ENABLED = False
|
2013-07-11 22:05:57 +00:00
|
|
|
DEFAULT_CHECK_SPACES = True
|
|
|
|
DEFAULT_CHECK_TABS = True
|
2013-08-21 01:05:50 +00:00
|
|
|
DEFAULT_LAST_WHITESPACE = False
|
2011-10-18 23:00:42 +00:00
|
|
|
|
|
|
|
#Set whether the plugin is on or off
|
2012-10-04 21:28:45 +00:00
|
|
|
hws_settings = sublime.load_settings('highlight_whitespaces.sublime-settings')
|
|
|
|
hws_enabled = bool(hws_settings.get('highlight_whitespaces_enabled',
|
2011-10-20 01:30:30 +00:00
|
|
|
DEFAULT_IS_ENABLED))
|
2011-10-17 21:15:03 +00:00
|
|
|
|
2013-10-21 11:50:29 +00:00
|
|
|
def get_settings():
|
|
|
|
s = sublime.load_settings('highlight_whitespaces.sublime-settings')
|
|
|
|
return s
|
|
|
|
|
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
|
|
|
|
2012-10-04 21:28:45 +00:00
|
|
|
# Return an array of regions matching whitespaces.
|
|
|
|
def find_whitespaces_spaces(view):
|
2013-10-21 11:50:29 +00:00
|
|
|
hws_settings = get_settings()
|
2013-08-21 01:05:50 +00:00
|
|
|
last_whitespace = bool(hws_settings.get('highlight_last_whitespace',DEFAULT_LAST_WHITESPACE))
|
2013-10-21 11:50:29 +00:00
|
|
|
regex = ' {1,}|\t | \t'
|
2013-08-21 01:05:50 +00:00
|
|
|
if last_whitespace:
|
2013-08-27 09:09:00 +00:00
|
|
|
regex += '| {1,}$'
|
2013-08-21 01:05:50 +00:00
|
|
|
|
|
|
|
return view.find_all(regex)
|
2012-10-04 21:28:45 +00:00
|
|
|
|
|
|
|
def find_whitespaces_tabs(view):
|
|
|
|
return view.find_all('\t+')
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2012-10-04 21:28:45 +00:00
|
|
|
# Highlight whitespaces
|
|
|
|
def highlight_whitespaces(view):
|
2013-10-21 11:50:29 +00:00
|
|
|
hws_settings = get_settings()
|
|
|
|
|
2012-10-04 21:28:45 +00:00
|
|
|
max_size = hws_settings.get('highlight_whitespaces_file_max_size',
|
2011-10-20 01:30:30 +00:00
|
|
|
DEFAULT_MAX_FILE_SIZE)
|
2012-10-04 21:28:45 +00:00
|
|
|
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',
|
2011-10-20 01:30:30 +00:00
|
|
|
DEFAULT_COLOR_SCOPE_NAME)
|
2012-06-18 10:01:47 +00:00
|
|
|
if view.size() <= max_size and not is_find_results(view):
|
2013-07-11 22:05:57 +00:00
|
|
|
if hws_settings.get('highlight_whitespaces_check_spaces', DEFAULT_CHECK_SPACES):
|
|
|
|
space_regions = find_whitespaces_spaces(view)
|
|
|
|
view.add_regions('WhitespacesHighlightListener',
|
|
|
|
space_regions, space_scope_name, '',
|
|
|
|
sublime.DRAW_EMPTY)
|
|
|
|
if hws_settings.get('highlight_whitespaces_check_tabs', DEFAULT_CHECK_TABS):
|
|
|
|
tab_regions = find_whitespaces_tabs(view)
|
|
|
|
view.add_regions('WhitespacesHighlightListener2',
|
|
|
|
tab_regions, tab_scope_name, '',
|
|
|
|
sublime.DRAW_EMPTY)
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2012-10-04 21:28:45 +00:00
|
|
|
# Clear all white spaces
|
|
|
|
def clear_whitespaces_highlight(window):
|
2011-10-18 23:00:42 +00:00
|
|
|
for view in window.views():
|
2012-10-04 21:28:45 +00:00
|
|
|
view.erase_regions('WhitespacesHighlightListener')
|
|
|
|
view.erase_regions('WhitespacesHighlightListener2')
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
|
2011-10-18 23:00:42 +00:00
|
|
|
# Toggle the event listner on or off
|
2012-10-04 21:28:45 +00:00
|
|
|
class HwsToggleWhitespacesCommand(sublime_plugin.WindowCommand):
|
2011-10-18 23:00:42 +00:00
|
|
|
def run(self):
|
2012-10-04 21:28:45 +00:00
|
|
|
global hws_enabled
|
|
|
|
hws_enabled = False if hws_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
|
2012-10-04 21:28:45 +00:00
|
|
|
if hws_enabled:
|
|
|
|
highlight_whitespaces(self.window.active_view())
|
2011-10-18 23:00:42 +00:00
|
|
|
else:
|
2012-10-04 21:28:45 +00:00
|
|
|
clear_whitespaces_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.
|
2012-10-04 21:28:45 +00:00
|
|
|
class WhitespacesHighlightListener(sublime_plugin.EventListener):
|
2011-10-17 21:15:03 +00:00
|
|
|
def on_modified(self, view):
|
2012-10-04 21:28:45 +00:00
|
|
|
if hws_enabled:
|
|
|
|
highlight_whitespaces(view)
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
def on_activated(self, view):
|
2012-10-04 21:28:45 +00:00
|
|
|
if hws_enabled:
|
|
|
|
highlight_whitespaces(view)
|
2011-10-18 23:00:42 +00:00
|
|
|
|
2011-10-19 00:14:17 +00:00
|
|
|
def on_load(self, view):
|
2012-10-04 21:28:45 +00:00
|
|
|
if hws_enabled:
|
|
|
|
highlight_whitespaces(view)
|
|
|
|
|
|
|
|
class WhitespacesHighlightListener2(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)
|
2011-10-17 21:15:03 +00:00
|
|
|
|