#include <highlighter.h>
Classes | |
struct | HighlightingRule |
Public Member Functions | |
Highlighter (QTextDocument *parent=0) | |
Protected Member Functions | |
void | highlightBlock (const QString &text) |
Definition at line 10 of file highlighter.h.
Highlighter::Highlighter | ( | QTextDocument * | parent = 0 ) |
Definition at line 4 of file highlighter.cpp.
: QSyntaxHighlighter(parent) { HighlightingRule rule; keywordFormat.setForeground(Qt::blue); keywordFormat.setFontWeight(QFont::Bold); QStringList keywordPatterns; keywordPatterns << "\\bbigint\\b" << "\\bbinary\\b" << "\\bbit\\b" << "\\bblob\\b" << "\\bbool\\b" << "\\bboolean\\b" << "\\bchar\\b" << "\\bdate\\b" << "\\bdatetime\\b" << "\\bdecimal\\b" << "\\bdouble\\b" << "\\benum\\b" << "\\bfloat\\b" << "\\bint\\b" << "\\blongblob\\b" << "\\bfloat\\b" << "\\bint\\b" << "\\blongblob\\b" << "\\blongtext\\b" << "\\bmediumblob\\b" << "\\bmediumint\\b" << "\\bmediumtext\\b" << "\\bnumeric\\b" << "\\breal\\b" << "\\bset\\b" << "\\bsmallint\\b" << "\\btext\\b" << "\\btime\\b" << "\\btimestamp\\b" << "\\btinyblob\\b" << "\\btinyint\\b" << "\\btinytext\\b" << "\\bvarbinary\\b" << "\\bvarchar\\b" << "\\byear\\b"; //read mysql keywords QFile file(":/dat/datafiles/keywords.dat"); if (!file.open(QFile::ReadOnly)); while (!file.atEnd()) { QByteArray line = file.readLine(); if (!line.isEmpty()) keywordPatterns << tr("%1%2%3").arg("\\b", line.trimmed(), "\\b"); keywordPatterns << tr("%1%2%3").arg("\\b", line.trimmed().toUpper(), "\\b"); } // foreach (QString pattern, keywordPatterns) { rule.pattern = QRegExp(pattern); rule.format = keywordFormat; rule.format.setFontCapitalization(QFont::AllUppercase); highlightingRules.append(rule); } classFormat.setFontWeight(QFont::Bold); classFormat.setForeground(Qt::green); classFormat.setFontCapitalization(QFont::AllUppercase); rule.pattern = QRegExp("#[^\n]*"); rule.format = classFormat; highlightingRules.append(rule); classFormat.setFontWeight(QFont::Bold); classFormat.setForeground(Qt::darkBlue); rule.pattern = QRegExp("$[^\n]"); rule.format = classFormat; highlightingRules.append(rule); braceFormat.setBackground(Qt::lightGray); rule.pattern = QRegExp("{[A-Za-z]+"); rule.format = braceFormat; highlightingRules.append(rule); singleLineCommentFormat.setForeground(Qt::darkGreen); singleLineCommentFormat.setFontWeight(QFont::Bold); rule.pattern = QRegExp("//[^\n]*"); rule.format = singleLineCommentFormat; highlightingRules.append(rule); multiLineCommentFormat.setForeground(Qt::darkGreen); multiLineCommentFormat.setFontWeight(QFont::Bold); braceFormater.setForeground(Qt::magenta); quotationFormat.setForeground(Qt::red); rule.pattern = QRegExp("\".*\""); rule.format = quotationFormat; highlightingRules.append(rule); singleQuotationFormat.setForeground(Qt::red); rule.pattern = QRegExp("'.*'"); rule.format = singleQuotationFormat; highlightingRules.append(rule); singleQuotationFormat.setForeground(Qt::magenta); singleQuotationFormat.setFontWeight(QFont::Bold); singleQuotationFormat.setFontItalic(true); // rule.pattern = QRegExp("`.*`"); // rule.format = singleQuotationFormat; // highlightingRules.append(rule); functionFormat.setFontItalic(true); functionFormat.setForeground(Qt::blue); rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); rule.format = functionFormat; highlightingRules.append(rule); commentStartExpression = QRegExp("/\\*"); commentEndExpression = QRegExp("\\*/"); braceStart = QRegExp("{"); braceEnd = QRegExp("}"); }
void Highlighter::highlightBlock | ( | const QString & | text ) | [protected] |
Definition at line 96 of file highlighter.cpp.
{ foreach (HighlightingRule rule, highlightingRules) { QRegExp expression(rule.pattern); int index = text.indexOf(expression); while (index >= 0) { int length = expression.matchedLength(); setFormat(index, length, rule.format); index = text.indexOf(expression, index + length); } } setCurrentBlockState(0); int startIndex = 0; if (previousBlockState() != 1) { startIndex = text.indexOf(commentStartExpression); } while (startIndex >= 0) { int endIndex = text.indexOf(commentEndExpression, startIndex); int commentLength; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + commentEndExpression.matchedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); } }