Esempio n. 1
0
    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
Esempio n. 2
0
    def update(self, *args, **keys):

        c = self.c

        # This is called at idle-time, and there can be problems when closing the window.
        if g.app.killed or not c or not hasattr(c, 'frame'):
            return

        w = c.frame.body.bodyCtrl
        tab_width = c.frame.tab_width

        s = w.getAllText()
        index = w.getInsertPoint()
        row, col = g.convertPythonIndexToRowCol(s, index)

        if col > 0:
            s2 = s[index - col:index]
            s2 = g.toUnicode(s2)
            col = g.computeWidth(s2, c.tab_width)

        if row != self.lastRow or col != self.lastCol:
            s = "line %d, col %d " % (row, col)
            self.label.configure(text=s)
            self.lastRow, self.lastCol = row, col

        if 0:  # Done in idle handler.
            self.label.after(500, self.update)
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
 def undent(self, p):
     '''Remove maximal leading whitespace from the start of all lines.'''
     if self.is_rst:
         return p.b  # Never unindent rst code.
     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.
             result.append("%s%s.%s" %
                           (self.c.atFileCommands.underindentEscapeString,
                            g.computeWidth(ws, self.tab_width), s.lstrip()))
     return result
Esempio n. 6
0
 def undent(self, p):
     '''Remove maximal leading whitespace from the start of all lines.'''
     if self.is_rst:
         return p.b # Never unindent rst code.
     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.
             result.append("%s%s.%s" % (
                 self.c.atFileCommands.underindentEscapeString,
                 g.computeWidth(ws, self.tab_width),
                 s.lstrip()))
     return result
Esempio n. 7
0
    def undent_by(self, s, undent_val):
        '''
        Remove leading whitespace equivalent to undent_val from each line.

        Strict languages: prepend the underindent escape for underindented lines.
        '''
        if self.is_rst:
            return s # Never unindent rst code.
        result = []
        for line in g.splitlines(s):
            lws_s = self.get_str_lws(line)
            lws = g.computeWidth(lws_s, self.tab_width)
            # Add underindentEscapeString only for strict languages.
            if self.strict and not line.isspace() and lws < undent_val:
                # End the underindent count with a period to
                # protect against lines that start with a digit!
                result.append("%s%s.%s" % (
                    self.escape, undent_val-lws, line.lstrip()))
            else:
                s = g.removeLeadingWhitespace(line, undent_val, self.tab_width)
                result.append(s)
        return ''.join(result)
Esempio n. 8
0
    def undent_by(self, s, undent_val):
        """
        Remove leading whitespace equivalent to undent_val from each line.

        Strict languages: prepend the underindent escape for underindented lines.
        """
        if self.is_rst:
            return s  # Never unindent rst code.
        result = []
        for line in g.splitlines(s):
            lws_s = self.get_str_lws(line)
            lws = g.computeWidth(lws_s, self.tab_width)
            # Add underindentEscapeString only for strict languages.
            if self.strict and not line.isspace() and lws < undent_val:
                # End the underindent count with a period to
                # protect against lines that start with a digit!
                result.append("%s%s.%s" % (
                    self.escape, undent_val - lws, line.lstrip()))
            else:
                s = g.removeLeadingWhitespace(line, undent_val, self.tab_width)
                result.append(s)
        return ''.join(result)