Newer
Older
}
void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedIndentDepth) const
{
const State &parentState = state();
const Token &tk = currentToken();
const int tokenPosition = column(tk.begin());
const bool firstToken = (tokenIndex() == 0);
const bool lastToken = (tokenIndex() == tokenCount() - 1);
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
switch (newState) {
case namespace_start:
if (firstToken)
*savedIndentDepth = tokenPosition;
*indentDepth = tokenPosition;
break;
case enum_start:
case class_start:
if (firstToken)
*savedIndentDepth = tokenPosition;
*indentDepth = tokenPosition + 2*m_indentSize;
break;
case template_param:
if (!lastToken)
*indentDepth = tokenPosition + tk.length();
else
*indentDepth += 2*m_indentSize;
break;
case statement_with_condition:
case for_statement:
case switch_statement:
case if_statement:
case return_statement:
if (firstToken)
*savedIndentDepth = tokenPosition;
*indentDepth = *savedIndentDepth + 2*m_indentSize;
break;
case declaration_start:
if (firstToken)
*savedIndentDepth = tokenPosition;
// continuation indent in function bodies only, to not indent
// after the return type in "void\nfoo() {}"
for (int i = 0; state(i).type != topmost_intro; ++i) {
if (state(i).type == defun_open) {
*indentDepth = *savedIndentDepth + 2*m_indentSize;
break;
}
}
break;
case arglist_open:
case condition_paren_open:
if (!lastToken)
*indentDepth = tokenPosition + 1;
else
*indentDepth += m_indentSize;
break;
case ternary_op:
if (!lastToken)
*indentDepth = tokenPosition + tk.length() + 1;
else
*indentDepth += m_indentSize;
break;
case stream_op:
*indentDepth = tokenPosition + tk.length() + 1;
break;
case stream_op_cont:
if (firstToken)
*savedIndentDepth = *indentDepth = tokenPosition + tk.length() + 1;
break;
// undo the continuation indent of the parent
*savedIndentDepth = parentState.savedIndentDepth;
*indentDepth = tokenPosition;
*indentDepth = *savedIndentDepth + m_indentSize - 2; // they'll get another 2 from member_init
break;
case member_init:
*indentDepth = *savedIndentDepth + 2; // savedIndentDepth is the position of ':'
break;
case member_init_paren_open:
*indentDepth = *savedIndentDepth + m_indentSize;
break;
case case_cont:
*indentDepth += m_indentSize;
break;
case class_open:
case enum_open:
case defun_open: {
// undo the continuation indent of the parent
*savedIndentDepth = parentState.savedIndentDepth;
bool followedByData = (!lastToken && !tokenAt(tokenIndex() + 1).isComment());
if (firstToken || followedByData)
*savedIndentDepth = tokenPosition;
*indentDepth = *savedIndentDepth;
if (followedByData) {
*indentDepth = column(tokenAt(tokenIndex() + 1).begin());
} else if (m_indentDeclarationMembers) {
case substatement_open:
if (firstToken) {
*savedIndentDepth = tokenPosition;
*indentDepth = *savedIndentDepth;
} else if (m_indentSubstatementBraces && !m_indentSubstatementStatements) {
// ### The preceding check is quite arbitrary.
// It actually needs another flag to determine whether the closing curly
// should be indented or not
*indentDepth = *savedIndentDepth += m_indentSize;
}
if (m_indentSubstatementStatements) {
if (parentState.type != switch_statement)
*indentDepth += m_indentSize;
}
break;
case brace_list_open:
if (parentState.type != initializer)
*indentDepth = parentState.savedIndentDepth + m_indentSize;
else if (lastToken) {
*savedIndentDepth = state(1).savedIndentDepth;
*indentDepth = *savedIndentDepth + m_indentSize;
}
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
break;
case block_open:
if (parentState.type != case_cont)
*indentDepth += m_indentSize;
break;
case condition_open:
// undo the continuation indent of the parent
*indentDepth = parentState.savedIndentDepth;
*savedIndentDepth = *indentDepth;
// fixed extra indent when continuing 'if (', but not for 'else if ('
if (tokenPosition <= *indentDepth + m_indentSize)
*indentDepth += 2*m_indentSize;
else
*indentDepth = tokenPosition + 1;
break;
case substatement:
// undo the continuation indent of the parent
*indentDepth = parentState.savedIndentDepth;
*savedIndentDepth = *indentDepth;
break;
case maybe_else: {
// set indent to outermost braceless savedIndent
int outermostBraceless = 0;
while (isBracelessState(state(outermostBraceless).type))
++outermostBraceless;
*indentDepth = state(outermostBraceless - 1).savedIndentDepth;
// this is where the else should go, if one appears - aligned to if_statement
*savedIndentDepth = state().savedIndentDepth;
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
} break;
case for_statement_paren_open:
*indentDepth = tokenPosition + 1;
break;
case multiline_comment_start:
*indentDepth = tokenPosition + 2;
break;
case multiline_comment_cont:
*indentDepth = tokenPosition;
break;
case cpp_macro:
case cpp_macro_cont:
*indentDepth = m_indentSize;
break;
}
}
void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, int lexerState, int *indentDepth) const
{
State topState = state();
State previousState = state(1);
const bool topWasMaybeElse = (topState.type == maybe_else);
if (topWasMaybeElse) {
int outermostBraceless = 1;
while (state(outermostBraceless).type != invalid && isBracelessState(state(outermostBraceless).type))
++outermostBraceless;
topState = state(outermostBraceless);
previousState = state(outermostBraceless + 1);
}
// adjusting the indentDepth here instead of in enter() gives 'else if' the correct indentation
// ### could be moved?
if (topState.type == substatement)
*indentDepth += m_indentSize;
// keep user-adjusted indent in multiline comments
if (topState.type == multiline_comment_start
|| topState.type == multiline_comment_cont) {
if (!tokens.isEmpty()) {
*indentDepth = tokens.at(0).begin();
return;
}
}
const int kind = tokenAt(0).kind();
switch (kind) {
case T_POUND: *indentDepth = 0; break;
case T_COLON:
// ### ok for constructor initializer lists - what about ? and bitfields?
if (topState.type == expression && previousState.type == declaration_start) {
*indentDepth = previousState.savedIndentDepth + m_indentSize;
} else if (topState.type == ternary_op) {
*indentDepth -= 2;
}
break;
case T_LBRACE: {
if (topState.type == case_cont) {
*indentDepth = topState.savedIndentDepth;
// function definition - argument list is expression state
} else if (topState.type == expression && previousState.type == declaration_start) {
*indentDepth = previousState.savedIndentDepth;
if (m_indentDeclarationBraces)
*indentDepth += m_indentSize;
} else if (topState.type == class_start) {
*indentDepth = topState.savedIndentDepth;
if (m_indentDeclarationBraces)
*indentDepth += m_indentSize;
} else if (topState.type == substatement) {
*indentDepth = topState.savedIndentDepth;
if (m_indentSubstatementBraces)
*indentDepth += m_indentSize;
} else if (topState.type != defun_open
&& topState.type != substatement_open
&& !topWasMaybeElse) {
*indentDepth = topState.savedIndentDepth;
}
break;
}
case T_RBRACE: {
if (topState.type == block_open && previousState.type == case_cont) {
*indentDepth = previousState.savedIndentDepth;
break;
}
for (int i = 0; state(i).type != topmost_intro; ++i) {
const int type = state(i).type;
if (type == defun_open
|| type == substatement_open
|| type == class_open
|| type == brace_list_open
|| type == namespace_open
|| type == block_open
|| type == enum_open) {
*indentDepth = state(i).savedIndentDepth;
break;
}
}
break;
}
// Disabled for now, see QTCREATORBUG-1825. It makes extending if conditions
// awkward: inserting a newline just before the ) shouldn't align to 'if'.
//case T_RPAREN:
// if (topState.type == condition_open) {
// *indentDepth = previousState.savedIndentDepth;
// }
// break;
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
case T_DEFAULT:
case T_CASE:
for (int i = 0; state(i).type != topmost_intro; ++i) {
const int type = state(i).type;
if (type == switch_statement || type == case_cont) {
*indentDepth = state(i).savedIndentDepth;
break;
} else if (type == topmost_intro) {
break;
}
}
break;
case T_PUBLIC:
case T_PRIVATE:
case T_PROTECTED:
case T_Q_SIGNALS:
if (topState.type == class_open) {
if (tokenAt(1).is(T_COLON) || tokenAt(2).is(T_COLON))
*indentDepth = topState.savedIndentDepth;
}
break;
case T_ELSE:
if (topWasMaybeElse)
*indentDepth = state().savedIndentDepth; // topSavedIndent is actually the previous
break;
case T_LESS_LESS:
case T_GREATER_GREATER:
if (topState.type == stream_op || topState.type == stream_op_cont)
*indentDepth -= 3; // to align << with <<
break;
case T_COMMENT:
case T_DOXY_COMMENT:
case T_CPP_COMMENT:
case T_CPP_DOXY_COMMENT:
// unindent the last line of a comment
if ((topState.type == multiline_comment_cont
|| topState.type == multiline_comment_start)
&& (kind == T_COMMENT || kind == T_DOXY_COMMENT)
&& (lexerState == Lexer::State_Default
|| tokens.size() != 1)) {
*indentDepth -= m_indentSize;
}
break;
}
}