Commit 82762535 authored by Maksim Klimov's avatar Maksim Klimov Committed by David Schulz

TextEditor: Highlight and animate matching parenthesis for generic highlighter

Change-Id: I78db37a91f1770513fcb5192bf45623799e14e34
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
Reviewed-by: default avatarAndré Hartmann <aha_1980@gmx.de>
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent a0715512
......@@ -155,6 +155,16 @@ void Highlighter::setTabSettings(const TabSettings &ts)
m_tabSettings = &ts;
}
static bool isOpeningParenthesis(QChar c)
{
return c == QLatin1Char('{') || c == QLatin1Char('[') || c == QLatin1Char('(');
}
static bool isClosingParenthesis(QChar c)
{
return c == QLatin1Char('}') || c == QLatin1Char(']') || c == QLatin1Char(')');
}
void Highlighter::highlightBlock(const QString &text)
{
if (!m_defaultContext.isNull() && !m_isBroken) {
......@@ -184,6 +194,17 @@ void Highlighter::highlightBlock(const QString &text)
// In the case region depth has changed since the last time the state was set.
setCurrentBlockState(computeState(extractObservableState(currentBlockState())));
}
TextEditor::Parentheses parentheses;
for (int pos = 0; pos < length; ++pos) {
const QChar c = text.at(pos);
if (isOpeningParenthesis(c))
parentheses.push_back(Parenthesis(Parenthesis::Opened, c, pos));
else if (isClosingParenthesis(c))
parentheses.push_back(Parenthesis(Parenthesis::Closed, c, pos));
}
TextDocumentLayout::setParentheses(currentBlock(), parentheses);
} catch (const HighlighterException &) {
m_isBroken = true;
}
......
......@@ -6155,6 +6155,7 @@ void TextEditorWidget::setDisplaySettings(const DisplaySettings &ds)
setHighlightCurrentLine(ds.m_highlightCurrentLine);
setRevisionsVisible(ds.m_markTextChanges);
setCenterOnScroll(ds.m_centerCursorOnScroll);
setParenthesesMatchingEnabled(ds.m_highlightMatchingParentheses);
d->m_fileEncodingLabelAction->setVisible(ds.m_displayFileEncoding);
if (d->m_displaySettings.m_visualizeWhitespace != ds.m_visualizeWhitespace) {
......
......@@ -30,6 +30,8 @@
#include "textdocumentlayout.h"
namespace TextEditor {
TextBlockUserData *TextDocumentLayout::userData(const QTextBlock &block)
{
TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
......@@ -37,3 +39,7 @@ TextBlockUserData *TextDocumentLayout::userData(const QTextBlock &block)
const_cast<QTextBlock &>(block).setUserData((data = new TextBlockUserData));
return data;
}
void TextDocumentLayout::setParentheses(const QTextBlock &, const Parentheses &) {}
} // namespace TextEditor
......@@ -34,6 +34,21 @@
#include <QTextBlockUserData>
// Replaces the "real" textdocumentlayout.h file.
namespace TextEditor {
struct Parenthesis;
typedef QVector<Parenthesis> Parentheses;
struct Parenthesis
{
enum Type { Opened, Closed };
inline Parenthesis() : type(Opened), pos(-1) {}
inline Parenthesis(Type t, QChar c, int position)
: type(t), chr(c), pos(position) {}
Type type;
QChar chr;
int pos;
};
struct CodeFormatterData {};
......@@ -50,6 +65,11 @@ struct TextBlockUserData : QTextBlockUserData
CodeFormatterData *m_data;
};
namespace TextDocumentLayout { TextBlockUserData *userData(const QTextBlock &block); }
namespace TextDocumentLayout {
TextBlockUserData *userData(const QTextBlock &block);
void setParentheses(const QTextBlock &, const Parentheses &);
}
} // namespace TextEditor
#endif // TEXTDOCUMENTLAYOUT_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment