From 706dc654b996f7fa35fddbed40e6b555cc3cb1b4 Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Thu, 16 Jun 2022 10:39:21 +0200 Subject: [PATCH] CppQuickFixes: Add function getDeclaration for AddLocalDeclaration Change-Id: Ie2d5c5de6b871a855b95efe7c633b28d6ed5c512 Reviewed-by: Christian Kandeler --- src/plugins/cppeditor/cppquickfixes.cpp | 37 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index b63d8f115f9..50170e82767 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -1614,6 +1614,23 @@ public: } void perform() override + { + CppRefactoringChanges refactoring(snapshot()); + CppRefactoringFilePtr currentFile = refactoring.file(filePath()); + QString declaration = getDeclaration(); + + if (!declaration.isEmpty()) { + ChangeSet changes; + changes.replace(currentFile->startOf(binaryAST), + currentFile->endOf(simpleNameAST), + declaration); + currentFile->setChangeSet(changes); + currentFile->apply(); + } + } + +private: + QString getDeclaration() { CppRefactoringChanges refactoring(snapshot()); CppRefactoringFilePtr currentFile = refactoring.file(filePath()); @@ -1622,9 +1639,9 @@ public: typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings()); Scope *scope = currentFile->scopeAt(binaryAST->firstToken()); const QList result = - typeOfExpression(currentFile->textOf(binaryAST->right_expression).toUtf8(), - scope, - TypeOfExpression::Preprocess); + typeOfExpression(currentFile->textOf(binaryAST->right_expression).toUtf8(), + scope, + TypeOfExpression::Preprocess); if (!result.isEmpty()) { SubstitutionEnvironment env; @@ -1640,19 +1657,13 @@ public: FullySpecifiedType tn = rewriteType(result.first().type(), &env, control); Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview(); - QString ty = oo.prettyType(tn, simpleNameAST->name); - if (!ty.isEmpty()) { - ChangeSet changes; - changes.replace(currentFile->startOf(binaryAST), - currentFile->endOf(simpleNameAST), - ty); - currentFile->setChangeSet(changes); - currentFile->apply(); - } + QString declaration = oo.prettyType(tn, simpleNameAST->name); + return declaration; } + + return {}; } -private: const BinaryExpressionAST *binaryAST; const SimpleNameAST *simpleNameAST; };