diff --git a/src/plugins/cppeditor/cppcodeformatter.cpp b/src/plugins/cppeditor/cppcodeformatter.cpp index 447a5a04871..85c841e2c72 100644 --- a/src/plugins/cppeditor/cppcodeformatter.cpp +++ b/src/plugins/cppeditor/cppcodeformatter.cpp @@ -284,6 +284,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) && m_currentState.at(m_currentState.size() - 2).type == declaration_start) { leave(); leave(); + } else if (m_currentState.top().type == catch_statement) { + turnInto(substatement); } break; default: tryExpression(); break; @@ -414,6 +416,11 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) default: leave(true); continue; } break; + case catch_statement: + switch (kind) { + case T_LPAREN: enter(arglist_open); break; + } break; + case for_statement_paren_open: enter(for_statement_init); continue; @@ -965,6 +972,12 @@ bool CodeFormatter::tryStatement() enter(do_statement); enter(substatement); return true; + case T_TRY: + enter(substatement); + return true; + case T_CATCH: + enter(catch_statement); + return true; case T_CASE: case T_DEFAULT: enter(case_start); diff --git a/src/plugins/cppeditor/cppcodeformatter.h b/src/plugins/cppeditor/cppcodeformatter.h index 93d32faf263..f3d69422ee0 100644 --- a/src/plugins/cppeditor/cppcodeformatter.h +++ b/src/plugins/cppeditor/cppcodeformatter.h @@ -119,6 +119,8 @@ public: // must be public to make Q_GADGET introspection work for_statement_condition, // The condition part of the for statement for_statement_expression, // The expression part of the for statement + catch_statement, + switch_statement, // After 'switch' token case_start, // after a 'case' or 'default' token case_cont, // after the colon in a case/default diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp index 29ecbb58ed9..12c3c528526 100644 --- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp +++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp @@ -109,6 +109,7 @@ private Q_SLOTS: void structuredBinding(); void subscriptOperatorInFunctionCall(); void statementMacros(); + void tryCatchClause(); }; struct Line { @@ -2231,6 +2232,45 @@ void tst_CodeFormatter::statementMacros() checkIndent(data, settings); } +void tst_CodeFormatter::tryCatchClause() +{ + CppCodeStyleSettings settings; + settings.indentFunctionBody = true; + settings.indentFunctionBraces = false; + settings.indentBlockBody = false; + settings.indentBlockBraces = true; + QList data; + data << Line("int main()") + << Line("{") + << Line(" try") + << Line(" {") + << Line(" throw;") + << Line(" }") + << Line(" catch (const E &e)") + << Line(" {") + << Line(" handle(e);") + << Line(" }") + << Line(" catch (...)") + << Line(" {") + << Line(" handle();") + << Line(" }") + << Line("}"); + checkIndent(data, settings); + + data.clear(); + data << Line("int main()") + << Line("{") + << Line(" try {") + << Line(" throw;") + << Line(" } catch (const E &e) {") + << Line(" handle(e);") + << Line(" } catch (...) {") + << Line(" handle();") + << Line(" }") + << Line("}"); + checkIndent(data); +} + QTEST_GUILESS_MAIN(tst_CodeFormatter) #include "tst_codeformatter.moc"