Newer
Older
/****************************************************************************
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** 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
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cppcodeformatter.h"
#include <texteditor/basetextdocumentlayout.h>
#include <cplusplus/Lexer.h>
#include <utils/qtcassert.h>
#include <QDebug>
#include <QMetaEnum>
#include <QTextDocument>
#include <QTextBlock>
using namespace CPlusPlus;
using namespace CppTools;
using namespace TextEditor;
using namespace CppTools::Internal;
CodeFormatter::BlockData::BlockData()
: m_blockRevision(-1)
{
}
: m_indentDepth(0)
, m_paddingDepth(0)
, m_tabSize(4)
{
}
CodeFormatter::~CodeFormatter()
{
}
void CodeFormatter::setTabSize(int tabSize)
{
m_tabSize = tabSize;
}
void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
{
restoreCurrentState(block.previous());
bool endedJoined = false;
const int lexerState = tokenizeBlock(block, &endedJoined);
m_tokenIndex = 0;
m_newStates.clear();
if (tokenAt(0).kind() == T_POUND) {
enter(cpp_macro_start);
m_tokenIndex = 1;
}
for (; m_tokenIndex < m_tokens.size(); ) {
m_currentToken = tokenAt(m_tokenIndex);
const int kind = m_currentToken.kind();
switch (m_currentState.top().type) {
case topmost_intro:
tryDeclaration();
break;
case namespace_start:
switch (kind) {
case T_LBRACE: enter(namespace_open); break;
case T_SEMICOLON:
case T_RBRACE: leave(); break;
} break;
case namespace_open:
if (tryDeclaration())
break;
switch (kind) {
case T_RBRACE: leave(); continue; // always nested in namespace_start
case extern_start:
switch (kind) {
case T_STRING_LITERAL: break; // continue looking for the lbrace
case T_LBRACE: enter(extern_open); break;
default: leave(); continue;
} break;
case extern_open:
if (tryDeclaration())
break;
switch (kind) {
case T_RBRACE: leave(); leave(); break; // always nested in extern_start
} break;
case class_start:
switch (kind) {
case T_SEMICOLON: leave(); break;
case T_LPAREN: turnInto(declaration_start); continue; // "struct Foo bar() {"
case T_LBRACE: enter(class_open); break;
} break;
case class_open:
if (tryDeclaration())
break;
switch (kind) {
case T_RBRACE: leave(); continue; // always nested in class_start
case access_specifier_start:
switch (kind) {
case T_COLON: leave(); break;
} break;
case enum_start:
switch (kind) {
case T_SEMICOLON: leave(); break;
case T_LPAREN: turnInto(declaration_start); continue; // "enum Foo bar() {"
case T_LBRACE: enter(enum_open); break;
} break;
case enum_open:
switch (kind) {
case T_RBRACE: leave(); continue; // always nested in enum_start
case T_LBRACE: enter(brace_list_open); break;
} break;
case brace_list_open:
switch (kind) {
case T_RBRACE: leave(); break;
case T_LBRACE: enter(brace_list_open); break;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
} break;
case using_start:
switch (kind) {
case T_SEMICOLON: leave(); break;
} break;
case template_start:
switch (kind) {
case T_LESS: turnInto(template_param); break;
} break;
case template_param:
switch (kind) {
case T_LESS: enter(template_param); break;
case T_GREATER: leave(); break;
} break;
case operator_declaration:
switch (kind) {
case T_LPAREN: break;
default: leave(); break;
} break;
case declaration_start:
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(true); break;
case T_EQUAL: enter(assign_open_or_initializer); break;
case T_LBRACE: enter(defun_open); break;
case T_COLON: enter(member_init_open); enter(member_init_expected); break;
case T_OPERATOR: enter(operator_declaration); break;
switch (kind) {
case T_LBRACE: enter(brace_list_open); break;
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(); continue;
case T_RPAREN: leave(); continue;
case T_COMMA: leave(); continue;
default: enter(assign_open); continue;
} break;
case expression:
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(); continue;
case T_LBRACE:
case T_COLON:
if (m_currentState.at(m_currentState.size() - 2).type == declaration_start) {
// oops, the expression was a function declaration argument list, hand lbrace/colon to declaration_start
leave();
continue;
} else {
turnInto(substatement_open);
default: tryExpression(); break;
} break;
case assign_open:
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(); continue;
case T_RPAREN: leave(); continue;
case T_COMMA: leave(); continue;
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
case lambda_instroducer_or_subscribtion:
switch (kind) {
case T_RBRACKET: turnInto(lambda_declarator_expected); break; // we can't determine exact kind of expression. Try again
case T_COMMA:
case T_EQUAL: turnInto(lambda_instroducer); break; // ',' or '=' inside brackets can be only whithin lambda capture list
case T_IDENTIFIER: // '&', id, 'this' are allowed both in the capture list and subscribtion
case T_AMPER:
case T_THIS: break;
default: leave(); leave(); tryExpression(m_currentState.at(m_currentState.size() - 1).type == declaration_start); break;
// any other symbol allowed only in subscribtion operator
} break;
case lambda_declarator_expected:
switch (kind) {
case T_LPAREN: turnInto(lambda_declarator_or_expression); break; // '(' just after ']'. We can't make decisioin here
case T_LBRACE: turnInto(substatement_open); break; // '{' just after ']' opens a lambda-compound statement
default:
if (m_currentState.size() >= 3 && m_currentState.at(m_currentState.size() - 3).type == declaration_start)
leave();
leave();
continue;
} break;
case lambda_instroducer:
switch (kind) {
case T_RBRACKET: turnInto(lambda_declarator); break;
} break;
case lambda_declarator_or_expression:
switch (kind) {
case T_LBRACE: turnInto(substatement_open); /*tryStatement();*/ break;
case T_RPAREN: turnInto(lambda_statement_expected); break;
case T_IDENTIFIER:
case T_SEMICOLON: leave(); continue;
default:
if (tryDeclaration()) {// We found the declaration within '()' so it is lambda declarator
leave();
turnInto(lambda_declarator);
break;
} else {
turnInto(expression);
enter(arglist_open);
continue;
}
} break;
case lambda_statement_expected:
switch (kind) {
case T_LBRACE: turnInto(substatement_open); /*tryStatement()*/; break;
case T_NOEXCEPT: // 'noexcept', 'decltype' and 'mutable' are only part of lambda declarator
case T_DECLTYPE:
case T_MUTABLE: turnInto(lambda_declarator); break;
case T_RBRACKET: // '[', ']' and '->' can be part of lambda declarator
case T_LBRACKET:
case T_ARROW: break;
default: leave(); continue;
} break;
case lambda_declarator:
switch (kind) {
case T_LBRACE: turnInto(substatement_open); /*tryStatement()*/; break;
} break;
case arglist_open:
switch (kind) {
case T_SEMICOLON: leave(true); break;
case T_LBRACE: enter(brace_list_open); break;
case T_RBRACE: leave(true); continue;
case braceinit_open:
switch (kind) {
case T_RBRACE: leave(); break;
case T_RPAREN: leave(); continue; // recover?
default: tryExpression(); break;
} break;
case ternary_op:
switch (kind) {
case T_RPAREN:
case T_COMMA:
case T_SEMICOLON: leave(); continue; // always nested, propagate
case stream_op_cont:
case T_LESS_LESS:
case T_GREATER_GREATER:
if (m_currentState.top().type == stream_op)
enter(stream_op_cont);
else // stream_op_cont already
turnInto(stream_op_cont);
break;
case T_SEMICOLON: leave(); continue; // always nested, propagate
} break;
case member_init_open:
switch (kind) {
case T_LBRACE: turnInto(defun_open); break;
case T_COMMA: enter(member_init_expected); break;
case T_SEMICOLON: leave(); continue; // try to recover
} break;
case member_init_expected:
switch (kind) {
case T_IDENTIFIER: turnInto(member_init); break;
case T_LBRACE:
case T_SEMICOLON: leave(); continue; // try to recover
} break;
case member_init:
switch (kind) {
case T_LBRACE:
case T_LPAREN: enter(member_init_nest_open); break;
case T_RBRACE:
case T_RPAREN: leave(); break;
case T_SEMICOLON: leave(); continue; // try to recover
} break;
case member_init_nest_open:
switch (kind) {
case T_RPAREN: leave(); continue;
case T_SEMICOLON: leave(); continue; // try to recover
} break;
case defun_open:
if (tryStatement())
break;
switch (kind) {
case T_RBRACE: leave(); leave(); break; // always nested in declaration_start
} break;
case switch_statement:
case statement_with_condition:
case if_statement:
switch (kind) {
case T_LPAREN: enter(condition_open); break;
default: leave(true); continue;
} break;
case maybe_else:
if (m_currentToken.isComment()) {
break;
} else if (kind == T_ELSE) {
turnInto(else_clause);
enter(substatement);
break;
} else {
leave(true);
continue;
}
case else_clause:
// ### shouldn't happen
dump();
QTC_CHECK(false);
leave(true);
break;
case do_statement:
// ### shouldn't happen
dump();
QTC_CHECK(false);
leave(true);
break;
case return_statement:
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(true); break;
} break;
case substatement:
// prefer substatement_open over block_open
if (kind != T_LBRACE && tryStatement())
break;
switch (kind) {
case T_LBRACE: turnInto(substatement_open); break;
case T_SEMICOLON: leave(true); break;
case T_RBRACE: leave(true); continue;
} break;
case for_statement:
switch (kind) {
case T_LPAREN: enter(for_statement_paren_open); break;
default: leave(true); continue;
} break;
case for_statement_paren_open:
enter(for_statement_init); continue;
case for_statement_init:
switch (kind) {
case T_SEMICOLON: turnInto(for_statement_condition); break;
case T_LPAREN: enter(condition_paren_open); break;
case T_RPAREN: turnInto(for_statement_expression); continue;
} break;
case for_statement_condition:
switch (kind) {
case T_SEMICOLON: turnInto(for_statement_expression); break;
case T_LPAREN: enter(condition_paren_open); break;
case T_RPAREN: turnInto(for_statement_expression); continue;
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
} break;
case for_statement_expression:
switch (kind) {
case T_RPAREN: leave(); turnInto(substatement); break;
case T_LPAREN: enter(condition_paren_open); break;
} break;
case case_start:
switch (kind) {
case T_COLON: turnInto(case_cont); break;
} break;
case case_cont:
if (kind != T_CASE && kind != T_DEFAULT && tryStatement())
break;
switch (kind) {
case T_RBRACE: leave(); continue;
case T_DEFAULT:
case T_CASE: leave(); continue;
} break;
case substatement_open:
if (tryStatement())
break;
switch (kind) {
case T_RBRACE: leave(true); break;
} break;
case condition_open:
switch (kind) {
case T_RPAREN: turnInto(substatement); break;
case T_LPAREN: enter(condition_paren_open); break;
} break;
case block_open:
if (tryStatement())
break;
case T_RBRACE: leave(true); break;
} break;
// paren nesting
case condition_paren_open:
switch (kind) {
case T_RPAREN: leave(); break;
case T_LPAREN: enter(condition_paren_open); break;
} break;
case qt_like_macro:
switch (kind) {
case T_LPAREN: enter(arglist_open); break;
case T_SEMICOLON: leave(true); break;
default: leave(); continue;
} break;
case label:
switch (kind) {
case T_COLON: leave(); break;
default: leave(); continue; // shouldn't happen
} break;
case multiline_comment_start:
case multiline_comment_cont:
if (kind != T_COMMENT && kind != T_DOXY_COMMENT) {
leave();
continue;
} else if (m_tokenIndex == m_tokens.size() - 1
&& lexerState == Lexer::State_Default) {
leave();
} else if (m_tokenIndex == 0 && m_currentToken.isComment()) {
// to allow enter/leave to update the indentDepth
turnInto(multiline_comment_cont);
}
break;
case cpp_macro_start: {
const int size = m_currentState.size();
int previousMarker = -1;
int previousPreviousMarker = -1;
for (int i = size - 1; i >= 0; --i) {
if (m_currentState.at(i).type == cpp_macro_conditional) {
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
previousPreviousMarker = i;
break;
}
}
}
QStringRef tokenText = currentTokenText();
if (tokenText == QLatin1String("ifdef")
|| tokenText == QLatin1String("if")
|| tokenText == QLatin1String("ifndef")) {
enter(cpp_macro_conditional);
// copy everything right of previousMarker, excluding cpp_macro_conditional
for (int i = previousMarker + 1; i < size; ++i)
m_currentState += m_currentState.at(i);
}
if (previousMarker != -1) {
if (tokenText == QLatin1String("endif")) {
QStack<State>::iterator begin = m_currentState.begin() + previousPreviousMarker + 1;
QStack<State>::iterator end = m_currentState.begin() + previousMarker + 1;
m_currentState.erase(begin, end);
} else if (tokenText == QLatin1String("else")
|| tokenText == QLatin1String("elif")) {
m_currentState.resize(previousMarker + 1);
for (int i = previousPreviousMarker + 1; i < previousMarker; ++i)
m_currentState += m_currentState.at(i);
}
}
turnInto(cpp_macro);
break;
}
case cpp_macro:
case cpp_macro_cont:
break;
default:
qWarning() << "Unhandled state" << m_currentState.top().type;
break;
} // end of state switch
++m_tokenIndex;
}
int topState = m_currentState.top().type;
if (topState != multiline_comment_start
&& topState != multiline_comment_cont
&& (lexerState == Lexer::State_MultiLineComment
|| lexerState == Lexer::State_MultiLineDoxyComment)) {
enter(multiline_comment_start);
}
if (topState == qt_like_macro)
leave(true);
if ((topState == cpp_macro_cont
|| topState == cpp_macro) && !endedJoined)
leave();
if (topState == cpp_macro && endedJoined)
turnInto(cpp_macro_cont);
saveCurrentState(block);
void CodeFormatter::indentFor(const QTextBlock &block, int *indent, int *padding)
{
// qDebug() << "indenting for" << block.blockNumber() + 1;
restoreCurrentState(block.previous());
*indent = m_indentDepth;
*padding = m_paddingDepth;
void CodeFormatter::indentForNewLineAfter(const QTextBlock &block, int *indent, int *padding)
{
restoreCurrentState(block);
*indent = m_indentDepth;
*padding = m_paddingDepth;
int lexerState = loadLexerState(block);
m_tokens.clear();
m_currentLine.clear();
adjustIndent(m_tokens, lexerState, indent, padding);
}
void CodeFormatter::updateStateUntil(const QTextBlock &endBlock)
{
QStack<State> previousState = initialState();
QTextBlock it = endBlock.document()->firstBlock();
// find the first block that needs recalculation
for (; it.isValid() && it != endBlock; it = it.next()) {
BlockData blockData;
if (!loadBlockData(it, &blockData))
if (blockData.m_blockRevision != it.revision())
if (previousState.isEmpty() || blockData.m_beginState.isEmpty()
|| previousState != blockData.m_beginState)
if (loadLexerState(it) == -1)
previousState = blockData.m_endState;
if (it == endBlock)
return;
// update everthing until endBlock
for (; it.isValid() && it != endBlock; it = it.next()) {
recalculateStateAfter(it);
}
// invalidate everything below by marking the state in endBlock as invalid
if (it.isValid()) {
BlockData invalidBlockData;
saveBlockData(&it, invalidBlockData);
}
}
void CodeFormatter::updateLineStateChange(const QTextBlock &block)
{
if (!block.isValid())
return;
BlockData blockData;
if (loadBlockData(block, &blockData) && blockData.m_blockRevision == block.revision())
return;
recalculateStateAfter(block);
// invalidate everything below by marking the next block's state as invalid
QTextBlock next = block.next();
if (!next.isValid())
return;
saveBlockData(&next, BlockData());
}
CodeFormatter::State CodeFormatter::state(int belowTop) const
{
if (belowTop < m_currentState.size())
return m_currentState.at(m_currentState.size() - 1 - belowTop);
else
return State();
}
const QVector<CodeFormatter::State> &CodeFormatter::newStatesThisLine() const
{
return m_newStates;
}
int CodeFormatter::tokenIndex() const
{
return m_tokenIndex;
}
int CodeFormatter::tokenCount() const
return m_tokens.size();
}
const CPlusPlus::Token &CodeFormatter::currentToken() const
{
return m_currentToken;
}
void CodeFormatter::invalidateCache(QTextDocument *document)
if (!document)
return;
BlockData invalidBlockData;
QTextBlock it = document->firstBlock();
for (; it.isValid(); it = it.next()) {
saveBlockData(&it, invalidBlockData);
}
}
void CodeFormatter::enter(int newState)
{
int savedIndentDepth = m_indentDepth;
int savedPaddingDepth = m_paddingDepth;
onEnter(newState, &m_indentDepth, &savedIndentDepth, &m_paddingDepth, &savedPaddingDepth);
State s(newState, savedIndentDepth, savedPaddingDepth);
m_currentState.push(s);
m_newStates.push(s);
}
void CodeFormatter::leave(bool statementDone)
{
QTC_ASSERT(m_currentState.size() > 1, return);
if (m_currentState.top().type == topmost_intro)
return;
if (m_newStates.size() > 0)
m_newStates.pop();
// restore indent depth
State poppedState = m_currentState.pop();
m_indentDepth = poppedState.savedIndentDepth;
m_paddingDepth = poppedState.savedPaddingDepth;
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
int topState = m_currentState.top().type;
// does it suffice to check if token is T_SEMICOLON or T_RBRACE?
// maybe distinction between leave and turnInto?
if (statementDone) {
if (topState == substatement
|| topState == statement_with_condition
|| topState == for_statement
|| topState == switch_statement
|| topState == do_statement) {
leave(true);
} else if (topState == if_statement) {
if (poppedState.type != maybe_else)
enter(maybe_else);
else
leave(true);
} else if (topState == else_clause) {
// leave the else *and* the surrounding if, to prevent another else
leave();
leave(true);
}
}
}
void CodeFormatter::correctIndentation(const QTextBlock &block)
{
const int lexerState = tokenizeBlock(block);
QTC_ASSERT(m_currentState.size() >= 1, return);
adjustIndent(m_tokens, lexerState, &m_indentDepth, &m_paddingDepth);
}
bool CodeFormatter::tryExpression(bool alsoExpression)
int newState = -1;
const int kind = m_currentToken.kind();
switch (kind) {
case T_LPAREN: newState = arglist_open; break;
case T_QUESTION: newState = ternary_op; break;
case T_LBRACE: newState = braceinit_open; break;
case T_EQUAL:
case T_AMPER_EQUAL:
case T_CARET_EQUAL:
case T_SLASH_EQUAL:
case T_EXCLAIM_EQUAL:
case T_GREATER_GREATER_EQUAL:
case T_LESS_LESS_EQUAL:
case T_MINUS_EQUAL:
case T_PERCENT_EQUAL:
case T_PIPE_EQUAL:
case T_PLUS_EQUAL:
case T_STAR_EQUAL:
case T_TILDE_EQUAL:
newState = assign_open;
break;
case T_LESS_LESS:
case T_GREATER_GREATER:
newState = stream_op;
for (int i = m_currentState.size() - 1; i >= 0; --i) {
const int type = m_currentState.at(i).type;
if (type == arglist_open) { // likely a left-shift instead
newState = -1;
break;
}
if (type == topmost_intro
|| type == substatement_open
|| type == defun_open
|| type == namespace_open
|| type == class_open
|| type == brace_list_open) {
break;
}
}
break;
case T_LBRACKET:
newState = lambda_instroducer_or_subscribtion;
break;
}
if (newState != -1) {
if (alsoExpression)
enter(expression);
enter(newState);
return true;
}
return false;
}
bool CodeFormatter::tryDeclaration()
{
const int kind = m_currentToken.kind();
switch (kind) {
case T_Q_ENUMS:
case T_Q_PROPERTY:
case T_Q_FLAGS:
case T_Q_GADGET:
case T_Q_OBJECT:
case T_Q_INTERFACES:
case T_Q_DECLARE_INTERFACE:
case T_Q_PRIVATE_SLOT:
enter(qt_like_macro);
return true;
case T_IDENTIFIER:
if (m_tokenIndex == 0) {
const QStringRef tokenText = currentTokenText();
if (tokenText.startsWith(QLatin1String("Q_"))
|| tokenText.startsWith(QLatin1String("QT_"))
|| tokenText.startsWith(QLatin1String("QML_"))
|| tokenText.startsWith(QLatin1String("QDOC_"))) {
enter(qt_like_macro);
return true;
}
if (m_tokens.size() > 1 && m_tokens.at(1).kind() == T_COLON) {
enter(label);
return true;
}
}
// fallthrough
case T_CHAR:
case T_CHAR16_T:
case T_CHAR32_T:
case T_WCHAR_T:
case T_BOOL:
case T_SHORT:
case T_INT:
case T_LONG:
case T_SIGNED:
case T_UNSIGNED:
case T_FLOAT:
case T_DOUBLE:
case T_VOID:
case T_AUTO:
case T___TYPEOF__:
case T___ATTRIBUTE__:
case T_STATIC:
case T_FRIEND:
case T_CONST:
case T_VOLATILE:
case T_INLINE:
enter(declaration_start);
return true;
case T_TEMPLATE:
enter(template_start);
return true;
case T_NAMESPACE:
enter(namespace_start);
return true;
case T_EXTERN:
enter(extern_start);
return true;
case T_STRUCT:
case T_UNION:
case T_CLASS:
enter(class_start);
return true;
case T_ENUM:
enter(enum_start);
return true;
case T_USING:
enter(using_start);
return true;
case T_PUBLIC:
case T_PRIVATE:
case T_PROTECTED:
case T_Q_SIGNALS:
if (m_currentState.top().type == class_open) {
enter(access_specifier_start);
return true;
}
return false;
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
default:
return false;
}
}
bool CodeFormatter::tryStatement()
{
const int kind = m_currentToken.kind();
if (tryDeclaration())
return true;
switch (kind) {
case T_RETURN:
enter(return_statement);
enter(expression);
return true;
case T_FOR:
enter(for_statement);
return true;
case T_SWITCH:
enter(switch_statement);
return true;
case T_IF:
enter(if_statement);
return true;
case T_WHILE:
case T_Q_FOREACH:
enter(statement_with_condition);
return true;
case T_DO:
enter(do_statement);
enter(substatement);
return true;
case T_CASE:
case T_DEFAULT:
enter(case_start);
return true;
case T_LBRACE:
enter(block_open);
return true;
default:
return false;
}
}
bool CodeFormatter::isBracelessState(int type) const
{
return type == substatement
|| type == if_statement
|| type == else_clause
|| type == statement_with_condition
|| type == for_statement
|| type == do_statement;
}
const Token &CodeFormatter::tokenAt(int idx) const
{
static const Token empty;
if (idx < 0 || idx >= m_tokens.size())
return empty;
else
return m_tokens.at(idx);
}
int CodeFormatter::column(int index) const
{
int col = 0;
if (index > m_currentLine.length())
index = m_currentLine.length();
const QChar tab = QLatin1Char('\t');
for (int i = 0; i < index; i++) {
if (m_currentLine[i] == tab)
col = ((col / m_tabSize) + 1) * m_tabSize;
col++;
}
return col;
}
QStringRef CodeFormatter::currentTokenText() const
{
return m_currentLine.midRef(m_currentToken.begin(), m_currentToken.length());
}