From feefe3585f94c2a9fad1f0445f4e95fe64d7b7eb Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 29 Jan 2021 17:00:06 +0100 Subject: [PATCH] CppEditor: Make "Complete switch statement" quickfix more visible Do not require the cursor to be on the "switch" keyword, but allow it to be anywhere within the switch construct. Fixes: QTCREATORBUG-5379 Change-Id: Ic25504237b3ef12958f339d7176dfeeac6bd3466 Reviewed-by: Christian Stenger --- src/plugins/cppeditor/cppquickfix_test.cpp | 26 ++++++++++++++++++++++ src/plugins/cppeditor/cppquickfixes.cpp | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 91f6df74b48..e2bc42cf4ce 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -409,6 +409,32 @@ void CppEditorPlugin::test_quickfix_data() "}\n" ); + // Same as above with the cursor somewhere in the body. + QTest::newRow("CompleteSwitchCaseStatement_basic1_enum class, cursor in the body") + << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _( + "enum class EnumType { V1, V2 };\n" + "\n" + "void f()\n" + "{\n" + " EnumType t;\n" + " switch (t) {\n" + " @}\n" + "}\n" + ) << _( + "enum class EnumType { V1, V2 };\n" + "\n" + "void f()\n" + "{\n" + " EnumType t;\n" + " switch (t) {\n" + " case EnumType::V1:\n" + " break;\n" + " case EnumType::V2:\n" + " break;\n" + " }\n" + "}\n" + ); + // Checks: All enum values are added as case statements for a blank switch when // the variable is declared alongside the enum definition. QTest::newRow("CompleteSwitchCaseStatement_basic1_enum_with_declaration") diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index db6f28aa987..84bceab08b7 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -2833,7 +2833,7 @@ void CompleteSwitchCaseStatement::match(const CppQuickFixInterface &interface, AST *ast = path.at(depth); SwitchStatementAST *switchStatement = ast->asSwitchStatement(); if (switchStatement) { - if (!interface.isCursorOn(switchStatement->switch_token) || !switchStatement->statement) + if (!switchStatement->statement) return; CompoundStatementAST *compoundStatement = switchStatement->statement->asCompoundStatement(); if (!compoundStatement) // we ignore pathologic case "switch (t) case A: ;"