Turns text like this: ''' def foo(): return "bar" def baz(): return "bat" def bam(): return "bat" ''' into this: '''def foo(): return "bar" def baz(): return "bat" def bam(): return "ba
(text: str)
| 45 | |
| 46 | |
| 47 | def squish_text(text: str) -> str: |
| 48 | """ |
| 49 | Turns text like this: |
| 50 | |
| 51 | ''' def foo(): |
| 52 | return "bar" |
| 53 | def baz(): |
| 54 | return "bat" |
| 55 | def bam(): |
| 56 | return "bat" |
| 57 | ''' |
| 58 | |
| 59 | into this: |
| 60 | |
| 61 | '''def foo(): |
| 62 | return "bar" |
| 63 | def baz(): |
| 64 | return "bat" |
| 65 | def bam(): |
| 66 | return "bat" |
| 67 | ''' |
| 68 | |
| 69 | The former is common when we are trying to use string templates |
| 70 | whose parameters are multiline and unaware of the existing indentation. |
| 71 | |
| 72 | :param text: a string with messed up indentation |
| 73 | :return: `text` but with indentation fixed |
| 74 | """ |
| 75 | prev_indentation = 0 |
| 76 | transformed_text_lines = [] |
| 77 | for line in text.strip("\n").splitlines(): |
| 78 | line_without_indentation = line.lstrip() |
| 79 | indentation = len(line) - len(line_without_indentation) |
| 80 | if indentation == 0: |
| 81 | indentation = prev_indentation |
| 82 | else: |
| 83 | prev_indentation = indentation |
| 84 | transformed_text_lines.append( |
| 85 | textwrap.indent(line_without_indentation, " " * indentation) |
| 86 | ) |
| 87 | return textwrap.dedent("\n".join(transformed_text_lines)) |
| 88 | |
| 89 | |
| 90 | class AutoreloadDetectionSuite(unittest.TestCase): |
no test coverage detected
searching dependent graphs…