RstripExtension.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. 'Provides "Strip trailing whitespace" under the "Format" menu.'
  2. class RstripExtension:
  3. menudefs = [
  4. ('format', [None, ('Strip trailing whitespace', '<<do-rstrip>>'), ] ), ]
  5. def __init__(self, editwin):
  6. self.editwin = editwin
  7. self.editwin.text.bind("<<do-rstrip>>", self.do_rstrip)
  8. def do_rstrip(self, event=None):
  9. text = self.editwin.text
  10. undo = self.editwin.undo
  11. undo.undo_block_start()
  12. end_line = int(float(text.index('end')))
  13. for cur in range(1, end_line):
  14. txt = text.get('%i.0' % cur, '%i.end' % cur)
  15. raw = len(txt)
  16. cut = len(txt.rstrip())
  17. # Since text.delete() marks file as changed, even if not,
  18. # only call it when needed to actually delete something.
  19. if cut < raw:
  20. text.delete('%i.%i' % (cur, cut), '%i.end' % cur)
  21. undo.undo_block_stop()
  22. if __name__ == "__main__":
  23. import unittest
  24. unittest.main('idlelib.idle_test.test_rstrip', verbosity=2, exit=False)