class JSHighlighter : public MultiLineCommentHighlighter { Q_OBJECT
public:
};
JSHighlighter
::JSHighlighter(QTextDocument *parent
) : MultiLineCommentHighlighter
(parent
) {
keywordFormat.setForeground(Qt::black);
keywordFormat.
setFontWeight(QFont::Bold);
keywordPatterns << "\\bvar\\b" << "\\bArray\\b" << "\\bfunction\\b"
<< "\\breturn\\b" << "\\barguments\\b" << "\\bif\\b"
<< "\\belse\\b" << "\\bfor\\b" << "\\bswitch\\b"
<< "\\bcase\\b" << "\\bbreak\\b" << "\\bwhile\\b";
int i = 0;
foreach
(const QString &pattern, keywordPatterns
) { SetRule
(QString("00_KeyWord_%1").
arg(i
),pattern,keywordFormat
);
++i;
}
// Values
valueFormat.setForeground(Qt::blue);
SetRule("03_Values","\\btrue\\b|\\bfalse\\b|\\b[0-9]+\\b",valueFormat);
//functionFormat.setFontItalic(false);
functionFormat.setForeground(Qt::darkBlue);
SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
// Qt Classes
classFormat.
setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
SetRule("06_QtClasses","\\bQ[A-Z]+[A-Za-z]+\\b",classFormat);
// Quotation
quotationFormat.setForeground(Qt::blue);
SetRule("z1_Quotations","\"[^\"]*\"",quotationFormat);
// Single Line Comments
singleLineCommentFormat.setForeground(Qt::darkGreen);
SetRule("z2_SingleLineComments","//[^\n]*",singleLineCommentFormat);
}
class JSHighlighter : public MultiLineCommentHighlighter { Q_OBJECT
public:
JSHighlighter(QTextDocument *parent = 0);
};
JSHighlighter::JSHighlighter(QTextDocument *parent) : MultiLineCommentHighlighter(parent)
{
QTextCharFormat keywordFormat;
keywordFormat.setForeground(Qt::black);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bvar\\b" << "\\bArray\\b" << "\\bfunction\\b"
<< "\\breturn\\b" << "\\barguments\\b" << "\\bif\\b"
<< "\\belse\\b" << "\\bfor\\b" << "\\bswitch\\b"
<< "\\bcase\\b" << "\\bbreak\\b" << "\\bwhile\\b";
int i = 0;
foreach (const QString &pattern, keywordPatterns) {
SetRule(QString("00_KeyWord_%1").arg(i),pattern,keywordFormat);
++i;
}
// Values
QTextCharFormat valueFormat;
valueFormat.setForeground(Qt::blue);
SetRule("03_Values","\\btrue\\b|\\bfalse\\b|\\b[0-9]+\\b",valueFormat);
QTextCharFormat functionFormat;
//functionFormat.setFontItalic(false);
functionFormat.setForeground(Qt::darkBlue);
SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
// Qt Classes
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
SetRule("06_QtClasses","\\bQ[A-Z]+[A-Za-z]+\\b",classFormat);
// Quotation
QTextCharFormat quotationFormat;
quotationFormat.setForeground(Qt::blue);
SetRule("z1_Quotations","\"[^\"]*\"",quotationFormat);
// Single Line Comments
QTextCharFormat singleLineCommentFormat;
singleLineCommentFormat.setForeground(Qt::darkGreen);
SetRule("z2_SingleLineComments","//[^\n]*",singleLineCommentFormat);
}
To copy to clipboard, switch view to plain text mode
That's of course a bit ugly, but works for me.
Bookmarks