CppEditor: Clarify different states in CppHighlighter::highlightBlock

...by giving the variables better names.

Change-Id: I0f98dfc958c3dd478a070526a4ca454b22cf495a
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Nikolai Kosjar
2014-02-26 12:50:15 -03:00
parent 453389e9d2
commit d6d233f51f

View File

@@ -65,11 +65,11 @@ CppHighlighter::CppHighlighter(QTextDocument *document) :
void CppHighlighter::highlightBlock(const QString &text) void CppHighlighter::highlightBlock(const QString &text)
{ {
const int previousState = previousBlockState(); const int previousBlockState_ = previousBlockState();
int state = 0, initialBraceDepth = 0; int lexerState = 0, initialBraceDepth = 0;
if (previousState != -1) { if (previousBlockState_ != -1) {
state = previousState & 0xff; lexerState = previousBlockState_ & 0xff;
initialBraceDepth = previousState >> 8; initialBraceDepth = previousBlockState_ >> 8;
} }
int braceDepth = initialBraceDepth; int braceDepth = initialBraceDepth;
@@ -81,11 +81,11 @@ void CppHighlighter::highlightBlock(const QString &text)
SimpleLexer tokenize; SimpleLexer tokenize;
tokenize.setLanguageFeatures(features); tokenize.setLanguageFeatures(features);
int initialState = state; int initialLexerState = lexerState;
const QList<Token> tokens = tokenize(text, initialState); const QList<Token> tokens = tokenize(text, initialLexerState);
state = tokenize.state(); // refresh the state lexerState = tokenize.state(); // refresh lexer state
initialState &= ~0x80; // discard newline expected bit initialLexerState &= ~0x80; // discard newline expected bit
int foldingIndent = initialBraceDepth; int foldingIndent = initialBraceDepth;
if (TextBlockUserData *userData = BaseTextDocumentLayout::testUserData(currentBlock())) { if (TextBlockUserData *userData = BaseTextDocumentLayout::testUserData(currentBlock())) {
userData->setFoldingIndent(0); userData->setFoldingIndent(0);
@@ -94,12 +94,12 @@ void CppHighlighter::highlightBlock(const QString &text)
} }
if (tokens.isEmpty()) { if (tokens.isEmpty()) {
setCurrentBlockState((braceDepth << 8) | state); setCurrentBlockState((braceDepth << 8) | lexerState);
BaseTextDocumentLayout::clearParentheses(currentBlock()); BaseTextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace if (text.length()) {// the empty line can still contain whitespace
if (initialState == T_COMMENT) if (initialLexerState == T_COMMENT)
highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat)); highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat));
else if (initialState == T_DOXY_COMMENT) else if (initialLexerState == T_DOXY_COMMENT)
highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat)); highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
else else
setFormat(0, text.length(), formatForCategory(CppVisualWhitespace)); setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));
@@ -183,7 +183,7 @@ void CppHighlighter::highlightBlock(const QString &text)
} else if (tk.isStringLiteral() || tk.isCharLiteral()) { } else if (tk.isStringLiteral() || tk.isCharLiteral()) {
highlightLine(text, tk.begin(), tk.length(), formatForCategory(CppStringFormat)); highlightLine(text, tk.begin(), tk.length(), formatForCategory(CppStringFormat));
} else if (tk.isComment()) { } else if (tk.isComment()) {
const int startPosition = initialState ? previousTokenEnd : tk.begin(); const int startPosition = initialLexerState ? previousTokenEnd : tk.begin();
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT)) if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT))
highlightLine(text, startPosition, tk.end() - startPosition, formatForCategory(CppCommentFormat)); highlightLine(text, startPosition, tk.end() - startPosition, formatForCategory(CppCommentFormat));
@@ -194,7 +194,7 @@ void CppHighlighter::highlightBlock(const QString &text)
// - the line starts in a C Comment (initalState != 0) // - 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)) // - the first token of the line is a T_COMMENT (i == 0 && tk.is(T_COMMENT))
// - is not a continuation line (tokens.size() > 1 || !state) // - is not a continuation line (tokens.size() > 1 || !state)
if (initialState && i == 0 && (tokens.size() > 1 || !state)) { if (initialLexerState && i == 0 && (tokens.size() > 1 || !lexerState)) {
--braceDepth; --braceDepth;
// unless we are at the end of the block, we reduce the folding indent // unless we are at the end of the block, we reduce the folding indent
if (i == tokens.size()-1) if (i == tokens.size()-1)
@@ -205,7 +205,7 @@ void CppHighlighter::highlightBlock(const QString &text)
parentheses.append(Parenthesis(Parenthesis::Closed, QLatin1Char('-'), tokenEnd)); parentheses.append(Parenthesis(Parenthesis::Closed, QLatin1Char('-'), tokenEnd));
// clear the initial state. // clear the initial state.
initialState = 0; initialLexerState = 0;
} }
} else if (tk.isKeyword() || CppTools::isQtKeyword(text.midRef(tk.begin(), tk.length())) || tk.isObjCAtKeyword()) { } else if (tk.isKeyword() || CppTools::isQtKeyword(text.midRef(tk.begin(), tk.length())) || tk.isObjCAtKeyword()) {
@@ -224,7 +224,7 @@ void CppHighlighter::highlightBlock(const QString &text)
if (text.length() > lastTokenEnd) if (text.length() > lastTokenEnd)
highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(CppVisualWhitespace)); highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(CppVisualWhitespace));
if (!initialState && state && !tokens.isEmpty() && tokens.last().isComment()) { if (!initialLexerState && lexerState && !tokens.isEmpty() && tokens.last().isComment()) {
parentheses.append(Parenthesis(Parenthesis::Opened, QLatin1Char('+'), parentheses.append(Parenthesis(Parenthesis::Opened, QLatin1Char('+'),
tokens.last().begin())); tokens.last().begin()));
++braceDepth; ++braceDepth;