| 108 | } |
| 109 | |
| 110 | void Highlighter::highlightBlock(const QString &text) |
| 111 | { |
| 112 | QTextBlock block = currentBlock(); |
| 113 | |
| 114 | BracketData* bd = new BracketData; |
| 115 | if (currentBlockUserData()) { |
| 116 | bd->d = static_cast<BracketData*>(currentBlockUserData())->d; |
| 117 | } |
| 118 | if (block.previous().userData()) { |
| 119 | bd->highlightingState = static_cast<BracketData*>(block.previous().userData())->highlightingState; |
| 120 | } |
| 121 | |
| 122 | QRegularExpression noneRegExp("\"|%|/\\*"); |
| 123 | QRegularExpression stringRegExp("\"|\\\\\\("); |
| 124 | QRegularExpression commentRegexp("\\*/"); |
| 125 | QRegularExpression interpolateRegexp("[)\"%(]|/\\*"); |
| 126 | |
| 127 | QTextCharFormat stringFormat; |
| 128 | stringFormat.setForeground(stringColor); |
| 129 | stringFormat.setFontItalic(true); |
| 130 | QTextCharFormat interpolateFormat; |
| 131 | interpolateFormat.setFontItalic(true); |
| 132 | |
| 133 | // Stage 1: find strings (including interpolations) and comments |
| 134 | QVector<HighlightingState>& highlightingState = bd->highlightingState; |
| 135 | int curPosition = 0; |
| 136 | HighlightingState currentState; |
| 137 | while (curPosition >= 0) { |
| 138 | currentState = highlightingState.empty() ? HS_NONE : highlightingState.back(); |
| 139 | switch (currentState) { |
| 140 | case HS_NONE: |
| 141 | { |
| 142 | int nxt = noneRegExp.match(text, curPosition).capturedStart(); |
| 143 | if (nxt==-1) { |
| 144 | curPosition = -1; |
| 145 | } else { |
| 146 | if (text[nxt]=='"') { |
| 147 | highlightingState.push_back(HS_STRING); |
| 148 | curPosition = nxt+1; |
| 149 | } else if (text[nxt]=='%') { |
| 150 | setFormat(nxt, text.size()-nxt, commentFormat); |
| 151 | curPosition = -1; |
| 152 | } else { |
| 153 | // /* |
| 154 | highlightingState.push_back(HS_COMMENT); |
| 155 | curPosition = nxt+2; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | break; |
| 160 | case HS_STRING: |
| 161 | { |
| 162 | int nxt = stringRegExp.match(text, curPosition).capturedStart(); |
| 163 | int stringStartIdx = curPosition==0 ? 0 : curPosition-1; |
| 164 | if (nxt==-1) { |
| 165 | setFormat(stringStartIdx, text.size()-stringStartIdx, stringFormat); |
| 166 | highlightingState.clear(); // this is an error, reset to NONE state |
| 167 | curPosition = -1; |