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