def undent(self, p): """ Remove the *maximum* whitespace of any line from the start of *all* lines, appending the underindent escape sequence for all underindented lines. This is *not* the same as textwrap.dedent! """ # Called from i.post_pass, i.unindent_all_nodes. c = self.c if self.is_rst: return p.b # Never unindent rst code. escape = c.atFileCommands.underindentEscapeString lines = self.get_lines(p) ws = self.common_lws(lines) result = [] for s in lines: if s.startswith(ws): result.append(s[len(ws) :]) elif s.isspace(): # Never change blank lines. result.append(s) else: # Indicate that the line is underindented. lws = g.get_leading_ws(s) # Bug fix 2021/11/15: Use n1 - n2, not n1! n1 = g.computeWidth(ws, self.tab_width) n2 = g.computeWidth(lws, self.tab_width) assert n1 > n2, (n1, n2) result.append(f"{escape}{n1-n2}.{s.lstrip()}") return result
def rp_get_leading_ws(c, lines, tabWidth): '''Compute and return indents and leading_ws.''' # c = self indents = [0, 0] leading_ws = ["", ""] for i in (0, 1): if i < len(lines): # Use the original, non-optimized leading whitespace. leading_ws[i] = ws = g.get_leading_ws(lines[i]) indents[i] = g.computeWidth(ws, tabWidth) indents[1] = max(indents) if len(lines) == 1: leading_ws[1] = leading_ws[0] return indents, leading_ws