From 5b3aece38738ff61bd37abaa255e55444218e772 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 7 Jul 2020 15:43:42 +0200 Subject: [PATCH] CppEditor: Make "Add curly braces" quickfix more robust Anchor the opening brace at the closing parenthesis, rather than at the statement. This way, we won't get troubled by macro weirdness in the statement part. Fixes: QTCREATORBUG-13921 Change-Id: I05af24d1642e6b62c78bb2f47a1ef0b1fea326d0 Reviewed-by: Christian Stenger --- src/plugins/cppeditor/cppeditorplugin.h | 2 ++ src/plugins/cppeditor/cppquickfix_test.cpp | 25 ++++++++++++++++++++++ src/plugins/cppeditor/cppquickfixes.cpp | 13 +++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index 83723f329ab..b6952d0b692 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -197,6 +197,8 @@ private slots: void test_quickfix_ExtractLiteralAsParameter_memberFunction_separateFiles(); void test_quickfix_ExtractLiteralAsParameter_notTriggeringForInvalidCode(); + void test_quickfix_addCurlyBraces(); + void test_quickfix_InsertVirtualMethods_data(); void test_quickfix_InsertVirtualMethods(); void test_quickfix_InsertVirtualMethods_implementationFile(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index cf66662e09c..7eff28577b5 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -5417,6 +5417,31 @@ void CppEditorPlugin::test_quickfix_ExtractLiteralAsParameter_notTriggeringForIn QuickFixOperationTest(testDocuments, &factory); } +void CppEditorPlugin::test_quickfix_addCurlyBraces() +{ + QList testDocuments; + const QByteArray original = R"delim( +void MyObject::f() +{ + @if (true) + emit mySig(); +} +)delim"; + const QByteArray expected = R"delim( +void MyObject::f() +{ + if (true) { + emit mySig(); + } +} +)delim"; + + testDocuments << QuickFixTestDocument::create("file.cpp", original, expected); + AddBracesToIf factory; + QuickFixOperationTest(testDocuments, &factory); + +} + void CppEditorPlugin::test_quickfix_ConvertQt4Connect_connectOutOfClass() { QByteArray prefix = diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 25592086aef..2f40c8c76bd 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -691,7 +691,8 @@ namespace { class AddBracesToIfOp: public CppQuickFixOperation { public: - AddBracesToIfOp(const CppQuickFixInterface &interface, int priority, StatementAST *statement) + AddBracesToIfOp(const CppQuickFixInterface &interface, int priority, + const IfStatementAST *statement) : CppQuickFixOperation(interface, priority) , _statement(statement) { @@ -705,10 +706,10 @@ public: ChangeSet changes; - const int start = currentFile->endOf(_statement->firstToken() - 1); + const int start = currentFile->endOf(_statement->rparen_token); changes.insert(start, QLatin1String(" {")); - const int end = currentFile->endOf(_statement->lastToken() - 1); + const int end = currentFile->endOf(_statement->statement->lastToken() - 1); changes.insert(end, QLatin1String("\n}")); currentFile->setChangeSet(changes); @@ -717,7 +718,7 @@ public: } private: - StatementAST *_statement; + const IfStatementAST * const _statement; }; } // anonymous namespace @@ -731,7 +732,7 @@ void AddBracesToIf::match(const CppQuickFixInterface &interface, QuickFixOperati IfStatementAST *ifStatement = path.at(index)->asIfStatement(); if (ifStatement && interface.isCursorOn(ifStatement->if_token) && ifStatement->statement && !ifStatement->statement->asCompoundStatement()) { - result << new AddBracesToIfOp(interface, index, ifStatement->statement); + result << new AddBracesToIfOp(interface, index, ifStatement); return; } @@ -742,7 +743,7 @@ void AddBracesToIf::match(const CppQuickFixInterface &interface, QuickFixOperati if (ifStatement && ifStatement->statement && interface.isCursorOn(ifStatement->statement) && !ifStatement->statement->asCompoundStatement()) { - result << new AddBracesToIfOp(interface, index, ifStatement->statement); + result << new AddBracesToIfOp(interface, index, ifStatement); return; } }