Skip to content

Commit

Permalink
Merge pull request #15 from zyphlar/mixed
Browse files Browse the repository at this point in the history
Child of PR#14: Adding support for mixed-whitespace detection
  • Loading branch information
disq committed Jun 10, 2015
2 parents ff0e672 + bb0207c commit 448392c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ as "invalid", "comment"... in "File Settings - User":
```
{
"highlight_whitespaces_space_highlight_scope_name": "invalid",
"highlight_whitespaces_tab_highlight_scope_name": "invalid"
"highlight_whitespaces_tab_highlight_scope_name": "invalid",
"highlight_whitespaces_eol_highlight_scope_name": "invalid",
"highlight_whitespaces_mixed_highlight_scope_name": "invalid"
}
```

Expand Down
53 changes: 53 additions & 0 deletions highlight_whitespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
{
"highlight_whitespaces_space_highlight_scope_name": "invalid",
"highlight_whitespaces_tab_highlight_scope_name": "invalid",
"highlight_whitespaces_eol_highlight_scope_name": "invalid",
"highlight_whitespaces_mixed_highlight_scope_name": "invalid",
"highlight_whitespaces_file_max_size": 1048576,
"highlight_whitespaces_enabled": true,
"highlight_whitespaces_check_spaces": true,
"highlight_whitespaces_check_tabs": true,
"highlight_whitespaces_check_eol": true,
"highlight_whitespaces_check_mixed": false,
"highlight_whitespaces_single_space": false,
"highlight_last_whitespace": true
}
Expand All @@ -33,7 +37,9 @@
DEFAULT_IS_ENABLED = True
DEFAULT_CHECK_SPACES = True
DEFAULT_SINGLE_SPACE = False
DEFAULT_CHECK_EOL = True
DEFAULT_CHECK_TABS = True
DEFAULT_CHECK_MIXED = True
DEFAULT_LAST_WHITESPACE = False

#Set whether the plugin is on or off
Expand Down Expand Up @@ -66,6 +72,12 @@ def find_whitespaces_spaces(view):
def find_whitespaces_tabs(view):
return view.find_all('\t+')

def find_whitespaces_eol(view):
return view.find_all('[\t ]+$')

def find_whitespaces_mixed(view):
return view.find_all('(\t )|( \t)')


# Highlight whitespaces
def highlight_whitespaces(view):
Expand All @@ -77,6 +89,10 @@ def highlight_whitespaces(view):
DEFAULT_COLOR_SCOPE_NAME)
tab_scope_name = hws_settings.get('highlight_whitespaces_tab_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
eol_scope_name = hws_settings.get('highlight_whitespaces_eol_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
mixed_scope_name = hws_settings.get('highlight_whitespaces_mixed_highlight_scope_name',
DEFAULT_COLOR_SCOPE_NAME)
if view.size() <= max_size and not is_find_results(view):
if hws_settings.get('highlight_whitespaces_check_spaces', DEFAULT_CHECK_SPACES):
space_regions = find_whitespaces_spaces(view)
Expand All @@ -88,13 +104,25 @@ def highlight_whitespaces(view):
view.add_regions('WhitespacesHighlightListener2',
tab_regions, tab_scope_name, '',
sublime.DRAW_EMPTY)
if hws_settings.get('highlight_whitespaces_check_eol', DEFAULT_CHECK_EOL):
eol_regions = find_whitespaces_eol(view)
view.add_regions('WhitespacesHighlightListener3',
eol_regions, eol_scope_name, '',
sublime.DRAW_EMPTY)
if hws_settings.get('highlight_whitespaces_check_mixed', DEFAULT_CHECK_MIXED):
mixed_regions = find_whitespaces_mixed(view)
view.add_regions('WhitespacesHighlightListener4',
mixed_regions, mixed_scope_name, '',
sublime.DRAW_EMPTY)


# Clear all white spaces
def clear_whitespaces_highlight(window):
for view in window.views():
view.erase_regions('WhitespacesHighlightListener')
view.erase_regions('WhitespacesHighlightListener2')
view.erase_regions('WhitespacesHighlightListener3')
view.erase_regions('WhitespacesHighlightListener4')


# Toggle the event listner on or off
Expand Down Expand Up @@ -138,3 +166,28 @@ def on_load(self, view):
if hws_enabled:
highlight_whitespaces(view)

class WhitespacesHighlightListener3(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)

class WhitespacesHighlightListener4(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)
13 changes: 13 additions & 0 deletions highlight_whitespaces.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
// Tabs color is determined by scope (default, "invalid")
// "highlight_whitespaces_tab_highlight_scope_name": "Whitespaces.tab.highlight",

// End-of-line color is determined by scope (default, "invalid")
// "highlight_whitespaces_eol_highlight_scope_name": "Whitespaces.eol.highlight",

// End-of-line color is determined by scope (default, "invalid")
// "highlight_whitespaces_mixed_highlight_scope_name": "Whitespaces.eol.highlight",


// Max file size to search
"highlight_whitespaces_file_max_size": 1048576,

Expand All @@ -17,6 +24,12 @@
// Whether to check for tabs
"highlight_whitespaces_check_tabs": true,

// Whether to check for end-of-line whitespace
"highlight_whitespaces_check_eol": true,

// Whether to check for mixed tabs and spaces
"highlight_whitespaces_check_mixed": false,

// Allow the highlighting of the last whitespace, one or more time
"highlight_last_whitespace": true
}

0 comments on commit 448392c

Please sign in to comment.