CppEditor: Refactor AddBracesToControlStatement quickfix

Preparation for bracifying if-else chains. No functional changes.

Change-Id: Idc101131c02aac88598a6de1f8f511f89c958346
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
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-07 10:50:20 +01:00
parent 8295f11576
commit 8c5c849b71

View File

@@ -808,9 +808,10 @@ int tokenToInsertOpeningBraceAfter(const Statement *statement)
template<typename Statement> class AddBracesToControlStatementOp : public CppQuickFixOperation template<typename Statement> class AddBracesToControlStatementOp : public CppQuickFixOperation
{ {
public: public:
AddBracesToControlStatementOp(const CppQuickFixInterface &interface, const Statement *statement) AddBracesToControlStatementOp(const CppQuickFixInterface &interface,
const QList<Statement *> &statements)
: CppQuickFixOperation(interface, 0) : CppQuickFixOperation(interface, 0)
, _statement(statement) , m_statements(statements)
{ {
setDescription(Tr::tr("Add Curly Braces")); setDescription(Tr::tr("Add Curly Braces"));
} }
@@ -821,26 +822,24 @@ public:
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath()); CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
for (Statement * const statement : m_statements) {
const int start = currentFile->endOf(tokenToInsertOpeningBraceAfter(_statement)); const int start = currentFile->endOf(tokenToInsertOpeningBraceAfter(statement));
changes.insert(start, QLatin1String(" {")); changes.insert(start, QLatin1String(" {"));
if constexpr (std::is_same_v<Statement, DoStatementAST>) { if constexpr (std::is_same_v<Statement, DoStatementAST>) {
const int end = currentFile->startOf(_statement->while_token); const int end = currentFile->startOf(statement->while_token);
changes.insert(end, QLatin1String("} ")); changes.insert(end, QLatin1String("} "));
} else { } else {
const int end = currentFile->endOf(_statement->statement->lastToken() - 1); const int end = currentFile->endOf(statement->statement->lastToken() - 1);
changes.insert(end, QLatin1String("\n}")); changes.insert(end, QLatin1String("\n}"));
} }
}
// TODO: For if statements, also bracify all else cases.
// Also check all else cases in the factory.
currentFile->setChangeSet(changes); currentFile->setChangeSet(changes);
currentFile->apply(); currentFile->apply();
} }
private: private:
const Statement * const _statement; const QList<Statement *> m_statements;
}; };
} // anonymous namespace } // anonymous namespace
@@ -854,7 +853,7 @@ bool checkControlStatementsHelper(const CppQuickFixInterface &interface, QuickFi
if (interface.isCursorOn(triggerToken(statement)) && statement->statement if (interface.isCursorOn(triggerToken(statement)) && statement->statement
&& !statement->statement->asCompoundStatement()) { && !statement->statement->asCompoundStatement()) {
result << new AddBracesToControlStatementOp(interface, statement); result << new AddBracesToControlStatementOp(interface, QList{statement});
} }
return true; return true;
} }
@@ -865,7 +864,8 @@ void checkControlStatements(const CppQuickFixInterface &interface, QuickFixOpera
(... || checkControlStatementsHelper<Statements>(interface, result)); (... || checkControlStatementsHelper<Statements>(interface, result));
} }
void AddBracesToControlStatement::doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) void AddBracesToControlStatement::doMatch(const CppQuickFixInterface &interface,
QuickFixOperations &result)
{ {
if (interface.path().isEmpty()) if (interface.path().isEmpty())
return; return;