Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #coding: utf8
- #################################### IMPORTS ###################################
- # Standard Libs
- import bisect
- # Sublime Libs
- import sublime
- import sublimeplugin
- #################################### HELPERS ###################################
- def strip_trailing(view, save_recent_indentation=True, ignore='string'):
- trailing_spaces = view.findAll('[ \t]+(?![^\n])')
- if not trailing_spaces: return
- if save_recent_indentation:
- for sel in view.sel():
- if not sel.empty(): continue
- line = view.line(sel)
- if view.substr(line).isspace() and sel.end() == line.end():
- pos = bisect.bisect(trailing_spaces, line) - 1
- trailing_sel = trailing_spaces[pos]
- if line.contains(trailing_sel):
- del trailing_spaces[pos]
- for sel in reversed(trailing_spaces):
- if ignore:
- pt_range = xrange(sel.begin(), sel.end())
- if any(view.matchSelector(pt, ignore) for pt in pt_range):
- continue
- view.erase(sel)
- ################################### COMMANDS ###################################
- class StripTrailing(sublimeplugin.TextCommand):
- def onPreSave(self, view):
- strip_trailing(view)
- def run(self, view, args):
- strip_trailing(view, ignore=None, save_recent_indentation=False)
- ################################################################################
Add Comment
Please, Sign In to add comment