CppEditor: Support try/catch in built-in indenter

Fixes: QTCREATORBUG-29452
Change-Id: Icf3d86a6080aeb741436800ae77af7c8bec3155c
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Christian Kandeler
2024-03-14 17:49:56 +01:00
parent e0cf25fb55
commit 812326eadc
3 changed files with 55 additions and 0 deletions

View File

@@ -284,6 +284,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
&& m_currentState.at(m_currentState.size() - 2).type == declaration_start) { && m_currentState.at(m_currentState.size() - 2).type == declaration_start) {
leave(); leave();
leave(); leave();
} else if (m_currentState.top().type == catch_statement) {
turnInto(substatement);
} }
break; break;
default: tryExpression(); break; default: tryExpression(); break;
@@ -414,6 +416,11 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
default: leave(true); continue; default: leave(true); continue;
} break; } break;
case catch_statement:
switch (kind) {
case T_LPAREN: enter(arglist_open); break;
} break;
case for_statement_paren_open: case for_statement_paren_open:
enter(for_statement_init); continue; enter(for_statement_init); continue;
@@ -965,6 +972,12 @@ bool CodeFormatter::tryStatement()
enter(do_statement); enter(do_statement);
enter(substatement); enter(substatement);
return true; return true;
case T_TRY:
enter(substatement);
return true;
case T_CATCH:
enter(catch_statement);
return true;
case T_CASE: case T_CASE:
case T_DEFAULT: case T_DEFAULT:
enter(case_start); enter(case_start);

View File

@@ -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_condition, // The condition part of the for statement
for_statement_expression, // The expression part of the for statement for_statement_expression, // The expression part of the for statement
catch_statement,
switch_statement, // After 'switch' token switch_statement, // After 'switch' token
case_start, // after a 'case' or 'default' token case_start, // after a 'case' or 'default' token
case_cont, // after the colon in a case/default case_cont, // after the colon in a case/default

View File

@@ -109,6 +109,7 @@ private Q_SLOTS:
void structuredBinding(); void structuredBinding();
void subscriptOperatorInFunctionCall(); void subscriptOperatorInFunctionCall();
void statementMacros(); void statementMacros();
void tryCatchClause();
}; };
struct Line { struct Line {
@@ -2231,6 +2232,45 @@ void tst_CodeFormatter::statementMacros()
checkIndent(data, settings); checkIndent(data, settings);
} }
void tst_CodeFormatter::tryCatchClause()
{
CppCodeStyleSettings settings;
settings.indentFunctionBody = true;
settings.indentFunctionBraces = false;
settings.indentBlockBody = false;
settings.indentBlockBraces = true;
QList<Line> 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) QTEST_GUILESS_MAIN(tst_CodeFormatter)
#include "tst_codeformatter.moc" #include "tst_codeformatter.moc"