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