MCPcopy Index your code
hub / github.com/bpython/bpython / indent_empty_lines

Function indent_empty_lines

bpython/curtsiesfrontend/preprocess.py:16–40  ·  view source on GitHub ↗

Indents blank lines that would otherwise cause early compilation Only really works if starting on a new line

(s: str, compiler: CommandCompiler)

Source from the content-addressed store, hash-verified

14
15
16def indent_empty_lines(s: str, compiler: CommandCompiler) -> str:
17 """Indents blank lines that would otherwise cause early compilation
18
19 Only really works if starting on a new line"""
20 initial_lines = s.split("\n")
21 ends_with_newline = False
22 if initial_lines and not initial_lines[-1]:
23 ends_with_newline = True
24 initial_lines.pop()
25 result_lines = []
26
27 prevs, lines, nexts = tee(initial_lines, 3)
28 prevs = chain(("",), prevs)
29 nexts = chain(islice(nexts, 1, None), ("",))
30
31 for p_line, line, n_line in zip(prevs, lines, nexts):
32 if len(line) == 0:
33 # "\s*" always matches
34 p_indent = indent_empty_lines_re.match(p_line).group() # type: ignore
35 n_indent = indent_empty_lines_re.match(n_line).group() # type: ignore
36 result_lines.append(min([p_indent, n_indent], key=len) + line)
37 else:
38 result_lines.append(line)
39
40 return "\n".join(result_lines) + ("\n" if ends_with_newline else "")
41
42
43def leading_tabs_to_spaces(s: str) -> str:

Callers 1

preprocessFunction · 0.85

Calls 2

matchMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected