CppEditor: Refactor "assign to local" quickfix

Mostly to separate type and name deduction. No functional changes.

Change-Id: I87858f06bec73fbc59a0359769bd37038176b3ad
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2022-05-16 10:55:07 +02:00
parent 0d733e68b5
commit 8c8e9e55b1

View File

@@ -6729,25 +6729,46 @@ public:
, m_insertPos(insertPos)
, m_ast(ast)
, m_name(name)
, m_oo(CppCodeStyleSettings::currentProjectCodeStyleOverview())
, m_originalName(m_oo.prettyName(m_name))
, m_file(CppRefactoringChanges(snapshot()).file(filePath()))
{
setDescription(QApplication::translate("CppEditor::QuickFix", "Assign to Local Variable"));
}
private:
void perform() override
{
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr file = refactoring.file(filePath());
QString type = deduceType();
if (type.isEmpty())
return;
const int origNameLength = m_originalName.length();
const QString varName = constructVarName();
const QString insertString = type.replace(type.length() - origNameLength, origNameLength,
varName + QLatin1String(" = "));
ChangeSet changes;
changes.insert(m_insertPos, insertString);
m_file->setChangeSet(changes);
m_file->apply();
// Determine return type and new variable name
// move cursor to new variable name
QTextCursor c = m_file->cursor();
c.setPosition(m_insertPos + insertString.length() - varName.length() - 3);
c.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
editor()->setTextCursor(c);
}
QString deduceType() const
{
TypeOfExpression typeOfExpression;
typeOfExpression.init(semanticInfo().doc, snapshot(),
context().bindings());
typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings());
typeOfExpression.setExpandTemplates(true);
Scope *scope = file->scopeAt(m_ast->firstToken());
const QList<LookupItem> result = typeOfExpression(file->textOf(m_ast).toUtf8(),
Scope * const scope = m_file->scopeAt(m_ast->firstToken());
const QList<LookupItem> result = typeOfExpression(m_file->textOf(m_ast).toUtf8(),
scope, TypeOfExpression::Preprocess);
if (result.isEmpty())
return {};
if (!result.isEmpty()) {
SubstitutionEnvironment env;
env.setContext(context());
env.switchScope(result.first().scope());
@@ -6760,9 +6781,12 @@ public:
Control *control = context().bindings()->control().data();
FullySpecifiedType type = rewriteType(result.first().type(), &env, control);
Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview();
QString originalName = oo.prettyName(m_name);
QString newName = originalName;
return m_oo.prettyType(type, m_name);
}
QString constructVarName() const
{
QString newName = m_originalName;
if (newName.startsWith(QLatin1String("get"), Qt::CaseInsensitive)
&& newName.length() > 3
&& newName.at(3).isUpper()) {
@@ -6777,30 +6801,15 @@ public:
newName.replace(0, 1, newName.at(0).toUpper());
newName.prepend(QLatin1String("local"));
}
const int nameLength = originalName.length();
QString tempType = oo.prettyType(type, m_name);
const QString insertString = tempType.replace(
tempType.length() - nameLength, nameLength, newName + QLatin1String(" = "));
if (!tempType.isEmpty()) {
ChangeSet changes;
changes.insert(m_insertPos, insertString);
file->setChangeSet(changes);
file->apply();
// move cursor to new variable name
QTextCursor c = file->cursor();
c.setPosition(m_insertPos + insertString.length() - newName.length() - 3);
c.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
editor()->setTextCursor(c);
}
}
return newName;
}
private:
const int m_insertPos;
const AST *m_ast;
const Name *m_name;
const AST * const m_ast;
const Name * const m_name;
const Overview m_oo;
const QString m_originalName;
const CppRefactoringFilePtr m_file;
};
} // anonymous namespace