| 394 | |
| 395 | |
| 396 | void CodeEditor::cursorChange() |
| 397 | { |
| 398 | QList<QTextEdit::ExtraSelection> allExtraSels = extraSelections(); |
| 399 | |
| 400 | BracketData* bd = static_cast<BracketData*>(textCursor().block().userData()); |
| 401 | QList<QTextEdit::ExtraSelection> extraSelections; |
| 402 | |
| 403 | { |
| 404 | QTextEdit::ExtraSelection highlightLineSelection; |
| 405 | QColor lineColor = theme.lineHighlightColor.get(darkMode); |
| 406 | highlightLineSelection.format.setBackground(lineColor); |
| 407 | highlightLineSelection.format.setProperty(QTextFormat::FullWidthSelection, true); |
| 408 | highlightLineSelection.cursor = textCursor(); |
| 409 | highlightLineSelection.cursor.clearSelection(); |
| 410 | extraSelections.append(highlightLineSelection); |
| 411 | } |
| 412 | |
| 413 | auto ec = theme.errorColor.get(darkMode); |
| 414 | auto wc = theme.warningColor.get(darkMode); |
| 415 | foreach (QTextEdit::ExtraSelection sel, allExtraSels) { |
| 416 | if (sel.format.underlineColor() == ec || sel.format.underlineColor() == wc) { |
| 417 | extraSelections.append(sel); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (bd) { |
| 422 | QVector<Bracket>& brackets = bd->brackets; |
| 423 | int pos = textCursor().block().position(); |
| 424 | for (int i=0; i<brackets.size(); i++) { |
| 425 | int curPos = textCursor().position()-textCursor().block().position(); |
| 426 | Bracket& b = brackets[i]; |
| 427 | int parenPos0 = -1; |
| 428 | int parenPos1 = -1; |
| 429 | int errPos = -1; |
| 430 | if (b.pos == curPos-1 && (b.b == '(' || b.b == '{' || b.b == '[')) { |
| 431 | parenPos1 = matchLeft(textCursor().block(), b.b, i+1, 0); |
| 432 | if (parenPos1 != -1) { |
| 433 | parenPos0 = pos+b.pos; |
| 434 | } else { |
| 435 | errPos = pos+b.pos; |
| 436 | } |
| 437 | } else if (b.pos == curPos-1 && (b.b == ')' || b.b == '}' || b.b == ']')) { |
| 438 | parenPos0 = matchRight(textCursor().block(), b.b, i-1, 0); |
| 439 | if (parenPos0 != -1) { |
| 440 | parenPos1 = pos+b.pos; |
| 441 | } else { |
| 442 | errPos = pos+b.pos; |
| 443 | } |
| 444 | } |
| 445 | if (parenPos0 != -1 && parenPos1 != -1) { |
| 446 | QTextEdit::ExtraSelection sel; |
| 447 | QTextCharFormat format = sel.format; |
| 448 | format.setBackground(theme.bracketsMatchColor.get(darkMode)); |
| 449 | sel.format = format; |
| 450 | QTextCursor cursor = textCursor(); |
| 451 | cursor.setPosition(parenPos0); |
| 452 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); |
| 453 | sel.cursor = cursor; |