Exemplo n.º 1
0
    def replace_patterns(self, code):

        # clear format
        fmt = QTextCharFormat()
        fmt.setBackground(QColor('#2b2b2b'))

        cursor = QTextCursor(self.document())
        cursor.setPosition(0, QTextCursor.MoveAnchor)
        cursor.setPosition(len(code) - 1, QTextCursor.KeepAnchor)
        self.just_changed_text = True
        cursor.setCharFormat(fmt)

        # static elements
        color = QColor(255, 255, 0, 50)

        for pattern in self.static_elements:
            positions = [
                i for i in range(len(code)) if code.startswith(pattern, i)
            ]
            for occ in reversed(positions):
                fmt.setBackground(color)
                cursor.setPosition(occ, QTextCursor.MoveAnchor)
                cursor.setPosition(occ + len(pattern), QTextCursor.KeepAnchor)
                self.just_changed_text = True
                cursor.setCharFormat(fmt)

        # usable components
        color = QColor(100, 100, 255, 200)

        for pattern in self.components:
            positions = [
                i for i in range(len(code)) if code.startswith(pattern, i)
            ]
            for occ in reversed(positions):
                fmt.setBackground(color)
                cursor.setPosition(occ, QTextCursor.MoveAnchor)
                cursor.setPosition(occ + len(pattern), QTextCursor.KeepAnchor)
                self.just_changed_text = True
                cursor.setCharFormat(fmt)
Exemplo n.º 2
0
 def unhighlight(self):
     cursor = QTextCursor(self.document())
     cursor.movePosition(QTextCursor.Start)
     cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
     cursor.setCharFormat(QTextCharFormat())