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