Skip to content

Commit

Permalink
fix: highlight current line and highlight bracket conflicts.
Browse files Browse the repository at this point in the history
Change-Id: Ic9efd5c9e7a42deac510890d8f24789bd3b7d8bd
  • Loading branch information
rekols committed Dec 4, 2018
1 parent bd13894 commit 5db54ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/dtextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ DTextEdit::DTextEdit(QPlainTextEdit *parent)
// Init widgets.
connect(this, &QPlainTextEdit::updateRequest, this, &DTextEdit::handleUpdateRequest);
connect(this, &QPlainTextEdit::textChanged, this, &DTextEdit::updateLineNumber, Qt::QueuedConnection);
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &DTextEdit::highlightCurrentLine);
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &DTextEdit::cursorPositionChanged);
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &DTextEdit::cursorPositionChanged, Qt::QueuedConnection);
connect(document(), &QTextDocument::modificationChanged, this, &DTextEdit::setModified);

// Init menu.
Expand Down Expand Up @@ -1401,6 +1400,7 @@ void DTextEdit::renderAllSelections()
selections.append(m_keywordSelections);
selections.append(m_cursorKeywordSelection);
selections.append(m_wordUnderCursorSelection);
selections.append(m_bracketsSelections);

setExtraSelections(selections);
}
Expand Down Expand Up @@ -1898,9 +1898,12 @@ bool DTextEdit::setCursorKeywordSeletoin(int position, bool findNext)

void DTextEdit::cursorPositionChanged()
{
updateHighlightLineSelection();
m_bracketsSelections.clear();
updateHighlightBrackets('(', ')');
updateHighlightBrackets('{', '}');
updateHighlightBrackets('[', ']');
renderAllSelections();
}

void DTextEdit::updateHighlightBrackets(const QChar &openChar, const QChar &closeChar)
Expand Down Expand Up @@ -1976,23 +1979,27 @@ void DTextEdit::updateHighlightBrackets(const QChar &openChar, const QChar &clos
bracketEndCursor = QTextCursor(doc);
bracketEndCursor.setPosition(position);
bracketEndCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
bracketEndCursor.setCharFormat(m_bracketMatchFormat);
break;
}
}

forward ? position++ : position--;
}
bracketBeginCursor.setCharFormat(m_bracketMatchFormat);

QTextEdit::ExtraSelection startExtra;
startExtra.cursor = bracketBeginCursor;
startExtra.format = m_bracketMatchFormat;

QTextEdit::ExtraSelection endExtra;
endExtra.cursor = bracketEndCursor;
endExtra.format = m_bracketMatchFormat;

m_bracketsSelections.clear();
m_bracketsSelections << startExtra << endExtra;
}

m_brackets[openChar] = bracketBeginCursor;
m_brackets[closeChar] = bracketEndCursor;

QTextEdit::ExtraSelection extra;
extra.format = m_bracketMatchFormat;
extra.cursor = cursor;
setExtraSelections(QList<QTextEdit::ExtraSelection>() << extra);
}

void DTextEdit::setThemeWithPath(const QString &path)
Expand Down
1 change: 1 addition & 0 deletions src/dtextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public slots:
QPropertyAnimation *m_scrollAnimation;

QList<QTextEdit::ExtraSelection> m_keywordSelections;
QList<QTextEdit::ExtraSelection> m_bracketsSelections;
QTextEdit::ExtraSelection m_currentLineSelection;
QTextEdit::ExtraSelection m_cursorKeywordSelection;
QTextEdit::ExtraSelection m_wordUnderCursorSelection;
Expand Down

0 comments on commit 5db54ad

Please sign in to comment.