Advertisement
Guest User

QSyntaxHighlighter

a guest
Feb 22nd, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <QtGui>
  2.  
  3. #include "highlighter.h"
  4.  
  5. Highlighter::Highlighter(QTextDocument *parent, FileMode mode)
  6.     : QSyntaxHighlighter(parent)
  7. {
  8.     this->filemode = mode;
  9.     HighlightingRule rule;
  10.  
  11.     keywordFormat.setForeground(QColor(0x80, 0x80, 0x00));
  12.     QStringList keywordPatterns;
  13.     keywordPatterns << "\\bclass\\b" << "\\bconst\\b" << "\\bexplicit\\b"
  14.                     << "\\bfriend\\b" << "\\binline\\b" << "\\bnamespace\\b"
  15.                     << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
  16.                     << "\\bsignals\\b" << "\\bslots\\b" << "\\bstatic\\b"
  17.                     << "\\btypename\\b" << "\\bvolatile\\b" << "\\bnew\\b"
  18.                     << "\\bdelete\\b" << "\\btrue\\b" << "\\bfalse\\b"
  19.                     << "\\bdouble\\b" << "\\bchar\\b" << "\\bthis\\b"
  20.                     << "\\blong\\b" << "\\bshort\\b" << "\\bsigned\\b"
  21.                     << "\\bstruct\\b" << "\\bvoid\\b" << "\\bunion\\b"
  22.                     << "\\bvirtual\\b" << "\\btypedef\\b" << "\\bunsigned\\b"
  23.                     << "\\benum\\b" << "\\bint\\b" << "\\boperator\\b"
  24.                     << "\\btemplate\\b" << "\\bfunction\\b" << "\\busing\\b"
  25.                     << "\\bif\\b" << "\\belse\\b" << "\\belseif\\b"
  26.                     << "\\bswitch\\b" << "\\bcase\\b" << "\\bbreak\\b"
  27.                     << "\\bcontinue\\b" << "\\bfor\\b" << "\\bwhile\\b"
  28.                     << "\\bforever\\b" << "\\btry\\b" << "\\bcatch\\b"
  29.                     << "\\bfinally\\b" << "\\breturn\\n" << "\\btypeof\\b"
  30.                     << "\\binstanceof\\b" << "\\binclude\\b" << "\\brequire\\b";
  31.     foreach(const QString &pattern, keywordPatterns){
  32.         rule.pattern = QRegExp(pattern);
  33.         rule.format = keywordFormat;
  34.         highlightingRules.append(rule);
  35.     }
  36.  
  37.     numberFormat.setForeground(QColor(0x00, 0x00, 0x80));
  38.     rule.pattern = QRegExp("\\b(?:0x)?[0-9]+\\b");
  39.     rule.format = numberFormat;
  40.     highlightingRules.append(rule);
  41.  
  42.     classFormat.setForeground(Qt::darkMagenta);
  43.     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
  44.     rule.format = classFormat;
  45.     highlightingRules.append(rule);
  46.  
  47.     quotationFormat.setForeground(Qt::darkGreen);
  48.  
  49.     functionFormat.setFontItalic(false);
  50.     functionFormat.setForeground(Qt::black);
  51.     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
  52.     rule.format = functionFormat;
  53.     highlightingRules.append(rule);
  54.  
  55.     precompileFormat.setForeground(Qt::darkBlue);
  56.     rule.pattern = QRegExp("\\b#[a-z]+\\b");
  57.     rule.format = precompileFormat;
  58.     highlightingRules.append(rule);
  59.  
  60.     singleLineCommentFormat.setForeground(Qt::darkGreen);
  61.     rule.pattern = QRegExp("//[^\n]*");
  62.     rule.format = singleLineCommentFormat;
  63.     highlightingRules.append(rule);
  64.  
  65.     multiLineCommentFormat.setForeground(Qt::darkGreen);
  66.  
  67.     commentStartExpression = QRegExp("/\\*");
  68.     commentEndExpression = QRegExp("\\*/");
  69. }
  70.  
  71. void Highlighter::highlightBlock(const QString &text)
  72. {
  73.     foreach(const HighlightingRule &rule, highlightingRules){
  74.         QRegExp expression(rule.pattern);
  75.         int index = expression.indexIn(text);
  76.         int length = expression.matchedLength();
  77.  
  78.         while (index >= 0) {
  79.             setFormat(index, length, rule.format);
  80.             index = expression.indexIn(text, index + length);
  81.         }
  82.     }
  83.  
  84.     int quotationStart = 0, quotationEnd = 0;
  85.     QChar edge = '"';
  86.     bool inQuotation = false;
  87.     for(int i = 0; i < text.length(); i++){
  88.         if(inQuotation && text.at(i) == '\\'){
  89.             i++;
  90.             continue;
  91.         }
  92.  
  93.         if(!inQuotation){
  94.             if(text.at(i) == '"' || text.at(i) == '\'' || text.at(i) == '`'){
  95.                 quotationStart = i;
  96.                 inQuotation = true;
  97.                 edge = text.at(i);
  98.             }
  99.  
  100.         }else{
  101.             if(text.at(i) == edge){
  102.                 quotationEnd = i;
  103.                 inQuotation = false;
  104.                 setFormat(quotationStart, quotationEnd - quotationStart + 1, quotationFormat);
  105.             }
  106.         }
  107.     }
  108.  
  109.     setCurrentBlockState(0);
  110.     int startIndex = 0;
  111.     if(previousBlockState() != 1){
  112.         startIndex = commentStartExpression.indexIn(text);
  113.     }
  114.  
  115.     while(startIndex >= 0){
  116.         int endIndex = commentEndExpression.indexIn(text, startIndex);
  117.         int commentLength;
  118.         if(endIndex == -1){
  119.             setCurrentBlockState(1);
  120.             commentLength = text.length() - startIndex;
  121.         }else{
  122.             commentLength = endIndex - startIndex
  123.                             + commentEndExpression.matchedLength();
  124.         }
  125.         setFormat(startIndex, commentLength, multiLineCommentFormat);
  126.         startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
  127.     }
  128. }
  129.  
  130.  
  131. Highlighter::FileMode Highlighter::fileMode() const{
  132.     return filemode;
  133. }
  134.  
  135. void Highlighter::setFileMode(FileMode mode){
  136.     this->filemode = mode;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement