summaryrefslogtreecommitdiff
path: root/Lib/idlelib/format.py
diff options
context:
space:
mode:
authorTerry Jan Reedy <tjreedy@udel.edu>2019-07-17 20:48:36 -0400
committerGitHub <noreply@github.com>2019-07-17 20:48:36 -0400
commit1b3892243433da7eae7f5f3a4f98f13d309c8926 (patch)
tree6d05c4527da31afb91c11e551a949666012f26fd /Lib/idlelib/format.py
parent8cb65d1381b027f0b09ee36bfed7f35bb4dec9a9 (diff)
downloadcpython-git-1b3892243433da7eae7f5f3a4f98f13d309c8926.tar.gz
bpo-36390: Gather IDLE Format menu functions into format.py (#14827)
Add two indent spec methods from editor and Rstrip to existing file. Tests are not added for indent methods because they need change in lights of 3.x's prohibition on mixing tabs and spaces.
Diffstat (limited to 'Lib/idlelib/format.py')
-rw-r--r--Lib/idlelib/format.py64
1 files changed, 62 insertions, 2 deletions
diff --git a/Lib/idlelib/format.py b/Lib/idlelib/format.py
index e11ca3a9d2..bced4c1770 100644
--- a/Lib/idlelib/format.py
+++ b/Lib/idlelib/format.py
@@ -6,6 +6,7 @@ comment, uncomment, tabify, and untabify.
File renamed from paragraph.py with functions added from editor.py.
"""
import re
+from tkinter.messagebox import askyesno
from tkinter.simpledialog import askinteger
from idlelib.config import idleConf
@@ -195,7 +196,7 @@ def get_comment_header(line):
return m.group(1)
-# Copy from editor.py; importing it would cause an import cycle.
+# Copied from editor.py; importing it would cause an import cycle.
_line_indent_re = re.compile(r'[ \t]*')
def get_line_indent(line, tabwidth):
@@ -209,7 +210,7 @@ def get_line_indent(line, tabwidth):
class FormatRegion:
- "Format selected text."
+ "Format selected text (region)."
def __init__(self, editwin):
self.editwin = editwin
@@ -352,6 +353,65 @@ class FormatRegion:
maxvalue=16)
+# With mixed indents not allowed, these are semi-useless and not unittested.
+class Indents: # pragma: no cover
+ "Change future indents."
+
+ def __init__(self, editwin):
+ self.editwin = editwin
+
+ def toggle_tabs_event(self, event):
+ editwin = self.editwin
+ usetabs = editwin.usetabs
+ if askyesno(
+ "Toggle tabs",
+ "Turn tabs " + ("on", "off")[usetabs] +
+ "?\nIndent width " +
+ ("will be", "remains at")[usetabs] + " 8." +
+ "\n Note: a tab is always 8 columns",
+ parent=editwin.text):
+ editwin.usetabs = not usetabs
+ # Try to prevent inconsistent indentation.
+ # User must change indent width manually after using tabs.
+ editwin.indentwidth = 8
+ return "break"
+
+ def change_indentwidth_event(self, event):
+ editwin = self.editwin
+ new = askinteger(
+ "Indent width",
+ "New indent width (2-16)\n(Always use 8 when using tabs)",
+ parent=editwin.text,
+ initialvalue=editwin.indentwidth,
+ minvalue=2,
+ maxvalue=16)
+ if new and new != editwin.indentwidth and not editwin.usetabs:
+ editwin.indentwidth = new
+ return "break"
+
+
+class Rstrip: # 'Strip Trailing Whitespace" on "Format" menu.
+ def __init__(self, editwin):
+ self.editwin = editwin
+
+ def do_rstrip(self, event=None):
+ text = self.editwin.text
+ undo = self.editwin.undo
+ undo.undo_block_start()
+
+ end_line = int(float(text.index('end')))
+ for cur in range(1, end_line):
+ txt = text.get('%i.0' % cur, '%i.end' % cur)
+ raw = len(txt)
+ cut = len(txt.rstrip())
+ # Since text.delete() marks file as changed, even if not,
+ # only call it when needed to actually delete something.
+ if cut < raw:
+ text.delete('%i.%i' % (cur, cut), '%i.end' % cur)
+
+ undo.undo_block_stop()
+
+
if __name__ == "__main__":
from unittest import main
main('idlelib.idle_test.test_format', verbosity=2, exit=False)