CppQuickFixes: Add function getDeclaration for AddLocalDeclaration

Change-Id: Ie2d5c5de6b871a855b95efe7c633b28d6ed5c512
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-06-16 10:39:21 +02:00
parent 677a8fe34a
commit 706dc654b9

View File

@@ -1614,6 +1614,23 @@ public:
} }
void perform() override 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()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
@@ -1622,9 +1639,9 @@ public:
typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings()); typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings());
Scope *scope = currentFile->scopeAt(binaryAST->firstToken()); Scope *scope = currentFile->scopeAt(binaryAST->firstToken());
const QList<LookupItem> result = const QList<LookupItem> result =
typeOfExpression(currentFile->textOf(binaryAST->right_expression).toUtf8(), typeOfExpression(currentFile->textOf(binaryAST->right_expression).toUtf8(),
scope, scope,
TypeOfExpression::Preprocess); TypeOfExpression::Preprocess);
if (!result.isEmpty()) { if (!result.isEmpty()) {
SubstitutionEnvironment env; SubstitutionEnvironment env;
@@ -1640,19 +1657,13 @@ public:
FullySpecifiedType tn = rewriteType(result.first().type(), &env, control); FullySpecifiedType tn = rewriteType(result.first().type(), &env, control);
Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview(); Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview();
QString ty = oo.prettyType(tn, simpleNameAST->name); QString declaration = oo.prettyType(tn, simpleNameAST->name);
if (!ty.isEmpty()) { return declaration;
ChangeSet changes;
changes.replace(currentFile->startOf(binaryAST),
currentFile->endOf(simpleNameAST),
ty);
currentFile->setChangeSet(changes);
currentFile->apply();
}
} }
return {};
} }
private:
const BinaryExpressionAST *binaryAST; const BinaryExpressionAST *binaryAST;
const SimpleNameAST *simpleNameAST; const SimpleNameAST *simpleNameAST;
}; };