git-svn-id: svn://db.shs.com.ru/libs@312 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2017-11-09 13:39:41 +00:00
parent bd91c2e0b7
commit b591bea3db
2 changed files with 64 additions and 3 deletions

View File

@@ -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<QTextEdit::ExtraSelection>() << es_line << es_selected << es_custom << es_cursor);
textCode->setExtraSelections(QList<QTextEdit::ExtraSelection>() << 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();
}