MCPcopy Index your code
hub / github.com/python/cpython / reformat_paragraph

Function reformat_paragraph

Lib/idlelib/format.py:120–154  ·  view source on GitHub ↗

Return data reformatted to specified width (limit).

(data, limit)

Source from the content-addressed store, hash-verified

118
119# This should perhaps be replaced with textwrap.wrap
120def reformat_paragraph(data, limit):
121 """Return data reformatted to specified width (limit)."""
122 lines = data.split("\n")
123 i = 0
124 n = len(lines)
125 while i < n and is_all_white(lines[i]):
126 i = i+1
127 if i >= n:
128 return data
129 indent1 = get_indent(lines[i])
130 if i+1 < n and not is_all_white(lines[i+1]):
131 indent2 = get_indent(lines[i+1])
132 else:
133 indent2 = indent1
134 new = lines[:i]
135 partial = indent1
136 while i < n and not is_all_white(lines[i]):
137 # XXX Should take double space after period (etc.) into account
138 words = re.split(r"(\s+)", lines[i])
139 for j in range(0, len(words), 2):
140 word = words[j]
141 if not word:
142 continue # Can happen when line ends in whitespace
143 if len((partial + word).expandtabs()) > limit and \
144 partial != indent1:
145 new.append(partial.rstrip())
146 partial = indent2
147 partial = partial + word + " "
148 if j+1 < len(words) and words[j+1] != " ":
149 partial = partial + " "
150 i = i+1
151 new.append(partial.rstrip())
152 # XXX Should reformat remaining paragraphs as well
153 new.extend(lines[i:])
154 return "\n".join(new)
155
156def reformat_comment(data, limit, comment_header):
157 """Return data reformatted to specified width with comment header."""

Callers 2

reformat_commentFunction · 0.85

Calls 8

is_all_whiteFunction · 0.85
get_indentFunction · 0.85
splitMethod · 0.45
expandtabsMethod · 0.45
appendMethod · 0.45
rstripMethod · 0.45
extendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…