| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <QtGui> |
|---|
| 4 | #include "ThuneHighlight.h" |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #define COLOR_COMMENT Qt::blue |
|---|
| 8 | #define COLOR_STRING Qt::darkRed |
|---|
| 9 | #define COLOR_KEYWORD Qt::darkGreen |
|---|
| 10 | #define COLOR_OPCODE Qt::darkMagenta |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | ThuneHighlight::ThuneHighlight(QTextDocument *parent) |
|---|
| 14 | : QSyntaxHighlighter(parent) |
|---|
| 15 | { |
|---|
| 16 | QTextCharFormat keywordFormat; |
|---|
| 17 | keywordFormat.setForeground( COLOR_KEYWORD ); |
|---|
| 18 | keywordFormat.setFontWeight( QFont::Bold ); |
|---|
| 19 | QStringList keywordPatterns; |
|---|
| 20 | keywordPatterns << "\\bdup\\b" << "\\bdup2\\b" << "\\bdrop\\b" |
|---|
| 21 | << "\\bover\\b" << "\\bnip\\b" << "\\btuck\\b" |
|---|
| 22 | << "\\binc\\b" << "\\bdec\\b"; |
|---|
| 23 | |
|---|
| 24 | foreach( QString pattern, keywordPatterns ) |
|---|
| 25 | mappings[pattern] = keywordFormat; |
|---|
| 26 | |
|---|
| 27 | QTextCharFormat classFormat; |
|---|
| 28 | classFormat.setFontWeight( QFont::Bold ); |
|---|
| 29 | classFormat.setForeground( COLOR_OPCODE ); |
|---|
| 30 | mappings["\\bQ[A-Za-z]+\\b"] = classFormat; |
|---|
| 31 | |
|---|
| 32 | QTextCharFormat singleLineCommentFormat; |
|---|
| 33 | singleLineCommentFormat.setForeground( COLOR_COMMENT ); |
|---|
| 34 | mappings[";[^\n]*"] = singleLineCommentFormat; |
|---|
| 35 | |
|---|
| 36 | multiLineCommentFormat.setForeground( COLOR_COMMENT ); |
|---|
| 37 | |
|---|
| 38 | QTextCharFormat quotationFormat; |
|---|
| 39 | quotationFormat.setForeground( COLOR_STRING ); |
|---|
| 40 | mappings["\".*\""] = quotationFormat; |
|---|
| 41 | |
|---|
| 42 | QTextCharFormat functionFormat; |
|---|
| 43 | functionFormat.setFontItalic( true ); |
|---|
| 44 | functionFormat.setForeground(Qt::blue); |
|---|
| 45 | mappings["\\b[A-Za-z0-9_]+\\(.*\\)"] = functionFormat; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | void ThuneHighlight::highlightBlock(const QString &text) |
|---|
| 50 | { |
|---|
| 51 | foreach( QString pattern, mappings.keys() ) |
|---|
| 52 | { |
|---|
| 53 | QRegExp expression(pattern); |
|---|
| 54 | int index = text.indexOf(expression); |
|---|
| 55 | while (index >= 0) |
|---|
| 56 | { |
|---|
| 57 | int length = expression.matchedLength(); |
|---|
| 58 | setFormat(index, length, mappings[pattern]); |
|---|
| 59 | index = text.indexOf(expression, index + length); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | setCurrentBlockState(0); |
|---|
| 63 | |
|---|
| 64 | QRegExp startExpression( "/\\*"); |
|---|
| 65 | QRegExp endExpression("\\*/"); |
|---|
| 66 | |
|---|
| 67 | int startIndex = 0; |
|---|
| 68 | if (previousBlockState() != 1) |
|---|
| 69 | startIndex = text.indexOf(startExpression); |
|---|
| 70 | |
|---|
| 71 | while (startIndex >= 0) |
|---|
| 72 | { |
|---|
| 73 | int endIndex = text.indexOf(endExpression, startIndex); |
|---|
| 74 | int commentLength; |
|---|
| 75 | if (endIndex == -1) |
|---|
| 76 | { |
|---|
| 77 | setCurrentBlockState(1); |
|---|
| 78 | commentLength = text.length() - startIndex; |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | commentLength = endIndex - startIndex + endExpression.matchedLength(); |
|---|
| 83 | } |
|---|
| 84 | setFormat(startIndex, commentLength, multiLineCommentFormat); |
|---|
| 85 | startIndex = text.indexOf(startExpression, startIndex + commentLength); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|