CppEditor: Make more use of CppQuickFixInterface::currentFile()

As in 5cc44b8cab, this time with the non-
trivial cases.

Change-Id: I48c3f27c83903457473afa6175c8522d2ff23f96
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-28 13:29:31 +02:00
parent 0e301004b8
commit 0fffe1058d
6 changed files with 31 additions and 35 deletions

View File

@@ -260,15 +260,14 @@ static void extractNames(const CppRefactoringFilePtr &file,
class GetterSetterRefactoringHelper class GetterSetterRefactoringHelper
{ {
public: public:
GetterSetterRefactoringHelper(CppQuickFixOperation *operation, GetterSetterRefactoringHelper(CppQuickFixOperation *operation, Class *clazz)
const FilePath &filePath,
Class *clazz)
: m_operation(operation) : m_operation(operation)
, m_changes(m_operation->snapshot()) , m_changes(m_operation->snapshot())
, m_locator(m_changes) , m_locator(m_changes)
, m_headerFile(m_changes.cppFile(filePath)) , m_headerFile(operation->currentFile())
, m_sourceFile([&] { , m_sourceFile([&] {
FilePath cppFilePath = correspondingHeaderOrSource(filePath, &m_isHeaderHeaderFile); FilePath cppFilePath = correspondingHeaderOrSource(m_headerFile->filePath(),
&m_isHeaderHeaderFile);
if (!m_isHeaderHeaderFile || !cppFilePath.exists()) { if (!m_isHeaderHeaderFile || !cppFilePath.exists()) {
// there is no "source" file // there is no "source" file
return m_headerFile; return m_headerFile;
@@ -1271,11 +1270,10 @@ private:
const ClassSpecifierAST *m_classAST; const ClassSpecifierAST *m_classAST;
InsertionPointLocator::AccessSpec m_accessSpec; InsertionPointLocator::AccessSpec m_accessSpec;
GenerateConstructorRefactoringHelper(CppQuickFixOperation *operation, GenerateConstructorRefactoringHelper(CppQuickFixOperation *operation,
const FilePath &filePath,
Class *clazz, Class *clazz,
const ClassSpecifierAST *classAST, const ClassSpecifierAST *classAST,
InsertionPointLocator::AccessSpec accessSpec) InsertionPointLocator::AccessSpec accessSpec)
: GetterSetterRefactoringHelper(operation, filePath, clazz) : GetterSetterRefactoringHelper(operation, clazz)
, m_classAST(classAST) , m_classAST(classAST)
, m_accessSpec(accessSpec) , m_accessSpec(accessSpec)
{} {}
@@ -1416,7 +1414,6 @@ private:
} }
}; };
GenerateConstructorRefactoringHelper helper(this, GenerateConstructorRefactoringHelper helper(this,
currentFile()->filePath(),
m_classAST->symbol, m_classAST->symbol,
m_classAST, m_classAST,
accessSpec); accessSpec);
@@ -1509,7 +1506,7 @@ public:
void perform() override void perform() override
{ {
GetterSetterRefactoringHelper helper(this, currentFile()->filePath(), m_data.clazz); GetterSetterRefactoringHelper helper(this, m_data.clazz);
helper.performGeneration(m_data, m_generateFlags); helper.performGeneration(m_data, m_generateFlags);
helper.applyChanges(); helper.applyChanges();
} }
@@ -1829,9 +1826,7 @@ private:
} }
if (m_candidates.empty()) if (m_candidates.empty())
return; return;
GetterSetterRefactoringHelper helper(this, GetterSetterRefactoringHelper helper(this, m_candidates.front().data.clazz);
currentFile()->filePath(),
m_candidates.front().data.clazz);
for (MemberInfo &mi : m_candidates) { for (MemberInfo &mi : m_candidates) {
if (mi.requestedFlags != 0) { if (mi.requestedFlags != 0) {
helper.performGeneration(mi.data, mi.requestedFlags); helper.performGeneration(mi.data, mi.requestedFlags);

View File

@@ -767,8 +767,7 @@ public:
printer.showTemplateParameters = true; printer.showTemplateParameters = true;
Utils::ChangeSet headerChangeSet; Utils::ChangeSet headerChangeSet;
const CppRefactoringChanges refactoring(snapshot()); const CppRefactoringChanges refactoring(snapshot());
const Utils::FilePath filePath = currentFile()->filePath(); const CppRefactoringFilePtr headerFile = currentFile();
const CppRefactoringFilePtr headerFile = refactoring.cppFile(filePath);
const LookupContext targetContext(headerFile->cppDocument(), snapshot()); const LookupContext targetContext(headerFile->cppDocument(), snapshot());
const Class *targetClass = m_classAST->symbol; const Class *targetClass = m_classAST->symbol;

View File

@@ -155,8 +155,7 @@ public:
// rewrite the function name // rewrite the function name
if (nameIncludesOperatorName(decl->name())) { if (nameIncludesOperatorName(decl->name())) {
CppRefactoringFilePtr file = refactoring.cppFile(op->filePath()); const QString operatorNameText = op->currentFile()->textOf(declAST->core_declarator);
const QString operatorNameText = file->textOf(declAST->core_declarator);
oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' ')); oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' '));
} }
const QString name = oo.prettyName(LookupContext::minimalName(decl, targetCoN, const QString name = oo.prettyName(LookupContext::minimalName(decl, targetCoN,

View File

@@ -86,10 +86,10 @@ public:
}; };
MoveFuncDefRefactoringHelper(CppQuickFixOperation *operation, MoveType type, MoveFuncDefRefactoringHelper(CppQuickFixOperation *operation, MoveType type,
const FilePath &fromFile, const FilePath &toFile) const FilePath &toFile)
: m_operation(operation), m_type(type), m_changes(m_operation->snapshot()) : m_operation(operation), m_type(type), m_changes(m_operation->snapshot())
{ {
m_fromFile = m_changes.cppFile(fromFile); m_fromFile = operation->currentFile();
m_toFile = (m_type == MoveOutside) ? m_fromFile : m_changes.cppFile(toFile); m_toFile = (m_type == MoveOutside) ? m_fromFile : m_changes.cppFile(toFile);
} }
@@ -159,19 +159,18 @@ public:
, m_funcDef(funcDef) , m_funcDef(funcDef)
, m_type(type) , m_type(type)
, m_cppFilePath(cppFilePath) , m_cppFilePath(cppFilePath)
, m_headerFilePath(funcDef->symbol->filePath())
{ {
if (m_type == MoveFuncDefRefactoringHelper::MoveOutside) { if (m_type == MoveFuncDefRefactoringHelper::MoveOutside) {
setDescription(Tr::tr("Move Definition Outside Class")); setDescription(Tr::tr("Move Definition Outside Class"));
} else { } else {
const FilePath resolved = m_cppFilePath.relativePathFrom(m_headerFilePath.parentDir()); const FilePath resolved = m_cppFilePath.relativePathFrom(filePath().parentDir());
setDescription(Tr::tr("Move Definition to %1").arg(resolved.displayName())); setDescription(Tr::tr("Move Definition to %1").arg(resolved.displayName()));
} }
} }
void perform() override void perform() override
{ {
MoveFuncDefRefactoringHelper helper(this, m_type, m_headerFilePath, m_cppFilePath); MoveFuncDefRefactoringHelper helper(this, m_type, m_cppFilePath);
helper.performMove(m_funcDef); helper.performMove(m_funcDef);
helper.applyChanges(); helper.applyChanges();
} }
@@ -180,7 +179,6 @@ private:
FunctionDefinitionAST *m_funcDef; FunctionDefinitionAST *m_funcDef;
MoveFuncDefRefactoringHelper::MoveType m_type; MoveFuncDefRefactoringHelper::MoveType m_type;
const FilePath m_cppFilePath; const FilePath m_cppFilePath;
const FilePath m_headerFilePath;
}; };
class MoveAllFuncDefOutsideOp : public CppQuickFixOperation class MoveAllFuncDefOutsideOp : public CppQuickFixOperation
@@ -193,12 +191,11 @@ public:
, m_type(type) , m_type(type)
, m_classDef(classDef) , m_classDef(classDef)
, m_cppFilePath(cppFileName) , m_cppFilePath(cppFileName)
, m_headerFilePath(classDef->symbol->filePath())
{ {
if (m_type == MoveFuncDefRefactoringHelper::MoveOutside) { if (m_type == MoveFuncDefRefactoringHelper::MoveOutside) {
setDescription(Tr::tr("Definitions Outside Class")); setDescription(Tr::tr("Definitions Outside Class"));
} else { } else {
const FilePath resolved = m_cppFilePath.relativePathFrom(m_headerFilePath.parentDir()); const FilePath resolved = m_cppFilePath.relativePathFrom(filePath().parentDir());
setDescription(Tr::tr("Move All Function Definitions to %1") setDescription(Tr::tr("Move All Function Definitions to %1")
.arg(resolved.displayName())); .arg(resolved.displayName()));
} }
@@ -206,7 +203,7 @@ public:
void perform() override void perform() override
{ {
MoveFuncDefRefactoringHelper helper(this, m_type, m_headerFilePath, m_cppFilePath); MoveFuncDefRefactoringHelper helper(this, m_type, m_cppFilePath);
for (DeclarationListAST *it = m_classDef->member_specifier_list; it; it = it->next) { for (DeclarationListAST *it = m_classDef->member_specifier_list; it; it = it->next) {
if (FunctionDefinitionAST *funcAST = it->value->asFunctionDefinition()) { if (FunctionDefinitionAST *funcAST = it->value->asFunctionDefinition()) {
if (funcAST->symbol && !funcAST->symbol->isGenerated()) if (funcAST->symbol && !funcAST->symbol->isGenerated())
@@ -220,7 +217,6 @@ private:
MoveFuncDefRefactoringHelper::MoveType m_type; MoveFuncDefRefactoringHelper::MoveType m_type;
ClassSpecifierAST *m_classDef; ClassSpecifierAST *m_classDef;
const FilePath m_cppFilePath; const FilePath m_cppFilePath;
const FilePath m_headerFilePath;
}; };
class MoveFuncDefToDeclOp : public CppQuickFixOperation class MoveFuncDefToDeclOp : public CppQuickFixOperation

View File

@@ -235,16 +235,17 @@ public:
private: private:
void perform() override void perform() override
{ {
const auto textDoc = const_cast<QTextDocument *>(currentFile()->document()); const CppRefactoringFilePtr file = currentFile();
const int pos = currentFile()->cppDocument()->translationUnit()->getTokenPositionInDocument( const auto textDoc = const_cast<QTextDocument *>(file->document());
const int pos = file->cppDocument()->translationUnit()->getTokenPositionInDocument(
m_symbol->sourceLocation(), textDoc); m_symbol->sourceLocation(), textDoc);
QTextCursor cursor(textDoc); QTextCursor cursor(textDoc);
cursor.setPosition(pos); cursor.setPosition(pos);
const CursorInEditor cursorInEditor(cursor, currentFile()->filePath(), editor(), const CursorInEditor cursorInEditor(cursor, file->filePath(), editor(),
editor()->textDocument()); editor()->textDocument());
const auto callback = [symbolLoc = m_symbol->toLink(), comments = m_commentTokens] const auto callback = [symbolLoc = m_symbol->toLink(), comments = m_commentTokens, file]
(const Link &link) { (const Link &link) {
moveComments(link, symbolLoc, comments); moveComments(file, link, symbolLoc, comments);
}; };
NonInteractiveFollowSymbolMarker niMarker; NonInteractiveFollowSymbolMarker niMarker;
CppCodeModelSettings::setInteractiveFollowSymbol(false); CppCodeModelSettings::setInteractiveFollowSymbol(false);
@@ -252,14 +253,16 @@ private:
FollowSymbolMode::Exact); FollowSymbolMode::Exact);
} }
static void moveComments(const Link &targetLoc, const Link &symbolLoc, static void moveComments(
const QList<Token> &comments) const CppRefactoringFilePtr &sourceFile,
const Link &targetLoc,
const Link &symbolLoc,
const QList<Token> &comments)
{ {
if (!targetLoc.hasValidTarget() || targetLoc.hasSameLocation(symbolLoc)) if (!targetLoc.hasValidTarget() || targetLoc.hasSameLocation(symbolLoc))
return; return;
CppRefactoringChanges changes(CppModelManager::snapshot()); CppRefactoringChanges changes(CppModelManager::snapshot());
const CppRefactoringFilePtr sourceFile = changes.cppFile(symbolLoc.targetFilePath);
const CppRefactoringFilePtr targetFile const CppRefactoringFilePtr targetFile
= targetLoc.targetFilePath == symbolLoc.targetFilePath = targetLoc.targetFilePath == symbolLoc.targetFilePath
? sourceFile ? sourceFile

View File

@@ -41,6 +41,7 @@ public:
setDescription( setDescription(
Tr::tr("Re-order Member Function Definitions According to Declaration Order")); Tr::tr("Re-order Member Function Definitions According to Declaration Order"));
m_state->decls = decls; m_state->decls = decls;
m_state->currentFile = currentFile();
} }
private: private:
@@ -71,6 +72,7 @@ private:
QList<Symbol *> decls; QList<Symbol *> decls;
QHash<FilePath, DefLocations> defLocations; QHash<FilePath, DefLocations> defLocations;
CppRefactoringFilePtr currentFile;
int remainingFollowSymbolOps = 0; int remainingFollowSymbolOps = 0;
}; };
@@ -158,7 +160,9 @@ private:
if (defLocsExpectedOrder == defLocsActualOrder) if (defLocsExpectedOrder == defLocsActualOrder)
continue; continue;
CppRefactoringFilePtr file = factory.cppFile(it.key()); CppRefactoringFilePtr file = it.key() == state->currentFile->filePath()
? state->currentFile
: factory.cppFile(it.key());
ChangeSet changes; ChangeSet changes;
for (int i = 0; i < defLocsActualOrder.size(); ++i) { for (int i = 0; i < defLocsActualOrder.size(); ++i) {
const DefLocation &actualLoc = defLocsActualOrder[i]; const DefLocation &actualLoc = defLocsActualOrder[i];