int LuaHighlighter::highlightLine(QString const& text, int initialState) { Scanner scanner(text.constData(),text.length()); scanner.setState(initialState); FormatToken tk; bool hasOnlyWhitespace = true; while((tk = scanner.read()).format() != Format_EndOfBlock) { Format format = tk.format(); if(format == Format_Keyword) { if(isImportKeyword(scanner.value(tk)) && hasOnlyWhitespace) { setFormat(tk.begin(), tk.length(), formatForCategory(format)); highlightImport(scanner); break; } } setFormat(tk.begin(), tk.length(), formatForCategory(format)); if(format != Format_Whitespace) hasOnlyWhitespace = false; } return scanner.state(); }
int GoHighlighter::highlightLine(const QString &text, int initialState) { GoScanner scanner(text.constData(), text.size()); scanner.setState(initialState); FormatToken tk; while ((tk = scanner.read()).format() != Format_EndOfBlock) setFormat(tk.begin(), tk.length(), formatForCategory(tk.format())); return scanner.state(); }
/** * @brief Highlights rest of line as import directive */ void PythonHighlighter::highlightImport(Scanner &scanner) { FormatToken tk; while ((tk = scanner.read()).format() != Format_EndOfBlock) { Format format = tk.format(); if (tk.format() == Format_Identifier) format = Format_ImportedModule; setFormat(tk.begin(), tk.length(), formatForCategory(format)); } }
void Highlighter::highlightImport(Scanner &scanner) { FormatToken tk; while ((tk = scanner.read()).format() != Format_EndOfBlock) { Format format = tk.format(); if (tk.format() == Format_IDENTIFIER) format = Format_IMPORTED_MODULE; setFormat(tk.begin(), tk.length(), _formats[format]); } }
/** * @brief Highlighter::highlight_impl * @param text - source code to highlight * @param initialState - initial state of scanner, retrieved from previous block * @return final state of scanner, should be saved with current block */ int Highlighter::highlightLine(const QString &text, int initialState) { Scanner scanner(text.constData(), text.size()); scanner.setState(initialState); HState hstate = HState_NORMAL; int blockNo = currentBlock().blockNumber(); QMap<int, QString>::iterator it = _userTypes.find(blockNo); if (it != _userTypes.end()) _userTypes.erase(it); FormatToken tk; bool hasOnlyWhitespace = true; while ((tk = scanner.read()).format() != Format_EndOfBlock) { Format format = tk.format(); switch (hstate) { default: // HState_NORMAL if (format == Format_KEYWORD) { QString value = scanner.value(tk); if (value == "class") { hstate = HState_ON_CLASS; } else if (isImportKeyword(value) && hasOnlyWhitespace) { setFormat(tk.begin(), tk.length(), _formats[format]); highlightImport(scanner); return scanner.getState(); } } if (format == Format_IDENTIFIER) { QString value = scanner.value(tk); QMap<int, QString>::const_iterator it; for (it = _userTypes.begin(); it != _userTypes.end(); ++it) { if (value == it.value()) { format = Format_TYPE; break; } } } break; case HState_ON_CLASS: if (format == Format_WHITESPACE) hstate = HState_AFTER_CLASS; else hstate = HState_NORMAL; break; case HState_AFTER_CLASS: hstate = HState_NORMAL; if (format == Format_IDENTIFIER) { _userTypes[blockNo] = scanner.value(tk); format = Format_TYPE; } break; } setFormat(tk.begin(), tk.length(), _formats[format]); if (format != Format_WHITESPACE) hasOnlyWhitespace = false; } return scanner.getState(); }