puro_nervio puro_nervio - 3 months ago 10
Python Question

Trigger trailing white space on demand on Sublime text 3

Related with the third answer from this question, I would like to refactor this simple plugin so that it works with Sublime Text 3. Please, could you help me? I am new to python, and I don´t know anything about plugin development for sublime.

import sublime, sublime_plugin

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

class TrimTrailingWhiteSpace2Command(sublime_plugin.TextCommand):
def run(self, edit):
trim_trailing_white_space2(self.view)


I have searched and the problem is
begin_edit()
and
end_edit()
. I don´t want to install a plugin just for triggering trailing white space on demand. Thanks a lot, best regards.

Answer

Working on ST3.

import sublime, sublime_plugin

    class TrimTrailingWhiteSpace2Command(sublime_plugin.TextCommand):
        def run(self, edit):
            trailing_white_space = self.view.find_all("[\t ]+$")
            trailing_white_space.reverse()
            for r in trailing_white_space:
                self.view.erase(edit, r)

    class TrimTrailingWhiteSpace2(sublime_plugin.EventListener):
        def run(self, view):
            view.run_command("trim_trailing_white_space2")