Home Download Buy Blog Forum Support

Plugin to highlighting trailing whitespace?

Re: Plugin to highlighting trailing whitespace?

Postby facelessuser on Tue Oct 18, 2011 4:25 pm

I went ahead and gave it the ability to be toggled on my system. When toggled on, it will go ahead an perform a pass on toggle else it will clear any view that has already been highlighted during the on state. This is for anyone who would like it to be toggled. By default it is on, so this change would leave things how they are by default. You can change the default state to be off.

@chikamichi: If you want to pull these changes, let me know and I will create a pull request; I just wanted to go ahead and get this functionality a little early so I didn't have to have it running all the time.

Code: Select all
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

import sublime, sublime_plugin


#Set whether the plugin is on or off
TrailingSpacesEnabled = DEFAULT_IS_ENABLED

# Return an array of regions matching trailing spaces.
def find_trailing_spaces(view):
    return view.find_all('[ \t]+$')

# Highlight trailing spaces
def highlight_trailing_spaces(view):
  max_size = view.settings().get('trailing_spaces_file_max_size',
  color_scope_name = view.settings().get('trailing_spaces_highlight_color',
  if view.size() <= max_size:
      regions = find_trailing_spaces(view)
                       regions, color_scope_name,

# Clear all trailing spaces
def clear_trailing_spaces_highlight(window):
  for view in window.views():

# 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

# Highlight matching regions.
class TrailingSpacesHighlightListener(sublime_plugin.EventListener):
  def on_modified(self, view):

  def on_activated(self,view):

  def on_load(self,view):

# 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
            msg = "No trailing spaces to delete!"


I didn't add it here, but also what would be nice, if when enabled, it did a pass when the view became active; that way I don't have to edit the file force it find the white space in a file.

EDIT: It was trivial, so I went ahead and added an on_load and on_activated event so you don't have to edit the file to see the highlights. I find being forced to make unneeded edits to see the trailing spaces undesirable.
Posts: 1780
Joined: Tue Apr 05, 2011 7:38 pm

Re: Plugin to highlighting trailing whitespace?

Postby chikamichi on Tue Oct 18, 2011 10:25 pm

Ah, nice! I didn't have the time tonight, so this is great. Pull request is the way to go, yes :)

Your edits are great. We may add a config key to set whether highlighting is enabled by default or not.
Posts: 14
Joined: Sun Oct 16, 2011 2:26 am

Re: Plugin to highlighting trailing whitespace?

Postby facelessuser on Tue Oct 18, 2011 11:07 pm

Pull request has been made.

I agree that whether it is initiated by default should be controlled via a setting. That is something I had not yet tackled.

I may have accidentally had it configured to be disabled by default. I would check that on the merge.

Edit: I set it to be enabled to True by default now.
Posts: 1780
Joined: Tue Apr 05, 2011 7:38 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests