Remove common leading whitespace when all lines are indented.
(text: str)
| 177 | |
| 178 | |
| 179 | def unindent_code(text: str) -> str: |
| 180 | """ |
| 181 | Remove common leading whitespace when all lines are indented. |
| 182 | """ |
| 183 | lines = text.splitlines(keepends=True) |
| 184 | |
| 185 | # Look for common prefix. |
| 186 | common_prefix = _common_whitespace_prefix(lines) |
| 187 | |
| 188 | # Remove indentation. |
| 189 | lines = [line[len(common_prefix) :] for line in lines] |
| 190 | |
| 191 | return "".join(lines) |
| 192 | |
| 193 | |
| 194 | def _common_whitespace_prefix(strings: Iterable[str]) -> str: |
no test coverage detected