| 83 | } |
| 84 | |
| 85 | void JavaScriptHighlighter::highlightBlock(const QString &text) |
| 86 | { |
| 87 | // Search standard rules, then name rules, comments and quotes |
| 88 | for (const HighlightingRule &rule : m_stdRules) |
| 89 | { |
| 90 | auto matchItr = rule.pattern.globalMatch(text); |
| 91 | while (matchItr.hasNext()) |
| 92 | { |
| 93 | auto match = matchItr.next(); |
| 94 | setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | auto matchItr = m_nameRule.pattern.globalMatch(text); |
| 99 | while (matchItr.hasNext()) |
| 100 | { |
| 101 | auto match = matchItr.next(); |
| 102 | if (match.lastCapturedIndex() >= 2) |
| 103 | setFormat(match.capturedStart(2), match.capturedLength(2), m_nameRule.format); |
| 104 | if (match.lastCapturedIndex() > 4) |
| 105 | { |
| 106 | // Highlight function parameters, but do not highlight the delimiter ',' |
| 107 | int paramPos = match.capturedStart(5); |
| 108 | QString substr = text.mid(paramPos, match.capturedLength(5)); |
| 109 | QStringList params = substr.split(QChar(',')); |
| 110 | for(const QString ¶m : params) |
| 111 | { |
| 112 | setFormat(paramPos, param.size(), m_parameterFormat); |
| 113 | paramPos += param.size() + 1; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | std::vector< std::pair<int, int> > singleLineComments; |
| 119 | matchItr = m_singleLineCommentExpr.globalMatch(text); |
| 120 | while (matchItr.hasNext()) |
| 121 | { |
| 122 | auto match = matchItr.next(); |
| 123 | setFormat(match.capturedStart(), match.capturedLength(), m_commentFormat); |
| 124 | singleLineComments.push_back({match.capturedStart(), match.capturedStart() + match.capturedLength()}); |
| 125 | } |
| 126 | |
| 127 | // Get previous block state. If state was a comment or quote, try to find ending expression |
| 128 | int prevBlockState = previousBlockState(), startIdx = 0, endIdx = 0; |
| 129 | if (prevBlockState == JavaScriptHighlighter::Comment) |
| 130 | { |
| 131 | endIdx = text.indexOf(m_commentEndExpr); |
| 132 | if (endIdx < 0) |
| 133 | { |
| 134 | setFormat(0, text.size(), m_commentFormat); |
| 135 | setCurrentBlockState(JavaScriptHighlighter::Comment); |
| 136 | return; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | setFormat(0, endIdx + 2, m_commentFormat); |
| 141 | setCurrentBlockState(JavaScriptHighlighter::None); |
| 142 | startIdx = endIdx + 3; |
nothing calls this directly
no outgoing calls
no test coverage detected