Exemple #1
0
    def textUnderCursor(self):
        tc = self.textCursor()
        tc.select(QTextCursor.WordUnderCursor)

        selected_text = tc.selectedText()

        if selected_text == "(":
            cursor_pos = tc.position()
            if cursor_pos - 2 > 0:
                cursor_prev = QTextCursor(self.document())
                cursor_prev.setPosition(cursor_pos - 2)
                cursor_prev.select(QTextCursor.WordUnderCursor)
                selected_text = cursor_prev.selectedText()

        #print(selected_text)
        return selected_text
Exemple #2
0
 def _highlight(self, editor: QTextEdit, text: str, fmt: QTextCharFormat,
                fmt_chg: QTextCharFormat):
     cursor = QTextCursor(editor.textCursor())
     if text.endswith('\n'):
         text = text[:-1]
     elif text.endswith('\n\1'):
         text = text[:-2] + '\1'
     pos_list = self._get_pos_list(text)
     text = text.replace('\0+',
                         '').replace('\0-',
                                     '').replace('\0^',
                                                 '').replace('\1', '')
     editor.setPlainText(text)
     n = 0
     for tag, start, end in pos_list:
         if not end == start + 2:
             cursor.setPosition(start - n)
             cursor.setPosition(end - n - 2, QTextCursor.KeepAnchor)
             if tag == '^':
                 cursor.mergeCharFormat(fmt_chg)
             else:
                 cursor.mergeCharFormat(fmt)
         n += 3
Exemple #3
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)