Skip to content

Commit 82494aa

Browse files
csabellaterryjreedy
authored andcommitted
bpo-36390: IDLE: Combine region formatting methods. (pythonGH-12481)
Rename paragraph.py to format.py and add region formatting methods from editor.py. Add tests for the latter.
1 parent fb26504 commit 82494aa

File tree

7 files changed

+589
-327
lines changed

7 files changed

+589
-327
lines changed

Lib/idlelib/configdialog.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from idlelib.autocomplete import AutoComplete
3030
from idlelib.codecontext import CodeContext
3131
from idlelib.parenmatch import ParenMatch
32-
from idlelib.paragraph import FormatParagraph
32+
from idlelib.format import FormatParagraph
3333
from idlelib.squeezer import Squeezer
3434

3535
changes = ConfigChanges()

Lib/idlelib/editor.py

+10-111
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class EditorWindow(object):
5353
from idlelib.autoexpand import AutoExpand
5454
from idlelib.calltip import Calltip
5555
from idlelib.codecontext import CodeContext
56-
from idlelib.paragraph import FormatParagraph
56+
from idlelib.format import FormatParagraph, FormatRegion
5757
from idlelib.parenmatch import ParenMatch
5858
from idlelib.rstrip import Rstrip
5959
from idlelib.squeezer import Squeezer
@@ -172,13 +172,14 @@ def __init__(self, flist=None, filename=None, key=None, root=None):
172172
text.bind("<<smart-backspace>>",self.smart_backspace_event)
173173
text.bind("<<newline-and-indent>>",self.newline_and_indent_event)
174174
text.bind("<<smart-indent>>",self.smart_indent_event)
175-
text.bind("<<indent-region>>",self.indent_region_event)
176-
text.bind("<<dedent-region>>",self.dedent_region_event)
177-
text.bind("<<comment-region>>",self.comment_region_event)
178-
text.bind("<<uncomment-region>>",self.uncomment_region_event)
179-
text.bind("<<tabify-region>>",self.tabify_region_event)
180-
text.bind("<<untabify-region>>",self.untabify_region_event)
181-
text.bind("<<toggle-tabs>>",self.toggle_tabs_event)
175+
self.fregion = fregion = self.FormatRegion(self)
176+
text.bind("<<indent-region>>", fregion.indent_region_event)
177+
text.bind("<<dedent-region>>", fregion.dedent_region_event)
178+
text.bind("<<comment-region>>", fregion.comment_region_event)
179+
text.bind("<<uncomment-region>>", fregion.uncomment_region_event)
180+
text.bind("<<tabify-region>>", fregion.tabify_region_event)
181+
text.bind("<<untabify-region>>", fregion.untabify_region_event)
182+
text.bind("<<toggle-tabs>>", self.toggle_tabs_event)
182183
text.bind("<<change-indentwidth>>",self.change_indentwidth_event)
183184
text.bind("<Left>", self.move_at_edge_if_selection(0))
184185
text.bind("<Right>", self.move_at_edge_if_selection(1))
@@ -1290,7 +1291,7 @@ def smart_indent_event(self, event):
12901291
try:
12911292
if first and last:
12921293
if index2line(first) != index2line(last):
1293-
return self.indent_region_event(event)
1294+
return self.fregion.indent_region_event(event)
12941295
text.delete(first, last)
12951296
text.mark_set("insert", first)
12961297
prefix = text.get("insert linestart", "insert")
@@ -1423,72 +1424,6 @@ def inner(offset, _startindex=startindex,
14231424
return _icis(_startindex + "+%dc" % offset)
14241425
return inner
14251426

1426-
def indent_region_event(self, event):
1427-
head, tail, chars, lines = self.get_region()
1428-
for pos in range(len(lines)):
1429-
line = lines[pos]
1430-
if line:
1431-
raw, effective = get_line_indent(line, self.tabwidth)
1432-
effective = effective + self.indentwidth
1433-
lines[pos] = self._make_blanks(effective) + line[raw:]
1434-
self.set_region(head, tail, chars, lines)
1435-
return "break"
1436-
1437-
def dedent_region_event(self, event):
1438-
head, tail, chars, lines = self.get_region()
1439-
for pos in range(len(lines)):
1440-
line = lines[pos]
1441-
if line:
1442-
raw, effective = get_line_indent(line, self.tabwidth)
1443-
effective = max(effective - self.indentwidth, 0)
1444-
lines[pos] = self._make_blanks(effective) + line[raw:]
1445-
self.set_region(head, tail, chars, lines)
1446-
return "break"
1447-
1448-
def comment_region_event(self, event):
1449-
head, tail, chars, lines = self.get_region()
1450-
for pos in range(len(lines) - 1):
1451-
line = lines[pos]
1452-
lines[pos] = '##' + line
1453-
self.set_region(head, tail, chars, lines)
1454-
return "break"
1455-
1456-
def uncomment_region_event(self, event):
1457-
head, tail, chars, lines = self.get_region()
1458-
for pos in range(len(lines)):
1459-
line = lines[pos]
1460-
if not line:
1461-
continue
1462-
if line[:2] == '##':
1463-
line = line[2:]
1464-
elif line[:1] == '#':
1465-
line = line[1:]
1466-
lines[pos] = line
1467-
self.set_region(head, tail, chars, lines)
1468-
return "break"
1469-
1470-
def tabify_region_event(self, event):
1471-
head, tail, chars, lines = self.get_region()
1472-
tabwidth = self._asktabwidth()
1473-
if tabwidth is None: return
1474-
for pos in range(len(lines)):
1475-
line = lines[pos]
1476-
if line:
1477-
raw, effective = get_line_indent(line, tabwidth)
1478-
ntabs, nspaces = divmod(effective, tabwidth)
1479-
lines[pos] = '\t' * ntabs + ' ' * nspaces + line[raw:]
1480-
self.set_region(head, tail, chars, lines)
1481-
return "break"
1482-
1483-
def untabify_region_event(self, event):
1484-
head, tail, chars, lines = self.get_region()
1485-
tabwidth = self._asktabwidth()
1486-
if tabwidth is None: return
1487-
for pos in range(len(lines)):
1488-
lines[pos] = lines[pos].expandtabs(tabwidth)
1489-
self.set_region(head, tail, chars, lines)
1490-
return "break"
1491-
14921427
def toggle_tabs_event(self, event):
14931428
if self.askyesno(
14941429
"Toggle tabs",
@@ -1523,33 +1458,6 @@ def change_indentwidth_event(self, event):
15231458
self.indentwidth = new
15241459
return "break"
15251460

1526-
def get_region(self):
1527-
text = self.text
1528-
first, last = self.get_selection_indices()
1529-
if first and last:
1530-
head = text.index(first + " linestart")
1531-
tail = text.index(last + "-1c lineend +1c")
1532-
else:
1533-
head = text.index("insert linestart")
1534-
tail = text.index("insert lineend +1c")
1535-
chars = text.get(head, tail)
1536-
lines = chars.split("\n")
1537-
return head, tail, chars, lines
1538-
1539-
def set_region(self, head, tail, chars, lines):
1540-
text = self.text
1541-
newchars = "\n".join(lines)
1542-
if newchars == chars:
1543-
text.bell()
1544-
return
1545-
text.tag_remove("sel", "1.0", "end")
1546-
text.mark_set("insert", head)
1547-
text.undo_block_start()
1548-
text.delete(head, tail)
1549-
text.insert(head, newchars)
1550-
text.undo_block_stop()
1551-
text.tag_add("sel", head, "insert")
1552-
15531461
# Make string that displays as n leading blanks.
15541462

15551463
def _make_blanks(self, n):
@@ -1571,15 +1479,6 @@ def reindent_to(self, column):
15711479
text.insert("insert", self._make_blanks(column))
15721480
text.undo_block_stop()
15731481

1574-
def _asktabwidth(self):
1575-
return self.askinteger(
1576-
"Tab width",
1577-
"Columns per tab? (2-16)",
1578-
parent=self.text,
1579-
initialvalue=self.indentwidth,
1580-
minvalue=2,
1581-
maxvalue=16)
1582-
15831482
# Guess indentwidth from text content.
15841483
# Return guessed indentwidth. This should not be believed unless
15851484
# it's in a reasonable range (e.g., it will be 0 if no indented

0 commit comments

Comments
 (0)