From 339d8c9524059cfefcb37ac70b1549ef1ff8f4ff Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 16 Nov 2019 23:54:47 -0700 Subject: [PATCH] bpo-33046: An IDLE option to strip trailing whitespace on save --- Lib/idlelib/config-main.def | 1 + Lib/idlelib/configdialog.py | 22 +++++++++++++++++++ Lib/idlelib/iomenu.py | 5 +++++ .../2019-11-16-23-54-30.bpo-33046.VMwGaW.rst | 2 ++ 4 files changed, 30 insertions(+) create mode 100644 Misc/NEWS.d/next/IDLE/2019-11-16-23-54-30.bpo-33046.VMwGaW.rst diff --git a/Lib/idlelib/config-main.def b/Lib/idlelib/config-main.def index 28ae94161d5..f866bbc848e 100644 --- a/Lib/idlelib/config-main.def +++ b/Lib/idlelib/config-main.def @@ -67,6 +67,7 @@ font-size= 10 font-bold= 0 encoding= none line-numbers-default= 0 +strip-trailing-whitespace-on-save= 1 [PyShell] auto-squeeze-min-lines= 50 diff --git a/Lib/idlelib/configdialog.py b/Lib/idlelib/configdialog.py index aaf319bbe1b..640c2f7e88a 100644 --- a/Lib/idlelib/configdialog.py +++ b/Lib/idlelib/configdialog.py @@ -1848,6 +1848,9 @@ class GenPage(Frame): frame_context: Frame context_title: Label (*)context_int: Entry - context_lines + frame_strip_trailing_space_save: Frame + strip_trailing_space_save_title: Label + (*)strip_trailing_space_save_bool: Checkbutton - strip_trailing_space_save frame_shell: LabelFrame frame_auto_squeeze_min_lines: Frame auto_squeeze_min_lines_title: Label @@ -1891,6 +1894,9 @@ class GenPage(Frame): ('main', 'EditorWindow', 'line-numbers-default')) self.context_lines = tracers.add( StringVar(self), ('extensions', 'CodeContext', 'maxlines')) + self.strip_trailing_space_save = tracers.add( + BooleanVar(self), + ('main', 'EditorWindow', 'strip-trailing-whitespace-on-save')) # Create widgets: # Section frames. @@ -1987,6 +1993,14 @@ class GenPage(Frame): validatecommand=self.digits_only, validate='key', ) + frame_strip_trailing_space_save = Frame(frame_editor, borderwidth=0) + strip_trailing_space_save_title = Label( + frame_strip_trailing_space_save, + text='Strip Trailing Whitespace on Save') + self.strip_trailing_space_save_bool = Checkbutton( + frame_strip_trailing_space_save, + variable=self.strip_trailing_space_save, width=1) + # Frame_shell. frame_auto_squeeze_min_lines = Frame(frame_shell, borderwidth=0) auto_squeeze_min_lines_title = Label(frame_auto_squeeze_min_lines, @@ -2069,6 +2083,11 @@ class GenPage(Frame): frame_context.pack(side=TOP, padx=5, pady=0, fill=X) context_title.pack(side=LEFT, anchor=W, padx=5, pady=5) self.context_int.pack(side=TOP, padx=5, pady=5) + # frame_strip_trailing_space_save. + frame_strip_trailing_space_save.pack(side=TOP, padx=5, pady=0, fill=X) + strip_trailing_space_save_title.pack( + side=LEFT, anchor=W,padx=5, pady=5) + self.strip_trailing_space_save_bool.pack(side=LEFT, padx=5, pady=5) # frame_auto_squeeze_min_lines frame_auto_squeeze_min_lines.pack(side=TOP, padx=5, pady=0, fill=X) @@ -2113,6 +2132,9 @@ class GenPage(Frame): 'main', 'EditorWindow', 'line-numbers-default', type='bool')) self.context_lines.set(idleConf.GetOption( 'extensions', 'CodeContext', 'maxlines', type='int')) + self.strip_trailing_space_save.set( + idleConf.GetOption('main', 'EditorWindow', + 'strip-trailing-whitespace-on-save', type='bool')) # Set variables for shell windows. self.auto_squeeze_min_lines.set(idleConf.GetOption( diff --git a/Lib/idlelib/iomenu.py b/Lib/idlelib/iomenu.py index 4b2833b8ca5..aefd48e7dee 100644 --- a/Lib/idlelib/iomenu.py +++ b/Lib/idlelib/iomenu.py @@ -371,6 +371,11 @@ class IOBinding: return "break" def writefile(self, filename): + strip = idleConf.GetOption( + 'main', 'EditorWindow', + 'strip-trailing-whitespace-on-save', type='bool') + if strip: + self.editwin.Rstrip(self.editwin).do_rstrip() text = self.fixnewlines() chars = self.encode(text) try: diff --git a/Misc/NEWS.d/next/IDLE/2019-11-16-23-54-30.bpo-33046.VMwGaW.rst b/Misc/NEWS.d/next/IDLE/2019-11-16-23-54-30.bpo-33046.VMwGaW.rst new file mode 100644 index 00000000000..8e84ab4f102 --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2019-11-16-23-54-30.bpo-33046.VMwGaW.rst @@ -0,0 +1,2 @@ +Add an option to IDLE to automatically strip trailing whitespace when saving +a file. The option is enabled by default.