Indents blank lines that would otherwise cause early compilation Only really works if starting on a new line
(s: str, compiler: CommandCompiler)
| 14 | |
| 15 | |
| 16 | def 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 | |
| 43 | def leading_tabs_to_spaces(s: str) -> str: |
no test coverage detected