From cc82b47a7747f7d18f124ee2cbca7e327a0a5167 Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Wed, 7 Mar 2012 18:20:45 +0100 Subject: [PATCH] add support for table names highlighting --- src/sqlitesyntaxhighlighter.cpp | 24 ++++++++++++++++++++++-- src/sqlitesyntaxhighlighter.h | 7 ++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/sqlitesyntaxhighlighter.cpp b/src/sqlitesyntaxhighlighter.cpp index 25a28afd..6c4bd5c8 100644 --- a/src/sqlitesyntaxhighlighter.cpp +++ b/src/sqlitesyntaxhighlighter.cpp @@ -5,6 +5,9 @@ SQLiteSyntaxHighlighter::SQLiteSyntaxHighlighter(QTextDocument *parent) : { HighlightingRule rule; + tableFormat.setForeground(Qt::darkCyan); + tableFormat.setFontWeight(QFont::Bold); + keywordFormat.setForeground(Qt::darkBlue); keywordFormat.setFontWeight(QFont::Bold); QStringList keywordPatterns; @@ -68,9 +71,20 @@ SQLiteSyntaxHighlighter::SQLiteSyntaxHighlighter(QTextDocument *parent) : highlightingRules.append(rule); } -void SQLiteSyntaxHighlighter::highlightBlock(const QString &text) +void SQLiteSyntaxHighlighter::setTableNames(const QStringList &tablenames) { - foreach (const HighlightingRule &rule, highlightingRules) { + tableNameRules.clear(); + foreach(const QString& tblname, tablenames) { + HighlightingRule rule; + rule.pattern = QRegExp(tblname); + rule.format = tableFormat; + tableNameRules.append(rule); + } +} + +void SQLiteSyntaxHighlighter::highlightBlockVector(const QString& text, const QVector& vec) +{ + foreach (const HighlightingRule &rule, vec) { QRegExp expression(rule.pattern); int index = expression.indexIn(text); while (index >= 0) { @@ -80,3 +94,9 @@ void SQLiteSyntaxHighlighter::highlightBlock(const QString &text) } } } + +void SQLiteSyntaxHighlighter::highlightBlock(const QString &text) +{ + highlightBlockVector(text, tableNameRules); + highlightBlockVector(text, highlightingRules); +} diff --git a/src/sqlitesyntaxhighlighter.h b/src/sqlitesyntaxhighlighter.h index 61857600..0bced95d 100644 --- a/src/sqlitesyntaxhighlighter.h +++ b/src/sqlitesyntaxhighlighter.h @@ -9,6 +9,8 @@ class SQLiteSyntaxHighlighter : public QSyntaxHighlighter public: explicit SQLiteSyntaxHighlighter(QTextDocument *parent = 0); + void setTableNames(const QStringList& tablenames); + protected: void highlightBlock(const QString &text); @@ -18,7 +20,10 @@ private: QRegExp pattern; QTextCharFormat format; }; + void highlightBlockVector(const QString& text, const QVector& vec); + QVector highlightingRules; + QVector tableNameRules; QRegExp commentStartExpression; QRegExp commentEndExpression; @@ -27,7 +32,7 @@ private: QTextCharFormat singleLineCommentFormat; QTextCharFormat multiLineCommentFormat; QTextCharFormat quotationFormat; - + QTextCharFormat tableFormat; }; #endif // SQLITESYNTAXHIGHLIGHTER_H