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