diff --git a/qad/widgets/qcodeedit.cpp b/qad/widgets/qcodeedit.cpp index fb02934..973f499 100644 --- a/qad/widgets/qcodeedit.cpp +++ b/qad/widgets/qcodeedit.cpp @@ -22,6 +22,10 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) { es_line.format.setBackground(QColor(240, 245, 240)); es_line.format.setProperty(QTextFormat::FullWidthSelection, true); es_cursor.format.setBackground(QColor(220, 255, 200)); + es_bracket.format.setBackground(QColor(180, 238, 180)); + es_bracket.format.setForeground(Qt::red); + es_range.format.setBackground(QColor(230, 246, 255)); + es_range.format.setProperty(QTextFormat::FullWidthSelection, true); widget_help = new QFrame(); widget_help->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint); widget_help->setFocusPolicy(Qt::NoFocus); @@ -374,8 +378,63 @@ void QCodeEdit::timerEvent(QTimerEvent * ) { } +char antiBracket(char c) { + switch (c) { + case '(': return ')'; + case '[': return ']'; + case '{': return '}'; + case '<': return '>'; + case ')': return '('; + case ']': return '['; + case '}': return '{'; + case '>': return '<'; + } + return 0; +} + + +void QCodeEdit::highlightBrackets() { + es_brackets.clear(); + QTextCursor stc = textCode->textCursor(), tc; + QTextEdit::ExtraSelection es; + stc.setPosition(stc.position()); + QTextCursor::MoveOperation mop[2] = {QTextCursor::Left, QTextCursor::Right}; + QString mbr[2] = {")]}>", "([{<"}; + for (int d = 0; d < 2; ++d) { + tc = stc; + tc.movePosition(mop[d], QTextCursor::KeepAnchor); + if (!tc.selectedText().isEmpty()) { + char ch = tc.selectedText()[0].toLatin1(); + if (mbr[d].contains(ch)) { + es = es_bracket; + es.cursor = tc; + es_brackets << es; + QTextCursor ftc = tc; + int bcnt = 1; char fch = antiBracket(ch); + while (bcnt > 0) { + ftc.setPosition(ftc.position()); + if (!ftc.movePosition(mop[d], QTextCursor::KeepAnchor)) break; + //qDebug() << tc.selectedText(); + if (ftc.selectedText().isEmpty()) break; + if (ftc.selectedText()[0].toLatin1() == ch) ++bcnt; + if (ftc.selectedText()[0].toLatin1() == fch) --bcnt; + } + if (bcnt == 0) { + es.cursor = ftc; + es_brackets << es; + es.format = es_range.format; + es.cursor.setPosition(tc.position(), QTextCursor::KeepAnchor); + if (!es.cursor.selection().isEmpty()) + es_brackets << es; + } + } + } + } +} + + void QCodeEdit::applyExtraSelection() { - textCode->setExtraSelections(QList() << es_line << es_selected << es_custom << es_cursor); + textCode->setExtraSelections(QList() << es_line << es_selected << es_custom << es_brackets << es_cursor); } @@ -969,6 +1028,7 @@ void QCodeEdit::textEdit_cursorPositionChanged() { es_line.cursor = textCode->textCursor(); es_line.cursor.select(QTextCursor::LineUnderCursor); es_line.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); + highlightBrackets(); applyExtraSelection(); } diff --git a/qad/widgets/qcodeedit.h b/qad/widgets/qcodeedit.h index f477180..801ed99 100644 --- a/qad/widgets/qcodeedit.h +++ b/qad/widgets/qcodeedit.h @@ -77,8 +77,8 @@ private: QTreeWidget * completer; IconedLabel * lbl_help[2]; QFrame * widget_help; - QTextEdit::ExtraSelection es_line, es_cursor; - QList es_selected, es_custom; + QTextEdit::ExtraSelection es_line, es_cursor, es_bracket, es_range; + QList es_selected, es_custom, es_brackets; QMap ac_classes; QStringList cursor_scope; int prev_lc, auto_comp_pl, timer; @@ -87,6 +87,7 @@ private: bool eventFilter(QObject * o, QEvent * e); void showEvent(QShowEvent * ); void timerEvent(QTimerEvent * ); + void highlightBrackets(); void applyExtraSelection(); void nextCompletition(); void previousCompletition();