Guest User

Untitled

a guest
Jun 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #coding: utf8
  3. #################################### IMPORTS ###################################
  4.  
  5. # Standard Libs
  6. import bisect
  7.  
  8. # Sublime Libs
  9. import sublime
  10. import sublimeplugin
  11.  
  12. #################################### HELPERS ###################################
  13.  
  14. def strip_trailing(view, save_recent_indentation=True, ignore='string'):
  15. trailing_spaces = view.findAll('[ \t]+(?![^\n])')
  16. if not trailing_spaces: return
  17.  
  18. if save_recent_indentation:
  19. for sel in view.sel():
  20. if not sel.empty(): continue
  21. line = view.line(sel)
  22.  
  23. if view.substr(line).isspace() and sel.end() == line.end():
  24. pos = bisect.bisect(trailing_spaces, line) - 1
  25. trailing_sel = trailing_spaces[pos]
  26.  
  27. if line.contains(trailing_sel):
  28. del trailing_spaces[pos]
  29.  
  30. for sel in reversed(trailing_spaces):
  31. if ignore:
  32. pt_range = xrange(sel.begin(), sel.end())
  33.  
  34. if any(view.matchSelector(pt, ignore) for pt in pt_range):
  35. continue
  36.  
  37. view.erase(sel)
  38.  
  39. ################################### COMMANDS ###################################
  40.  
  41. class StripTrailing(sublimeplugin.TextCommand):
  42. def onPreSave(self, view):
  43. strip_trailing(view)
  44.  
  45. def run(self, view, args):
  46. strip_trailing(view, ignore=None, save_recent_indentation=False)
  47.  
  48. ################################################################################
Add Comment
Please, Sign In to add comment