2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cpphighlighter.h"
|
2013-08-13 12:57:31 +02:00
|
|
|
#include "cppeditorenums.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-08-22 15:24:40 +02:00
|
|
|
#include <cpptools/cppdoxygen.h>
|
2010-11-03 11:02:25 +01:00
|
|
|
#include <cpptools/cpptoolsreuse.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/textdocumentlayout.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/SimpleLexer.h>
|
|
|
|
|
#include <cplusplus/Lexer.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextDocument>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
using namespace CppEditor::Internal;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
2008-12-16 13:19:11 +01:00
|
|
|
CppHighlighter::CppHighlighter(QTextDocument *document) :
|
2015-02-04 17:01:07 +02:00
|
|
|
SyntaxHighlighter(document)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2016-09-23 15:46:41 +02:00
|
|
|
static const QVector<TextStyle> categories({
|
|
|
|
|
C_NUMBER,
|
|
|
|
|
C_STRING,
|
|
|
|
|
C_TYPE,
|
|
|
|
|
C_KEYWORD,
|
|
|
|
|
C_PRIMITIVE_TYPE,
|
|
|
|
|
C_OPERATOR,
|
|
|
|
|
C_PREPROCESSOR,
|
|
|
|
|
C_LABEL,
|
|
|
|
|
C_COMMENT,
|
|
|
|
|
C_DOXYGEN_COMMENT,
|
|
|
|
|
C_DOXYGEN_TAG,
|
|
|
|
|
C_VISUAL_WHITESPACE
|
|
|
|
|
});
|
2013-08-13 12:57:31 +02:00
|
|
|
setTextFormatCategories(categories);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-16 13:19:11 +01:00
|
|
|
void CppHighlighter::highlightBlock(const QString &text)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-02-26 12:50:15 -03:00
|
|
|
const int previousBlockState_ = previousBlockState();
|
|
|
|
|
int lexerState = 0, initialBraceDepth = 0;
|
|
|
|
|
if (previousBlockState_ != -1) {
|
|
|
|
|
lexerState = previousBlockState_ & 0xff;
|
|
|
|
|
initialBraceDepth = previousBlockState_ >> 8;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-09-25 17:22:10 +02:00
|
|
|
int braceDepth = initialBraceDepth;
|
2009-05-06 16:48:51 +02:00
|
|
|
|
2013-10-06 02:41:22 +02:00
|
|
|
// FIXME: Check defaults or get from document.
|
|
|
|
|
LanguageFeatures features;
|
|
|
|
|
features.cxx11Enabled = true;
|
2016-02-26 18:48:39 +01:00
|
|
|
features.cxxEnabled = true;
|
2014-05-05 22:56:15 +08:00
|
|
|
features.c99Enabled = true;
|
2016-09-12 13:42:42 +02:00
|
|
|
features.objCEnabled = true;
|
2013-10-06 02:41:22 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
SimpleLexer tokenize;
|
2013-10-06 02:41:22 +02:00
|
|
|
tokenize.setLanguageFeatures(features);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-02-26 12:50:15 -03:00
|
|
|
int initialLexerState = lexerState;
|
2014-11-06 09:35:29 +01:00
|
|
|
const Tokens tokens = tokenize(text, initialLexerState);
|
2014-02-26 12:50:15 -03:00
|
|
|
lexerState = tokenize.state(); // refresh lexer state
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-02-26 12:50:15 -03:00
|
|
|
initialLexerState &= ~0x80; // discard newline expected bit
|
2010-05-20 15:10:26 +02:00
|
|
|
int foldingIndent = initialBraceDepth;
|
2014-09-26 09:14:03 +02:00
|
|
|
if (TextBlockUserData *userData = TextDocumentLayout::testUserData(currentBlock())) {
|
2010-05-20 15:10:26 +02:00
|
|
|
userData->setFoldingIndent(0);
|
|
|
|
|
userData->setFoldingStartIncluded(false);
|
|
|
|
|
userData->setFoldingEndIncluded(false);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (tokens.isEmpty()) {
|
2014-02-26 12:50:15 -03:00
|
|
|
setCurrentBlockState((braceDepth << 8) | lexerState);
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::clearParentheses(currentBlock());
|
2012-02-07 13:48:22 +01:00
|
|
|
if (text.length()) {// the empty line can still contain whitespace
|
2014-02-26 12:50:15 -03:00
|
|
|
if (initialLexerState == T_COMMENT)
|
2013-08-13 12:57:31 +02:00
|
|
|
highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat));
|
2014-02-26 12:50:15 -03:00
|
|
|
else if (initialLexerState == T_DOXY_COMMENT)
|
2013-08-13 12:57:31 +02:00
|
|
|
highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
|
2012-02-07 13:48:22 +01:00
|
|
|
else
|
2013-08-13 12:57:31 +02:00
|
|
|
setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));
|
2012-02-07 13:48:22 +01:00
|
|
|
}
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 21:37:46 +01:00
|
|
|
const unsigned firstNonSpace = tokens.first().utf16charsBegin();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
Parentheses parentheses;
|
2015-05-11 15:05:19 +02:00
|
|
|
parentheses.reserve(5);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-03-17 11:03:08 +01:00
|
|
|
bool expectPreprocessorKeyword = false;
|
|
|
|
|
bool onlyHighlightComments = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i < tokens.size(); ++i) {
|
2010-06-29 17:57:15 +02:00
|
|
|
const Token &tk = tokens.at(i);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-29 17:57:15 +02:00
|
|
|
unsigned previousTokenEnd = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
if (i != 0) {
|
|
|
|
|
// mark the whitespaces
|
2013-12-12 21:37:46 +01:00
|
|
|
previousTokenEnd = tokens.at(i - 1).utf16charsBegin() +
|
|
|
|
|
tokens.at(i - 1).utf16chars();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-12 21:37:46 +01:00
|
|
|
if (previousTokenEnd != tk.utf16charsBegin()) {
|
2013-12-13 18:41:15 +01:00
|
|
|
setFormat(previousTokenEnd,
|
2013-12-12 21:37:46 +01:00
|
|
|
tk.utf16charsBegin() - previousTokenEnd,
|
2013-12-13 18:41:15 +01:00
|
|
|
formatForCategory(CppVisualWhitespace));
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
const QChar c = text.at(tk.utf16charsBegin());
|
|
|
|
|
parentheses.append(Parenthesis(Parenthesis::Opened, c, tk.utf16charsBegin()));
|
2010-05-20 15:10:26 +02:00
|
|
|
if (tk.is(T_LBRACE)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
++braceDepth;
|
2010-05-20 15:10:26 +02:00
|
|
|
|
|
|
|
|
// if a folding block opens at the beginning of a line, treat the entire line
|
|
|
|
|
// as if it were inside the folding block
|
2013-12-12 21:37:46 +01:00
|
|
|
if (tk.utf16charsBegin() == firstNonSpace) {
|
2010-05-20 15:10:26 +02:00
|
|
|
++foldingIndent;
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true);
|
2010-05-20 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
} else if (tk.is(T_RPAREN) || tk.is(T_RBRACE) || tk.is(T_RBRACKET)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
const QChar c = text.at(tk.utf16charsBegin());
|
|
|
|
|
parentheses.append(Parenthesis(Parenthesis::Closed, c, tk.utf16charsBegin()));
|
2010-05-20 15:10:26 +02:00
|
|
|
if (tk.is(T_RBRACE)) {
|
2009-09-16 16:16:45 +02:00
|
|
|
--braceDepth;
|
2010-05-20 15:10:26 +02:00
|
|
|
if (braceDepth < foldingIndent) {
|
|
|
|
|
// unless we are at the end of the block, we reduce the folding indent
|
|
|
|
|
if (i == tokens.size()-1 || tokens.at(i+1).is(T_SEMICOLON))
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
|
2010-05-20 15:10:26 +02:00
|
|
|
else
|
|
|
|
|
foldingIndent = qMin(braceDepth, foldingIndent);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-17 11:03:08 +01:00
|
|
|
bool highlightCurrentWordAsPreprocessor = expectPreprocessorKeyword;
|
2009-02-20 11:52:27 +01:00
|
|
|
|
2011-03-17 11:03:08 +01:00
|
|
|
if (expectPreprocessorKeyword)
|
|
|
|
|
expectPreprocessorKeyword = false;
|
|
|
|
|
|
|
|
|
|
if (onlyHighlightComments && !tk.isComment())
|
|
|
|
|
continue;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
if (i == 0 && tk.is(T_POUND)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(),
|
2013-12-13 18:41:15 +01:00
|
|
|
formatForCategory(CppPreprocessorFormat));
|
2011-03-17 11:03:08 +01:00
|
|
|
expectPreprocessorKeyword = true;
|
2013-12-13 18:41:15 +01:00
|
|
|
} else if (highlightCurrentWordAsPreprocessor
|
|
|
|
|
&& (tk.isKeyword() || tk.is(T_IDENTIFIER))
|
2013-12-12 21:37:46 +01:00
|
|
|
&& isPPKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))) {
|
|
|
|
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppPreprocessorFormat));
|
|
|
|
|
const QStringRef ppKeyword = text.midRef(tk.utf16charsBegin(), tk.utf16chars());
|
2011-03-17 11:03:08 +01:00
|
|
|
if (ppKeyword == QLatin1String("error")
|
|
|
|
|
|| ppKeyword == QLatin1String("warning")
|
|
|
|
|
|| ppKeyword == QLatin1String("pragma")) {
|
|
|
|
|
onlyHighlightComments = true;
|
|
|
|
|
}
|
2009-02-20 11:52:27 +01:00
|
|
|
|
2013-07-17 00:01:45 +03:00
|
|
|
} else if (tk.is(T_NUMERIC_LITERAL)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppNumberFormat));
|
2013-07-17 00:01:45 +03:00
|
|
|
} else if (tk.isStringLiteral() || tk.isCharLiteral()) {
|
2013-12-12 21:37:46 +01:00
|
|
|
highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppStringFormat));
|
2013-07-17 00:01:45 +03:00
|
|
|
} else if (tk.isComment()) {
|
2013-12-12 21:37:46 +01:00
|
|
|
const int startPosition = initialLexerState ? previousTokenEnd : tk.utf16charsBegin();
|
2013-12-13 18:41:15 +01:00
|
|
|
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
highlightLine(text, startPosition, tk.utf16charsEnd() - startPosition,
|
2013-12-13 18:41:15 +01:00
|
|
|
formatForCategory(CppCommentFormat));
|
|
|
|
|
}
|
2009-02-20 11:52:27 +01:00
|
|
|
|
|
|
|
|
else // a doxygen comment
|
2013-12-12 21:37:46 +01:00
|
|
|
highlightDoxygenComment(text, startPosition, tk.utf16charsEnd() - startPosition);
|
2009-02-20 11:52:27 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// we need to insert a close comment parenthesis, if
|
|
|
|
|
// - the line starts in a C Comment (initalState != 0)
|
|
|
|
|
// - the first token of the line is a T_COMMENT (i == 0 && tk.is(T_COMMENT))
|
2013-07-24 11:52:01 +02:00
|
|
|
// - is not a continuation line (tokens.size() > 1 || !state)
|
2014-02-26 12:50:15 -03:00
|
|
|
if (initialLexerState && i == 0 && (tokens.size() > 1 || !lexerState)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
--braceDepth;
|
2010-05-20 15:10:26 +02:00
|
|
|
// unless we are at the end of the block, we reduce the folding indent
|
|
|
|
|
if (i == tokens.size()-1)
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
|
2010-05-20 15:10:26 +02:00
|
|
|
else
|
|
|
|
|
foldingIndent = qMin(braceDepth, foldingIndent);
|
2013-12-12 21:37:46 +01:00
|
|
|
const int tokenEnd = tk.utf16charsBegin() + tk.utf16chars() - 1;
|
2008-12-02 12:01:29 +01:00
|
|
|
parentheses.append(Parenthesis(Parenthesis::Closed, QLatin1Char('-'), tokenEnd));
|
|
|
|
|
|
|
|
|
|
// clear the initial state.
|
2014-02-26 12:50:15 -03:00
|
|
|
initialLexerState = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-02-20 11:52:27 +01:00
|
|
|
|
2013-12-13 18:41:15 +01:00
|
|
|
} else if (tk.isKeyword()
|
2013-12-12 21:37:46 +01:00
|
|
|
|| CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))
|
2013-12-13 18:41:15 +01:00
|
|
|
|| tk.isObjCAtKeyword()) {
|
2013-12-12 21:37:46 +01:00
|
|
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat));
|
2014-06-21 14:31:06 +04:00
|
|
|
} else if (tk.isPrimitiveType()) {
|
|
|
|
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(),
|
|
|
|
|
formatForCategory(CppPrimitiveTypeFormat));
|
2013-07-17 00:01:45 +03:00
|
|
|
} else if (tk.isOperator()) {
|
2013-12-12 21:37:46 +01:00
|
|
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppOperatorFormat));
|
2013-07-17 00:01:45 +03:00
|
|
|
} else if (i == 0 && tokens.size() > 1 && tk.is(T_IDENTIFIER) && tokens.at(1).is(T_COLON)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppLabelFormat));
|
2013-07-17 00:01:45 +03:00
|
|
|
} else if (tk.is(T_IDENTIFIER)) {
|
2013-12-12 21:37:46 +01:00
|
|
|
highlightWord(text.midRef(tk.utf16charsBegin(), tk.utf16chars()), tk.utf16charsBegin(),
|
|
|
|
|
tk.utf16chars());
|
2013-07-17 00:01:45 +03:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark the trailing white spaces
|
2013-12-12 21:37:46 +01:00
|
|
|
const int lastTokenEnd = tokens.last().utf16charsEnd();
|
2012-08-22 15:24:40 +02:00
|
|
|
if (text.length() > lastTokenEnd)
|
2013-08-13 12:57:31 +02:00
|
|
|
highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(CppVisualWhitespace));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-02-08 23:42:36 +02:00
|
|
|
if (!initialLexerState && lexerState && !tokens.isEmpty()) {
|
|
|
|
|
const Token &lastToken = tokens.last();
|
|
|
|
|
if (lastToken.is(T_COMMENT) || lastToken.is(T_DOXY_COMMENT)) {
|
|
|
|
|
parentheses.append(Parenthesis(Parenthesis::Opened, QLatin1Char('+'),
|
2013-12-12 21:37:46 +01:00
|
|
|
lastToken.utf16charsBegin()));
|
2014-02-08 23:42:36 +02:00
|
|
|
++braceDepth;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::setParentheses(currentBlock(), parentheses);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-09-25 17:22:10 +02:00
|
|
|
// if the block is ifdefed out, we only store the parentheses, but
|
2010-05-20 15:10:26 +02:00
|
|
|
|
2009-09-25 17:22:10 +02:00
|
|
|
// do not adjust the brace depth.
|
2014-09-26 09:14:03 +02:00
|
|
|
if (TextDocumentLayout::ifdefedOut(currentBlock())) {
|
2009-09-25 17:22:10 +02:00
|
|
|
braceDepth = initialBraceDepth;
|
2010-05-20 15:10:26 +02:00
|
|
|
foldingIndent = initialBraceDepth;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
|
2009-05-06 16:48:51 +02:00
|
|
|
|
|
|
|
|
// optimization: if only the brace depth changes, we adjust subsequent blocks
|
|
|
|
|
// to have QSyntaxHighlighter stop the rehighlighting
|
|
|
|
|
int currentState = currentBlockState();
|
|
|
|
|
if (currentState != -1) {
|
|
|
|
|
int oldState = currentState & 0xff;
|
|
|
|
|
int oldBraceDepth = currentState >> 8;
|
|
|
|
|
if (oldState == tokenize.state() && oldBraceDepth != braceDepth) {
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::FoldValidator foldValidor;
|
|
|
|
|
foldValidor.setup(qobject_cast<TextDocumentLayout *>(document()->documentLayout()));
|
2009-05-06 16:48:51 +02:00
|
|
|
int delta = braceDepth - oldBraceDepth;
|
|
|
|
|
QTextBlock block = currentBlock().next();
|
2009-11-23 15:15:56 +01:00
|
|
|
while (block.isValid() && block.userState() != -1) {
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::changeBraceDepth(block, delta);
|
|
|
|
|
TextDocumentLayout::changeFoldingIndent(block, delta);
|
2011-08-12 12:13:23 +02:00
|
|
|
foldValidor.process(block);
|
2009-05-06 16:48:51 +02:00
|
|
|
block = block.next();
|
|
|
|
|
}
|
2011-08-12 12:13:23 +02:00
|
|
|
foldValidor.finalize();
|
2009-05-06 16:48:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
setCurrentBlockState((braceDepth << 8) | tokenize.state());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-12-16 13:19:11 +01:00
|
|
|
bool CppHighlighter::isPPKeyword(const QStringRef &text) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
switch (text.length())
|
|
|
|
|
{
|
|
|
|
|
case 2:
|
2012-01-11 14:26:09 +01:00
|
|
|
if (text.at(0) == QLatin1Char('i') && text.at(1) == QLatin1Char('f'))
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
2012-01-11 14:26:09 +01:00
|
|
|
if (text.at(0) == QLatin1Char('e')
|
|
|
|
|
&& (text == QLatin1String("elif") || text == QLatin1String("else")))
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 5:
|
2012-01-11 14:26:09 +01:00
|
|
|
switch (text.at(0).toLatin1()) {
|
|
|
|
|
case 'i':
|
|
|
|
|
if (text == QLatin1String("ifdef"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
case 'u':
|
|
|
|
|
if (text == QLatin1String("undef"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
case 'e':
|
|
|
|
|
if (text == QLatin1String("endif") || text == QLatin1String("error"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6:
|
2012-01-11 14:26:09 +01:00
|
|
|
switch (text.at(0).toLatin1()) {
|
|
|
|
|
case 'i':
|
|
|
|
|
if (text == QLatin1String("ifndef") || text == QLatin1String("import"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
|
|
|
|
if (text == QLatin1String("define"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
if (text == QLatin1String("pragma"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7:
|
2012-01-11 14:26:09 +01:00
|
|
|
switch (text.at(0).toLatin1()) {
|
|
|
|
|
case 'i':
|
|
|
|
|
if (text == QLatin1String("include"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
case 'w':
|
|
|
|
|
if (text == QLatin1String("warning"))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 12:
|
2012-01-11 14:26:09 +01:00
|
|
|
if (text.at(0) == QLatin1Char('i') && text == QLatin1String("include_next"))
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-16 17:42:29 +02:00
|
|
|
void CppHighlighter::highlightLine(const QString &text, int position, int length,
|
|
|
|
|
const QTextCharFormat &format)
|
2009-10-08 12:56:56 +02:00
|
|
|
{
|
2013-08-13 12:57:31 +02:00
|
|
|
QTextCharFormat visualSpaceFormat = formatForCategory(CppVisualWhitespace);
|
2012-08-22 15:24:40 +02:00
|
|
|
visualSpaceFormat.setBackground(format.background());
|
2009-10-08 12:56:56 +02:00
|
|
|
|
|
|
|
|
const int end = position + length;
|
|
|
|
|
int index = position;
|
|
|
|
|
|
|
|
|
|
while (index != end) {
|
|
|
|
|
const bool isSpace = text.at(index).isSpace();
|
|
|
|
|
const int start = index;
|
|
|
|
|
|
|
|
|
|
do { ++index; }
|
|
|
|
|
while (index != end && text.at(index).isSpace() == isSpace);
|
|
|
|
|
|
|
|
|
|
const int tokenLength = index - start;
|
2010-04-16 17:39:55 +02:00
|
|
|
if (isSpace)
|
|
|
|
|
setFormat(start, tokenLength, visualSpaceFormat);
|
|
|
|
|
else if (format.isValid())
|
|
|
|
|
setFormat(start, tokenLength, format);
|
2009-10-08 12:56:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-16 13:19:11 +01:00
|
|
|
void CppHighlighter::highlightWord(QStringRef word, int position, int length)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
// try to highlight Qt 'identifiers' like QObject and Q_PROPERTY
|
2010-05-25 14:53:21 +02:00
|
|
|
|
2010-06-01 10:08:00 +02:00
|
|
|
if (word.length() > 2 && word.at(0) == QLatin1Char('Q')) {
|
|
|
|
|
if (word.at(1) == QLatin1Char('_') // Q_
|
2010-06-25 14:12:01 +10:00
|
|
|
|| (word.at(1) == QLatin1Char('T') && word.at(2) == QLatin1Char('_'))) { // QT_
|
2010-06-01 10:08:00 +02:00
|
|
|
for (int i = 1; i < word.length(); ++i) {
|
|
|
|
|
const QChar &ch = word.at(i);
|
2013-07-24 11:52:01 +02:00
|
|
|
if (!(ch.isUpper() || ch == QLatin1Char('_')))
|
2010-06-01 10:08:00 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2010-05-25 14:53:21 +02:00
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
setFormat(position, length, formatForCategory(CppTypeFormat));
|
2010-06-01 10:08:00 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
2009-02-20 11:52:27 +01:00
|
|
|
|
2009-02-20 11:53:32 +01:00
|
|
|
void CppHighlighter::highlightDoxygenComment(const QString &text, int position, int)
|
2009-02-20 11:52:27 +01:00
|
|
|
{
|
|
|
|
|
int initial = position;
|
|
|
|
|
|
|
|
|
|
const QChar *uc = text.unicode();
|
|
|
|
|
const QChar *it = uc + position;
|
|
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
const QTextCharFormat &format = formatForCategory(CppDoxygenCommentFormat);
|
|
|
|
|
const QTextCharFormat &kwFormat = formatForCategory(CppDoxygenTagFormat);
|
2009-02-20 11:52:27 +01:00
|
|
|
|
2013-07-24 11:52:01 +02:00
|
|
|
while (!it->isNull()) {
|
2009-02-20 11:52:27 +01:00
|
|
|
if (it->unicode() == QLatin1Char('\\') ||
|
|
|
|
|
it->unicode() == QLatin1Char('@')) {
|
|
|
|
|
++it;
|
|
|
|
|
|
|
|
|
|
const QChar *start = it;
|
2014-05-08 09:48:27 -04:00
|
|
|
while (CppTools::isValidAsciiIdentifierChar(*it))
|
2009-02-20 11:52:27 +01:00
|
|
|
++it;
|
|
|
|
|
|
2009-02-20 12:55:01 +01:00
|
|
|
int k = CppTools::classifyDoxygenTag(start, it - start);
|
|
|
|
|
if (k != CppTools::T_DOXY_IDENTIFIER) {
|
2012-02-03 11:46:13 +01:00
|
|
|
highlightLine(text, initial, start - uc - initial, format);
|
2009-02-20 11:52:27 +01:00
|
|
|
setFormat(start - uc - 1, it - start + 1, kwFormat);
|
|
|
|
|
initial = it - uc;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 11:46:13 +01:00
|
|
|
highlightLine(text, initial, it - uc - initial, format);
|
2009-02-20 11:52:27 +01:00
|
|
|
}
|
|
|
|
|
|