forked from qt-creator/qt-creator
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:
@@ -6729,25 +6729,46 @@ 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
|
||||||
|
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 typeOfExpression;
|
||||||
typeOfExpression.init(semanticInfo().doc, snapshot(),
|
typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings());
|
||||||
context().bindings());
|
|
||||||
typeOfExpression.setExpandTemplates(true);
|
typeOfExpression.setExpandTemplates(true);
|
||||||
Scope *scope = file->scopeAt(m_ast->firstToken());
|
Scope * const scope = m_file->scopeAt(m_ast->firstToken());
|
||||||
const QList<LookupItem> result = typeOfExpression(file->textOf(m_ast).toUtf8(),
|
const QList<LookupItem> result = typeOfExpression(m_file->textOf(m_ast).toUtf8(),
|
||||||
scope, TypeOfExpression::Preprocess);
|
scope, TypeOfExpression::Preprocess);
|
||||||
|
if (result.isEmpty())
|
||||||
|
return {};
|
||||||
|
|
||||||
if (!result.isEmpty()) {
|
|
||||||
SubstitutionEnvironment env;
|
SubstitutionEnvironment env;
|
||||||
env.setContext(context());
|
env.setContext(context());
|
||||||
env.switchScope(result.first().scope());
|
env.switchScope(result.first().scope());
|
||||||
@@ -6760,9 +6781,12 @@ public:
|
|||||||
Control *control = context().bindings()->control().data();
|
Control *control = context().bindings()->control().data();
|
||||||
FullySpecifiedType type = rewriteType(result.first().type(), &env, control);
|
FullySpecifiedType type = rewriteType(result.first().type(), &env, control);
|
||||||
|
|
||||||
Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview();
|
return m_oo.prettyType(type, m_name);
|
||||||
QString originalName = oo.prettyName(m_name);
|
}
|
||||||
QString newName = originalName;
|
|
||||||
|
QString constructVarName() const
|
||||||
|
{
|
||||||
|
QString newName = m_originalName;
|
||||||
if (newName.startsWith(QLatin1String("get"), Qt::CaseInsensitive)
|
if (newName.startsWith(QLatin1String("get"), Qt::CaseInsensitive)
|
||||||
&& newName.length() > 3
|
&& newName.length() > 3
|
||||||
&& newName.at(3).isUpper()) {
|
&& newName.at(3).isUpper()) {
|
||||||
@@ -6777,30 +6801,15 @@ public:
|
|||||||
newName.replace(0, 1, newName.at(0).toUpper());
|
newName.replace(0, 1, newName.at(0).toUpper());
|
||||||
newName.prepend(QLatin1String("local"));
|
newName.prepend(QLatin1String("local"));
|
||||||
}
|
}
|
||||||
|
return newName;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
Reference in New Issue
Block a user