Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: FormatParagraph.py
"""Extension to format a paragraph or selection to a max width.
[0] Fix | Delete
[1] Fix | Delete
Does basic, standard text formatting, and also understands Python
[2] Fix | Delete
comment blocks. Thus, for editing Python source code, this
[3] Fix | Delete
extension is really only suitable for reformatting these comment
[4] Fix | Delete
blocks or triple-quoted strings.
[5] Fix | Delete
[6] Fix | Delete
Known problems with comment reformatting:
[7] Fix | Delete
* If there is a selection marked, and the first line of the
[8] Fix | Delete
selection is not complete, the block will probably not be detected
[9] Fix | Delete
as comments, and will have the normal "text formatting" rules
[10] Fix | Delete
applied.
[11] Fix | Delete
* If a comment block has leading whitespace that mixes tabs and
[12] Fix | Delete
spaces, they will not be considered part of the same block.
[13] Fix | Delete
* Fancy comments, like this bulleted list, aren't handled :-)
[14] Fix | Delete
"""
[15] Fix | Delete
[16] Fix | Delete
import re
[17] Fix | Delete
from idlelib.configHandler import idleConf
[18] Fix | Delete
[19] Fix | Delete
class FormatParagraph:
[20] Fix | Delete
[21] Fix | Delete
menudefs = [
[22] Fix | Delete
('format', [ # /s/edit/format dscherer@cmu.edu
[23] Fix | Delete
('Format Paragraph', '<<format-paragraph>>'),
[24] Fix | Delete
])
[25] Fix | Delete
]
[26] Fix | Delete
[27] Fix | Delete
def __init__(self, editwin):
[28] Fix | Delete
self.editwin = editwin
[29] Fix | Delete
[30] Fix | Delete
def close(self):
[31] Fix | Delete
self.editwin = None
[32] Fix | Delete
[33] Fix | Delete
def format_paragraph_event(self, event, limit=None):
[34] Fix | Delete
"""Formats paragraph to a max width specified in idleConf.
[35] Fix | Delete
[36] Fix | Delete
If text is selected, format_paragraph_event will start breaking lines
[37] Fix | Delete
at the max width, starting from the beginning selection.
[38] Fix | Delete
[39] Fix | Delete
If no text is selected, format_paragraph_event uses the current
[40] Fix | Delete
cursor location to determine the paragraph (lines of text surrounded
[41] Fix | Delete
by blank lines) and formats it.
[42] Fix | Delete
[43] Fix | Delete
The length limit parameter is for testing with a known value.
[44] Fix | Delete
"""
[45] Fix | Delete
if limit is None:
[46] Fix | Delete
# The default length limit is that defined by pep8
[47] Fix | Delete
limit = idleConf.GetOption(
[48] Fix | Delete
'extensions', 'FormatParagraph', 'max-width',
[49] Fix | Delete
type='int', default=72)
[50] Fix | Delete
text = self.editwin.text
[51] Fix | Delete
first, last = self.editwin.get_selection_indices()
[52] Fix | Delete
if first and last:
[53] Fix | Delete
data = text.get(first, last)
[54] Fix | Delete
comment_header = get_comment_header(data)
[55] Fix | Delete
else:
[56] Fix | Delete
first, last, comment_header, data = \
[57] Fix | Delete
find_paragraph(text, text.index("insert"))
[58] Fix | Delete
if comment_header:
[59] Fix | Delete
newdata = reformat_comment(data, limit, comment_header)
[60] Fix | Delete
else:
[61] Fix | Delete
newdata = reformat_paragraph(data, limit)
[62] Fix | Delete
text.tag_remove("sel", "1.0", "end")
[63] Fix | Delete
[64] Fix | Delete
if newdata != data:
[65] Fix | Delete
text.mark_set("insert", first)
[66] Fix | Delete
text.undo_block_start()
[67] Fix | Delete
text.delete(first, last)
[68] Fix | Delete
text.insert(first, newdata)
[69] Fix | Delete
text.undo_block_stop()
[70] Fix | Delete
else:
[71] Fix | Delete
text.mark_set("insert", last)
[72] Fix | Delete
text.see("insert")
[73] Fix | Delete
return "break"
[74] Fix | Delete
[75] Fix | Delete
def find_paragraph(text, mark):
[76] Fix | Delete
"""Returns the start/stop indices enclosing the paragraph that mark is in.
[77] Fix | Delete
[78] Fix | Delete
Also returns the comment format string, if any, and paragraph of text
[79] Fix | Delete
between the start/stop indices.
[80] Fix | Delete
"""
[81] Fix | Delete
lineno, col = map(int, mark.split("."))
[82] Fix | Delete
line = text.get("%d.0" % lineno, "%d.end" % lineno)
[83] Fix | Delete
[84] Fix | Delete
# Look for start of next paragraph if the index passed in is a blank line
[85] Fix | Delete
while text.compare("%d.0" % lineno, "<", "end") and is_all_white(line):
[86] Fix | Delete
lineno = lineno + 1
[87] Fix | Delete
line = text.get("%d.0" % lineno, "%d.end" % lineno)
[88] Fix | Delete
first_lineno = lineno
[89] Fix | Delete
comment_header = get_comment_header(line)
[90] Fix | Delete
comment_header_len = len(comment_header)
[91] Fix | Delete
[92] Fix | Delete
# Once start line found, search for end of paragraph (a blank line)
[93] Fix | Delete
while get_comment_header(line)==comment_header and \
[94] Fix | Delete
not is_all_white(line[comment_header_len:]):
[95] Fix | Delete
lineno = lineno + 1
[96] Fix | Delete
line = text.get("%d.0" % lineno, "%d.end" % lineno)
[97] Fix | Delete
last = "%d.0" % lineno
[98] Fix | Delete
[99] Fix | Delete
# Search back to beginning of paragraph (first blank line before)
[100] Fix | Delete
lineno = first_lineno - 1
[101] Fix | Delete
line = text.get("%d.0" % lineno, "%d.end" % lineno)
[102] Fix | Delete
while lineno > 0 and \
[103] Fix | Delete
get_comment_header(line)==comment_header and \
[104] Fix | Delete
not is_all_white(line[comment_header_len:]):
[105] Fix | Delete
lineno = lineno - 1
[106] Fix | Delete
line = text.get("%d.0" % lineno, "%d.end" % lineno)
[107] Fix | Delete
first = "%d.0" % (lineno+1)
[108] Fix | Delete
[109] Fix | Delete
return first, last, comment_header, text.get(first, last)
[110] Fix | Delete
[111] Fix | Delete
# This should perhaps be replaced with textwrap.wrap
[112] Fix | Delete
def reformat_paragraph(data, limit):
[113] Fix | Delete
"""Return data reformatted to specified width (limit)."""
[114] Fix | Delete
lines = data.split("\n")
[115] Fix | Delete
i = 0
[116] Fix | Delete
n = len(lines)
[117] Fix | Delete
while i < n and is_all_white(lines[i]):
[118] Fix | Delete
i = i+1
[119] Fix | Delete
if i >= n:
[120] Fix | Delete
return data
[121] Fix | Delete
indent1 = get_indent(lines[i])
[122] Fix | Delete
if i+1 < n and not is_all_white(lines[i+1]):
[123] Fix | Delete
indent2 = get_indent(lines[i+1])
[124] Fix | Delete
else:
[125] Fix | Delete
indent2 = indent1
[126] Fix | Delete
new = lines[:i]
[127] Fix | Delete
partial = indent1
[128] Fix | Delete
while i < n and not is_all_white(lines[i]):
[129] Fix | Delete
# XXX Should take double space after period (etc.) into account
[130] Fix | Delete
words = re.split("(\s+)", lines[i])
[131] Fix | Delete
for j in range(0, len(words), 2):
[132] Fix | Delete
word = words[j]
[133] Fix | Delete
if not word:
[134] Fix | Delete
continue # Can happen when line ends in whitespace
[135] Fix | Delete
if len((partial + word).expandtabs()) > limit and \
[136] Fix | Delete
partial != indent1:
[137] Fix | Delete
new.append(partial.rstrip())
[138] Fix | Delete
partial = indent2
[139] Fix | Delete
partial = partial + word + " "
[140] Fix | Delete
if j+1 < len(words) and words[j+1] != " ":
[141] Fix | Delete
partial = partial + " "
[142] Fix | Delete
i = i+1
[143] Fix | Delete
new.append(partial.rstrip())
[144] Fix | Delete
# XXX Should reformat remaining paragraphs as well
[145] Fix | Delete
new.extend(lines[i:])
[146] Fix | Delete
return "\n".join(new)
[147] Fix | Delete
[148] Fix | Delete
def reformat_comment(data, limit, comment_header):
[149] Fix | Delete
"""Return data reformatted to specified width with comment header."""
[150] Fix | Delete
[151] Fix | Delete
# Remove header from the comment lines
[152] Fix | Delete
lc = len(comment_header)
[153] Fix | Delete
data = "\n".join(line[lc:] for line in data.split("\n"))
[154] Fix | Delete
# Reformat to maxformatwidth chars or a 20 char width,
[155] Fix | Delete
# whichever is greater.
[156] Fix | Delete
format_width = max(limit - len(comment_header), 20)
[157] Fix | Delete
newdata = reformat_paragraph(data, format_width)
[158] Fix | Delete
# re-split and re-insert the comment header.
[159] Fix | Delete
newdata = newdata.split("\n")
[160] Fix | Delete
# If the block ends in a \n, we dont want the comment prefix
[161] Fix | Delete
# inserted after it. (Im not sure it makes sense to reformat a
[162] Fix | Delete
# comment block that is not made of complete lines, but whatever!)
[163] Fix | Delete
# Can't think of a clean solution, so we hack away
[164] Fix | Delete
block_suffix = ""
[165] Fix | Delete
if not newdata[-1]:
[166] Fix | Delete
block_suffix = "\n"
[167] Fix | Delete
newdata = newdata[:-1]
[168] Fix | Delete
return '\n'.join(comment_header+line for line in newdata) + block_suffix
[169] Fix | Delete
[170] Fix | Delete
def is_all_white(line):
[171] Fix | Delete
"""Return True if line is empty or all whitespace."""
[172] Fix | Delete
[173] Fix | Delete
return re.match(r"^\s*$", line) is not None
[174] Fix | Delete
[175] Fix | Delete
def get_indent(line):
[176] Fix | Delete
"""Return the initial space or tab indent of line."""
[177] Fix | Delete
return re.match(r"^([ \t]*)", line).group()
[178] Fix | Delete
[179] Fix | Delete
def get_comment_header(line):
[180] Fix | Delete
"""Return string with leading whitespace and '#' from line or ''.
[181] Fix | Delete
[182] Fix | Delete
A null return indicates that the line is not a comment line. A non-
[183] Fix | Delete
null return, such as ' #', will be used to find the other lines of
[184] Fix | Delete
a comment block with the same indent.
[185] Fix | Delete
"""
[186] Fix | Delete
m = re.match(r"^([ \t]*#*)", line)
[187] Fix | Delete
if m is None: return ""
[188] Fix | Delete
return m.group(1)
[189] Fix | Delete
[190] Fix | Delete
if __name__ == "__main__":
[191] Fix | Delete
import unittest
[192] Fix | Delete
unittest.main('idlelib.idle_test.test_formatparagraph',
[193] Fix | Delete
verbosity=2, exit=False)
[194] Fix | Delete
[195] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function