forked from qt-creator/qt-creator
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:
@@ -260,15 +260,14 @@ static void extractNames(const CppRefactoringFilePtr &file,
|
||||
class GetterSetterRefactoringHelper
|
||||
{
|
||||
public:
|
||||
GetterSetterRefactoringHelper(CppQuickFixOperation *operation,
|
||||
const FilePath &filePath,
|
||||
Class *clazz)
|
||||
GetterSetterRefactoringHelper(CppQuickFixOperation *operation, Class *clazz)
|
||||
: m_operation(operation)
|
||||
, m_changes(m_operation->snapshot())
|
||||
, m_locator(m_changes)
|
||||
, m_headerFile(m_changes.cppFile(filePath))
|
||||
, m_headerFile(operation->currentFile())
|
||||
, m_sourceFile([&] {
|
||||
FilePath cppFilePath = correspondingHeaderOrSource(filePath, &m_isHeaderHeaderFile);
|
||||
FilePath cppFilePath = correspondingHeaderOrSource(m_headerFile->filePath(),
|
||||
&m_isHeaderHeaderFile);
|
||||
if (!m_isHeaderHeaderFile || !cppFilePath.exists()) {
|
||||
// there is no "source" file
|
||||
return m_headerFile;
|
||||
@@ -1271,11 +1270,10 @@ private:
|
||||
const ClassSpecifierAST *m_classAST;
|
||||
InsertionPointLocator::AccessSpec m_accessSpec;
|
||||
GenerateConstructorRefactoringHelper(CppQuickFixOperation *operation,
|
||||
const FilePath &filePath,
|
||||
Class *clazz,
|
||||
const ClassSpecifierAST *classAST,
|
||||
InsertionPointLocator::AccessSpec accessSpec)
|
||||
: GetterSetterRefactoringHelper(operation, filePath, clazz)
|
||||
: GetterSetterRefactoringHelper(operation, clazz)
|
||||
, m_classAST(classAST)
|
||||
, m_accessSpec(accessSpec)
|
||||
{}
|
||||
@@ -1416,7 +1414,6 @@ private:
|
||||
}
|
||||
};
|
||||
GenerateConstructorRefactoringHelper helper(this,
|
||||
currentFile()->filePath(),
|
||||
m_classAST->symbol,
|
||||
m_classAST,
|
||||
accessSpec);
|
||||
@@ -1509,7 +1506,7 @@ public:
|
||||
|
||||
void perform() override
|
||||
{
|
||||
GetterSetterRefactoringHelper helper(this, currentFile()->filePath(), m_data.clazz);
|
||||
GetterSetterRefactoringHelper helper(this, m_data.clazz);
|
||||
helper.performGeneration(m_data, m_generateFlags);
|
||||
helper.applyChanges();
|
||||
}
|
||||
@@ -1829,9 +1826,7 @@ private:
|
||||
}
|
||||
if (m_candidates.empty())
|
||||
return;
|
||||
GetterSetterRefactoringHelper helper(this,
|
||||
currentFile()->filePath(),
|
||||
m_candidates.front().data.clazz);
|
||||
GetterSetterRefactoringHelper helper(this, m_candidates.front().data.clazz);
|
||||
for (MemberInfo &mi : m_candidates) {
|
||||
if (mi.requestedFlags != 0) {
|
||||
helper.performGeneration(mi.data, mi.requestedFlags);
|
||||
|
@@ -767,8 +767,7 @@ public:
|
||||
printer.showTemplateParameters = true;
|
||||
Utils::ChangeSet headerChangeSet;
|
||||
const CppRefactoringChanges refactoring(snapshot());
|
||||
const Utils::FilePath filePath = currentFile()->filePath();
|
||||
const CppRefactoringFilePtr headerFile = refactoring.cppFile(filePath);
|
||||
const CppRefactoringFilePtr headerFile = currentFile();
|
||||
const LookupContext targetContext(headerFile->cppDocument(), snapshot());
|
||||
|
||||
const Class *targetClass = m_classAST->symbol;
|
||||
|
@@ -155,8 +155,7 @@ public:
|
||||
|
||||
// rewrite the function name
|
||||
if (nameIncludesOperatorName(decl->name())) {
|
||||
CppRefactoringFilePtr file = refactoring.cppFile(op->filePath());
|
||||
const QString operatorNameText = file->textOf(declAST->core_declarator);
|
||||
const QString operatorNameText = op->currentFile()->textOf(declAST->core_declarator);
|
||||
oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' '));
|
||||
}
|
||||
const QString name = oo.prettyName(LookupContext::minimalName(decl, targetCoN,
|
||||
|
@@ -86,10 +86,10 @@ public:
|
||||
};
|
||||
|
||||
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_fromFile = m_changes.cppFile(fromFile);
|
||||
m_fromFile = operation->currentFile();
|
||||
m_toFile = (m_type == MoveOutside) ? m_fromFile : m_changes.cppFile(toFile);
|
||||
}
|
||||
|
||||
@@ -159,19 +159,18 @@ public:
|
||||
, m_funcDef(funcDef)
|
||||
, m_type(type)
|
||||
, m_cppFilePath(cppFilePath)
|
||||
, m_headerFilePath(funcDef->symbol->filePath())
|
||||
{
|
||||
if (m_type == MoveFuncDefRefactoringHelper::MoveOutside) {
|
||||
setDescription(Tr::tr("Move Definition Outside Class"));
|
||||
} 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()));
|
||||
}
|
||||
}
|
||||
|
||||
void perform() override
|
||||
{
|
||||
MoveFuncDefRefactoringHelper helper(this, m_type, m_headerFilePath, m_cppFilePath);
|
||||
MoveFuncDefRefactoringHelper helper(this, m_type, m_cppFilePath);
|
||||
helper.performMove(m_funcDef);
|
||||
helper.applyChanges();
|
||||
}
|
||||
@@ -180,7 +179,6 @@ private:
|
||||
FunctionDefinitionAST *m_funcDef;
|
||||
MoveFuncDefRefactoringHelper::MoveType m_type;
|
||||
const FilePath m_cppFilePath;
|
||||
const FilePath m_headerFilePath;
|
||||
};
|
||||
|
||||
class MoveAllFuncDefOutsideOp : public CppQuickFixOperation
|
||||
@@ -193,12 +191,11 @@ public:
|
||||
, m_type(type)
|
||||
, m_classDef(classDef)
|
||||
, m_cppFilePath(cppFileName)
|
||||
, m_headerFilePath(classDef->symbol->filePath())
|
||||
{
|
||||
if (m_type == MoveFuncDefRefactoringHelper::MoveOutside) {
|
||||
setDescription(Tr::tr("Definitions Outside Class"));
|
||||
} 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")
|
||||
.arg(resolved.displayName()));
|
||||
}
|
||||
@@ -206,7 +203,7 @@ public:
|
||||
|
||||
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) {
|
||||
if (FunctionDefinitionAST *funcAST = it->value->asFunctionDefinition()) {
|
||||
if (funcAST->symbol && !funcAST->symbol->isGenerated())
|
||||
@@ -220,7 +217,6 @@ private:
|
||||
MoveFuncDefRefactoringHelper::MoveType m_type;
|
||||
ClassSpecifierAST *m_classDef;
|
||||
const FilePath m_cppFilePath;
|
||||
const FilePath m_headerFilePath;
|
||||
};
|
||||
|
||||
class MoveFuncDefToDeclOp : public CppQuickFixOperation
|
||||
|
@@ -235,16 +235,17 @@ public:
|
||||
private:
|
||||
void perform() override
|
||||
{
|
||||
const auto textDoc = const_cast<QTextDocument *>(currentFile()->document());
|
||||
const int pos = currentFile()->cppDocument()->translationUnit()->getTokenPositionInDocument(
|
||||
const CppRefactoringFilePtr file = currentFile();
|
||||
const auto textDoc = const_cast<QTextDocument *>(file->document());
|
||||
const int pos = file->cppDocument()->translationUnit()->getTokenPositionInDocument(
|
||||
m_symbol->sourceLocation(), textDoc);
|
||||
QTextCursor cursor(textDoc);
|
||||
cursor.setPosition(pos);
|
||||
const CursorInEditor cursorInEditor(cursor, currentFile()->filePath(), editor(),
|
||||
const CursorInEditor cursorInEditor(cursor, file->filePath(), editor(),
|
||||
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) {
|
||||
moveComments(link, symbolLoc, comments);
|
||||
moveComments(file, link, symbolLoc, comments);
|
||||
};
|
||||
NonInteractiveFollowSymbolMarker niMarker;
|
||||
CppCodeModelSettings::setInteractiveFollowSymbol(false);
|
||||
@@ -252,14 +253,16 @@ private:
|
||||
FollowSymbolMode::Exact);
|
||||
}
|
||||
|
||||
static void moveComments(const Link &targetLoc, const Link &symbolLoc,
|
||||
static void moveComments(
|
||||
const CppRefactoringFilePtr &sourceFile,
|
||||
const Link &targetLoc,
|
||||
const Link &symbolLoc,
|
||||
const QList<Token> &comments)
|
||||
{
|
||||
if (!targetLoc.hasValidTarget() || targetLoc.hasSameLocation(symbolLoc))
|
||||
return;
|
||||
|
||||
CppRefactoringChanges changes(CppModelManager::snapshot());
|
||||
const CppRefactoringFilePtr sourceFile = changes.cppFile(symbolLoc.targetFilePath);
|
||||
const CppRefactoringFilePtr targetFile
|
||||
= targetLoc.targetFilePath == symbolLoc.targetFilePath
|
||||
? sourceFile
|
||||
|
@@ -41,6 +41,7 @@ public:
|
||||
setDescription(
|
||||
Tr::tr("Re-order Member Function Definitions According to Declaration Order"));
|
||||
m_state->decls = decls;
|
||||
m_state->currentFile = currentFile();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -71,6 +72,7 @@ private:
|
||||
|
||||
QList<Symbol *> decls;
|
||||
QHash<FilePath, DefLocations> defLocations;
|
||||
CppRefactoringFilePtr currentFile;
|
||||
int remainingFollowSymbolOps = 0;
|
||||
};
|
||||
|
||||
@@ -158,7 +160,9 @@ private:
|
||||
if (defLocsExpectedOrder == defLocsActualOrder)
|
||||
continue;
|
||||
|
||||
CppRefactoringFilePtr file = factory.cppFile(it.key());
|
||||
CppRefactoringFilePtr file = it.key() == state->currentFile->filePath()
|
||||
? state->currentFile
|
||||
: factory.cppFile(it.key());
|
||||
ChangeSet changes;
|
||||
for (int i = 0; i < defLocsActualOrder.size(); ++i) {
|
||||
const DefLocation &actualLoc = defLocsActualOrder[i];
|
||||
|
Reference in New Issue
Block a user