Home Download Buy Blog Forum Support

Modified trim_trailing_white_space to trigger from command

Modified trim_trailing_white_space to trigger from command

Postby rchl on Sun Jan 29, 2012 7:50 pm

The script at Default/trim_trailing_white_space.py has method for on_pre_save event only. Here's modified version that has a method that can be used with command too (key shortcut):

Code: Select all
import sublime, sublime_plugin

def trim_trailing_whitespace(view):
    trailing_white_space = view.find_all("[\t ]+$")
    trailing_white_space.reverse()
    edit = view.begin_edit()
    for r in trailing_white_space:
        view.erase(edit, r)
    view.end_edit(edit)

class TrimTrailingWhiteSpaceCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        trim_trailing_whitespace(self.view)

class TrimTrailingWhiteSpace(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        if view.settings().get("trim_trailing_white_space_on_save") == True:
            trim_trailing_whitespace(self.view)

class EnsureNewlineAtEof(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        if view.settings().get("ensure_newline_at_eof_on_save") == True:
            if view.size() > 0 and view.substr(view.size() - 1) != '\n':
                edit = view.begin_edit()
                view.insert(edit, view.size(), "\n")
                view.end_edit(edit)


Would be nice to incorporate it into default package.
rchl
 
Posts: 148
Joined: Fri Mar 04, 2011 11:02 pm

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 4 guests