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

Function reflow_lines

Parser/asdl_c.py:35–75  ·  view source on GitHub ↗

Reflow the line s indented depth tabs. Return a sequence of lines where no line extends beyond MAX_COL when properly indented. The first line is properly indented based exclusively on depth * TABSIZE. All following lines -- these are the reflowed lines generated by this function -

(s, depth)

Source from the content-addressed store, hash-verified

33 return "%s_ty" % name
34
35def reflow_lines(s, depth):
36 """Reflow the line s indented depth tabs.
37
38 Return a sequence of lines where no line extends beyond MAX_COL
39 when properly indented. The first line is properly indented based
40 exclusively on depth * TABSIZE. All following lines -- these are
41 the reflowed lines generated by this function -- start at the same
42 column as the first character beyond the opening { in the first
43 line.
44 """
45 size = MAX_COL - depth * TABSIZE
46 if len(s) < size:
47 return [s]
48
49 lines = []
50 cur = s
51 padding = ""
52 while len(cur) > size:
53 i = cur.rfind(' ', 0, size)
54 # XXX this should be fixed for real
55 if i == -1 and 'GeneratorExp' in cur:
56 i = size + 3
57 assert i != -1, "Impossible line %d to reflow: %r" % (size, s)
58 lines.append(padding + cur[:i])
59 if len(lines) == 1:
60 # find new size based on brace
61 j = cur.find('{', 0, i)
62 if j >= 0:
63 j += 2 # account for the brace and the space after it
64 size -= j
65 padding = " " * j
66 else:
67 j = cur.find('(', 0, i)
68 if j >= 0:
69 j += 1 # account for the paren (no space after it)
70 size -= j
71 padding = " " * j
72 cur = cur[i+1:]
73 else:
74 lines.append(padding + cur)
75 return lines
76
77def reflow_c_string(s, depth):
78 return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE))

Callers 1

emitMethod · 0.85

Calls 3

rfindMethod · 0.45
appendMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…