TextEditor: filepathify RefactoringChanges

Change-Id: Ie97e484bcdeaa0cb2f5d04b3c79ace55ff2e426c
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-05-28 12:02:36 +02:00
parent 15e4649fe8
commit 79b9a2fea6
21 changed files with 238 additions and 195 deletions

View File

@@ -78,7 +78,8 @@ void ClangFixItOperation::perform()
const QString filePath = i.key(); const QString filePath = i.key();
const QVector<ClangBackEnd::FixItContainer> fixits = i.value(); const QVector<ClangBackEnd::FixItContainer> fixits = i.value();
RefactoringFilePtr refactoringFile = refactoringChanges.file(filePath); RefactoringFilePtr refactoringFile = refactoringChanges.file(
Utils::FilePath::fromString(filePath));
refactoringFiles.append(refactoringFile); refactoringFiles.append(refactoringFile);
applyFixitsToFile(*refactoringFile, fixits); applyFixitsToFile(*refactoringFile, fixits);

View File

@@ -70,7 +70,7 @@ void ClangToolQuickFixOperation::perform()
continue; continue;
TextEditor::RefactoringFilePtr &refactoringFile = refactoringFiles[step.location.filePath]; TextEditor::RefactoringFilePtr &refactoringFile = refactoringFiles[step.location.filePath];
if (refactoringFile.isNull()) if (refactoringFile.isNull())
refactoringFile = changes.file(step.location.filePath); refactoringFile = changes.file(Utils::FilePath::fromString(step.location.filePath));
Utils::ChangeSet changeSet = refactoringFile->changeSet(); Utils::ChangeSet changeSet = refactoringFile->changeSet();
Range range = toRange(refactoringFile->document(), {step.ranges.first(), step.ranges.last()}); Range range = toRange(refactoringFile->document(), {step.ranges.first(), step.ranges.last()});
changeSet.replace(range, step.message); changeSet.replace(range, step.message);

View File

@@ -1129,7 +1129,7 @@ void CppEditorWidget::onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefL
abortDeclDefLink(); abortDeclDefLink();
d->m_declDefLink = link; d->m_declDefLink = link;
IDocument *targetDocument = DocumentModel::documentForFilePath( IDocument *targetDocument = DocumentModel::documentForFilePath(
FilePath::fromString(d->m_declDefLink->targetFile->fileName())); d->m_declDefLink->targetFile->filePath());
if (textDocument() != targetDocument) { if (textDocument() != targetDocument) {
if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument)) if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument))
connect(textDocument, connect(textDocument,
@@ -1162,7 +1162,7 @@ void CppEditorWidget::abortDeclDefLink()
return; return;
IDocument *targetDocument = DocumentModel::documentForFilePath( IDocument *targetDocument = DocumentModel::documentForFilePath(
FilePath::fromString(d->m_declDefLink->targetFile->fileName())); d->m_declDefLink->targetFile->filePath());
if (textDocument() != targetDocument) { if (textDocument() != targetDocument) {
if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument)) if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument))
disconnect(textDocument, disconnect(textDocument,

View File

@@ -172,7 +172,8 @@ static QSharedPointer<FunctionDeclDefLink> findLinkHelper(QSharedPointer<Functio
// parse the target file to get the linked decl/def // parse the target file to get the linked decl/def
const QString targetFileName = QString::fromUtf8( const QString targetFileName = QString::fromUtf8(
target->fileName(), target->fileNameLength()); target->fileName(), target->fileNameLength());
CppRefactoringFileConstPtr targetFile = changes.fileNoEditor(targetFileName); CppRefactoringFileConstPtr targetFile = changes.fileNoEditor(
Utils::FilePath::fromString(targetFileName));
if (!targetFile->isValid()) if (!targetFile->isValid())
return noResult; return noResult;
@@ -220,7 +221,8 @@ void FunctionDeclDefLinkFinder::startFindLinkAt(
// find the start/end offsets // find the start/end offsets
CppRefactoringChanges refactoringChanges(snapshot); CppRefactoringChanges refactoringChanges(snapshot);
CppRefactoringFilePtr sourceFile = refactoringChanges.file(doc->fileName()); CppRefactoringFilePtr sourceFile = refactoringChanges.file(
Utils::FilePath::fromString(doc->fileName()));
sourceFile->setCppDocument(doc); sourceFile->setCppDocument(doc);
int start, end; int start, end;
declDefLinkStartEnd(sourceFile, parent, funcDecl, &start, &end); declDefLinkStartEnd(sourceFile, parent, funcDecl, &start, &end);
@@ -280,7 +282,7 @@ void FunctionDeclDefLink::apply(CppEditorWidget *editor, bool jumpToMatch)
// first verify the interesting region of the target file is unchanged // first verify the interesting region of the target file is unchanged
CppRefactoringChanges refactoringChanges(snapshot); CppRefactoringChanges refactoringChanges(snapshot);
CppRefactoringFilePtr newTargetFile = refactoringChanges.file(targetFile->fileName()); CppRefactoringFilePtr newTargetFile = refactoringChanges.file(targetFile->filePath());
if (!newTargetFile->isValid()) if (!newTargetFile->isValid())
return; return;
const int targetStart = newTargetFile->position(targetLine, targetColumn); const int targetStart = newTargetFile->position(targetLine, targetColumn);

View File

@@ -786,8 +786,8 @@ public:
printer.showTemplateParameters = true; printer.showTemplateParameters = true;
Utils::ChangeSet headerChangeSet; Utils::ChangeSet headerChangeSet;
const CppRefactoringChanges refactoring(snapshot()); const CppRefactoringChanges refactoring(snapshot());
const QString filename = currentFile()->fileName(); const Utils::FilePath filePath = currentFile()->filePath();
const CppRefactoringFilePtr headerFile = refactoring.file(filename); const CppRefactoringFilePtr headerFile = refactoring.file(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;
@@ -905,7 +905,8 @@ public:
if (!clazz) if (!clazz)
return; return;
CppRefactoringFilePtr implementationFile = refactoring.file(m_cppFileName); CppRefactoringFilePtr implementationFile = refactoring.file(
Utils::FilePath::fromString(m_cppFileName));
Utils::ChangeSet implementationChangeSet; Utils::ChangeSet implementationChangeSet;
const int insertPos = qMax(0, implementationFile->document()->characterCount() - 1); const int insertPos = qMax(0, implementationFile->document()->characterCount() - 1);

View File

@@ -361,7 +361,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
if (negation) { if (negation) {
@@ -455,7 +455,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
changes.flip(currentFile->range(binary->left_expression), changes.flip(currentFile->range(binary->left_expression),
@@ -544,7 +544,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
changes.replace(currentFile->range(pattern->binary_op_token), QLatin1String("||")); changes.replace(currentFile->range(pattern->binary_op_token), QLatin1String("||"));
@@ -631,7 +631,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
@@ -722,7 +722,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
@@ -797,7 +797,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
@@ -871,7 +871,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
@@ -950,7 +950,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
const Token binaryToken = currentFile->tokenAt(condition->binary_op_token); const Token binaryToken = currentFile->tokenAt(condition->binary_op_token);
@@ -1189,7 +1189,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
@@ -1393,7 +1393,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
@@ -1455,7 +1455,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
changes.replace(start, end, replacement); changes.replace(start, end, replacement);
@@ -1614,7 +1614,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
TypeOfExpression typeOfExpression; TypeOfExpression typeOfExpression;
typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings()); typeOfExpression.init(semanticInfo().doc, snapshot(), context().bindings());
@@ -1712,7 +1712,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
QString newName = m_isAllUpper ? m_name.toLower() : m_name; QString newName = m_isAllUpper ? m_name.toLower() : m_name;
for (int i = 1; i < newName.length(); ++i) { for (int i = 1; i < newName.length(); ++i) {
@@ -1794,7 +1794,7 @@ AddIncludeForUndefinedIdentifierOp::AddIncludeForUndefinedIdentifierOp(
void AddIncludeForUndefinedIdentifierOp::perform() void AddIncludeForUndefinedIdentifierOp::perform()
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr file = refactoring.file(filePath().toString()); CppRefactoringFilePtr file = refactoring.file(filePath());
insertNewIncludeDirective(m_include, file, semanticInfo().doc); insertNewIncludeDirective(m_include, file, semanticInfo().doc);
} }
@@ -1817,7 +1817,7 @@ void AddForwardDeclForUndefinedIdentifierOp::perform()
const QStringList namespaces = parts.mid(0, parts.length() - 1); const QStringList namespaces = parts.mid(0, parts.length() - 1);
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr file = refactoring.file(filePath().toString()); CppRefactoringFilePtr file = refactoring.file(filePath());
NSVisitor visitor(file.data(), namespaces, m_symbolPos); NSVisitor visitor(file.data(), namespaces, m_symbolPos);
visitor.accept(file->cppDocument()->translationUnit()->ast()); visitor.accept(file->cppDocument()->translationUnit()->ast());
@@ -2164,7 +2164,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
int targetEndPos = currentFile->endOf(m_targetParam); int targetEndPos = currentFile->endOf(m_targetParam);
ChangeSet changes; ChangeSet changes;
@@ -2244,7 +2244,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
currentFile->setChangeSet(m_change); currentFile->setChangeSet(m_change);
currentFile->apply(); currentFile->apply();
} }
@@ -2406,7 +2406,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ChangeSet changes; ChangeSet changes;
int start = currentFile->endOf(compoundStatement->lbrace_token); int start = currentFile->endOf(compoundStatement->lbrace_token);
@@ -2548,7 +2548,8 @@ public:
m_targetFileName, m_targetSymbol, m_xsSpec); m_targetFileName, m_targetSymbol, m_xsSpec);
QTC_ASSERT(loc.isValid(), return); QTC_ASSERT(loc.isValid(), return);
CppRefactoringFilePtr targetFile = refactoring.file(m_targetFileName); CppRefactoringFilePtr targetFile = refactoring.file(
Utils::FilePath::fromString(m_targetFileName));
int targetPosition1 = targetFile->position(loc.line(), loc.column()); int targetPosition1 = targetFile->position(loc.line(), loc.column());
int targetPosition2 = qMax(0, targetFile->position(loc.line(), 1) - 1); int targetPosition2 = qMax(0, targetFile->position(loc.line(), 1) - 1);
@@ -2730,7 +2731,8 @@ public:
refactoring, targetFilePath); refactoring, targetFilePath);
QTC_ASSERT(loc.isValid(), return); QTC_ASSERT(loc.isValid(), return);
CppRefactoringFilePtr targetFile = refactoring.file(loc.fileName()); CppRefactoringFilePtr targetFile = refactoring.file(
Utils::FilePath::fromString(loc.fileName()));
Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview(); Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview();
oo.showFunctionSignatures = true; oo.showFunctionSignatures = true;
oo.showReturnTypes = true; oo.showReturnTypes = true;
@@ -2792,7 +2794,7 @@ public:
// rewrite the function name // rewrite the function name
if (nameIncludesOperatorName(decl->name())) { if (nameIncludesOperatorName(decl->name())) {
CppRefactoringFilePtr file = refactoring.file(op->filePath().toString()); CppRefactoringFilePtr file = refactoring.file(op->filePath());
const QString operatorNameText = file->textOf(declAST->core_declarator); const QString operatorNameText = file->textOf(declAST->core_declarator);
oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' ')); oo.includeWhiteSpaceInOperatorName = operatorNameText.contains(QLatin1Char(' '));
} }
@@ -2982,7 +2984,7 @@ private:
filePath, m_class, InsertionPointLocator::Private); filePath, m_class, InsertionPointLocator::Private);
QTC_ASSERT(loc.isValid(), return); QTC_ASSERT(loc.isValid(), return);
CppRefactoringFilePtr targetFile = refactoring.file(filePath); CppRefactoringFilePtr targetFile = refactoring.file(Utils::FilePath::fromString(filePath));
const int targetPosition1 = targetFile->position(loc.line(), loc.column()); const int targetPosition1 = targetFile->position(loc.line(), loc.column());
const int targetPosition2 = qMax(0, targetFile->position(loc.line(), 1) - 1); const int targetPosition2 = qMax(0, targetFile->position(loc.line(), 1) - 1);
ChangeSet target; ChangeSet target;
@@ -3312,7 +3314,8 @@ private:
&changeSet.first, &changeSet.second); &changeSet.first, &changeSet.second);
} }
for (auto it = changeSets.cbegin(); it != changeSets.cend(); ++it) { for (auto it = changeSets.cbegin(); it != changeSets.cend(); ++it) {
const CppRefactoringFilePtr file = refactoring.file(it.key()); const CppRefactoringFilePtr file = refactoring.file(
Utils::FilePath::fromString(it.key()));
for (const ChangeSet::Range &r : it.value().second) for (const ChangeSet::Range &r : it.value().second)
file->appendIndentRange(r); file->appendIndentRange(r);
file->setChangeSet(it.value().first); file->setChangeSet(it.value().first);
@@ -3438,14 +3441,14 @@ public:
: 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.file(fileName)) , m_headerFile(m_changes.file(Utils::FilePath::fromString(fileName)))
, m_sourceFile([&] { , m_sourceFile([&] {
QString cppFileName = correspondingHeaderOrSource(fileName, &m_isHeaderHeaderFile); QString cppFileName = correspondingHeaderOrSource(fileName, &m_isHeaderHeaderFile);
if (!m_isHeaderHeaderFile || !QFile::exists(cppFileName)) { if (!m_isHeaderHeaderFile || !QFile::exists(cppFileName)) {
// there is no "source" file // there is no "source" file
return m_headerFile; return m_headerFile;
} else { } else {
return m_changes.file(cppFileName); return m_changes.file(Utils::FilePath::fromString(cppFileName));
} }
}()) }())
, m_class(clazz) , m_class(clazz)
@@ -3613,7 +3616,7 @@ protected:
if (insertionPoint != m_headerInsertionPoints.end()) if (insertionPoint != m_headerInsertionPoints.end())
return *insertionPoint; return *insertionPoint;
const InsertionLocation loc = m_locator.methodDeclarationInClass( const InsertionLocation loc = m_locator.methodDeclarationInClass(
m_headerFile->fileName(), m_class, spec, m_headerFile->filePath().toString(), m_class, spec,
InsertionPointLocator::ForceAccessSpec::Yes); InsertionPointLocator::ForceAccessSpec::Yes);
m_headerInsertionPoints.insert(spec, loc); m_headerInsertionPoints.insert(spec, loc);
return loc; return loc;
@@ -3630,7 +3633,7 @@ protected:
? NamespaceHandling::CreateMissing ? NamespaceHandling::CreateMissing
: NamespaceHandling::Ignore, : NamespaceHandling::Ignore,
m_changes, m_changes,
m_sourceFile->fileName(), m_sourceFile->filePath().toString(),
insertedNamespaces); insertedNamespaces);
if (m_settings->addUsingNamespaceinCppFile()) { if (m_settings->addUsingNamespaceinCppFile()) {
// check if we have to insert a using namespace ... // check if we have to insert a using namespace ...
@@ -3768,7 +3771,9 @@ public:
void perform() override void perform() override
{ {
GetterSetterRefactoringHelper helper(this, currentFile()->fileName(), m_data.clazz); GetterSetterRefactoringHelper helper(this,
currentFile()->filePath().toString(),
m_data.clazz);
helper.performGeneration(m_data, m_generateFlags); helper.performGeneration(m_data, m_generateFlags);
helper.applyChanges(); helper.applyChanges();
} }
@@ -3945,11 +3950,12 @@ void GetterSetterRefactoringHelper::performGeneration(ExistingGetterSetterData d
+ "()" + constSpec + "\n{\nreturn " + returnExpression + ";\n}"; + "()" + constSpec + "\n{\nreturn " + returnExpression + ";\n}";
addSourceFileCode(code); addSourceFileCode(code);
} else if (getterLocation == CppQuickFixSettings::FunctionLocation::OutsideClass) { } else if (getterLocation == CppQuickFixSettings::FunctionLocation::OutsideClass) {
InsertionLocation loc = insertLocationForMethodDefinition(data.declarationSymbol, InsertionLocation loc
= insertLocationForMethodDefinition(data.declarationSymbol,
false, false,
NamespaceHandling::Ignore, NamespaceHandling::Ignore,
m_changes, m_changes,
m_headerFile->fileName()); m_headerFile->filePath().toString());
const FullySpecifiedType returnType = getReturnTypeAt(m_headerFile, loc); const FullySpecifiedType returnType = getReturnTypeAt(m_headerFile, loc);
const QString clazz = symbolAt(data.clazz, m_headerFile, loc); const QString clazz = symbolAt(data.clazz, m_headerFile, loc);
QString code = overview.prettyType(returnType, clazz + "::" + data.getterName) QString code = overview.prettyType(returnType, clazz + "::" + data.getterName)
@@ -4020,11 +4026,12 @@ void GetterSetterRefactoringHelper::performGeneration(ExistingGetterSetterData d
+ body; + body;
addSourceFileCode(code); addSourceFileCode(code);
} else if (setterLocation == CppQuickFixSettings::FunctionLocation::OutsideClass) { } else if (setterLocation == CppQuickFixSettings::FunctionLocation::OutsideClass) {
InsertionLocation loc = insertLocationForMethodDefinition(data.declarationSymbol, InsertionLocation loc
= insertLocationForMethodDefinition(data.declarationSymbol,
false, false,
NamespaceHandling::Ignore, NamespaceHandling::Ignore,
m_changes, m_changes,
m_headerFile->fileName()); m_headerFile->filePath().toString());
FullySpecifiedType newParameterType = typeAt(data.declarationSymbol->type(), FullySpecifiedType newParameterType = typeAt(data.declarationSymbol->type(),
data.clazz, data.clazz,
@@ -4094,7 +4101,7 @@ void GetterSetterRefactoringHelper::performGeneration(ExistingGetterSetterData d
false, false,
NamespaceHandling::Ignore, NamespaceHandling::Ignore,
m_changes, m_changes,
m_headerFile->fileName()); m_headerFile->filePath().toString());
const FullySpecifiedType type = typeAt(data.declarationSymbol->type(), const FullySpecifiedType type = typeAt(data.declarationSymbol->type(),
data.clazz, data.clazz,
m_headerFile, m_headerFile,
@@ -4665,7 +4672,7 @@ private:
if (m_candidates.empty()) if (m_candidates.empty())
return; return;
GetterSetterRefactoringHelper helper(this, GetterSetterRefactoringHelper helper(this,
currentFile()->fileName(), currentFile()->filePath().toString(),
m_candidates.front().data.clazz); m_candidates.front().data.clazz);
for (MemberInfo &mi : m_candidates) { for (MemberInfo &mi : m_candidates) {
if (mi.requestedFlags != 0) { if (mi.requestedFlags != 0) {
@@ -4744,7 +4751,7 @@ public:
{ {
QTC_ASSERT(!m_funcReturn || !m_relevantDecls.isEmpty(), return); QTC_ASSERT(!m_funcReturn || !m_relevantDecls.isEmpty(), return);
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
ExtractFunctionOptions options; ExtractFunctionOptions options;
if (m_functionNameGetter) if (m_functionNameGetter)
@@ -4850,7 +4857,7 @@ public:
} }
funcDef.append(QLatin1String("\n}\n\n")); funcDef.append(QLatin1String("\n}\n\n"));
funcDef.replace(QChar::ParagraphSeparator, QLatin1String("\n")); funcDef.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
funcDef.prepend(inlinePrefix(currentFile->fileName())); funcDef.prepend(inlinePrefix(currentFile->filePath().toString()));
funcCall.append(QLatin1Char(';')); funcCall.append(QLatin1Char(';'));
// Get starting indentation from original code. // Get starting indentation from original code.
@@ -4890,7 +4897,7 @@ public:
const QString fileName = QLatin1String(matchingClass->fileName()); const QString fileName = QLatin1String(matchingClass->fileName());
const InsertionLocation &location = const InsertionLocation &location =
locator.methodDeclarationInClass(fileName, matchingClass, options.access); locator.methodDeclarationInClass(fileName, matchingClass, options.access);
CppRefactoringFilePtr declFile = refactoring.file(fileName); CppRefactoringFilePtr declFile = refactoring.file(Utils::FilePath::fromString(fileName));
change.clear(); change.clear();
position = declFile->position(location.line(), location.column()); position = declFile->position(location.line(), location.column());
change.insert(position, location.prefix() + funcDecl + location.suffix()); change.insert(position, location.prefix() + funcDecl + location.suffix());
@@ -5400,7 +5407,7 @@ public:
declFileName = QString::fromUtf8(matchingClass->fileName(), declFileName = QString::fromUtf8(matchingClass->fileName(),
matchingClass->fileNameLength()); matchingClass->fileNameLength());
result.file = refactoring.file(declFileName); result.file = refactoring.file(Utils::FilePath::fromString(declFileName));
ASTPath astPath(result.file->cppDocument()); ASTPath astPath(result.file->cppDocument());
const QList<AST *> path = astPath(s->line(), s->column()); const QList<AST *> path = astPath(s->line(), s->column());
SimpleDeclarationAST *simpleDecl = nullptr; SimpleDeclarationAST *simpleDecl = nullptr;
@@ -5423,7 +5430,7 @@ public:
declFileName = correspondingHeaderOrSource(filePath().toString(), &isHeaderFile); declFileName = correspondingHeaderOrSource(filePath().toString(), &isHeaderFile);
if (!QFile::exists(declFileName)) if (!QFile::exists(declFileName))
return FoundDeclaration(); return FoundDeclaration();
result.file = refactoring.file(declFileName); result.file = refactoring.file(Utils::FilePath::fromString(declFileName));
if (!result.file) if (!result.file)
return FoundDeclaration(); return FoundDeclaration();
const LookupContext lc(result.file->cppDocument(), snapshot()); const LookupContext lc(result.file->cppDocument(), snapshot());
@@ -5452,7 +5459,7 @@ public:
FunctionDeclaratorAST *functionDeclaratorOfDefinition FunctionDeclaratorAST *functionDeclaratorOfDefinition
= functionDeclarator(m_functionDefinition); = functionDeclarator(m_functionDefinition);
const CppRefactoringChanges refactoring(snapshot()); const CppRefactoringChanges refactoring(snapshot());
const CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); const CppRefactoringFilePtr currentFile = refactoring.file(filePath());
deduceTypeNameOfLiteral(currentFile->cppDocument()); deduceTypeNameOfLiteral(currentFile->cppDocument());
ChangeSet changes; ChangeSet changes;
@@ -5474,7 +5481,7 @@ public:
appendFunctionParameter(functionDeclaratorOfDefinition, currentFile, &changes, appendFunctionParameter(functionDeclaratorOfDefinition, currentFile, &changes,
!functionDeclaration.ast); !functionDeclaration.ast);
if (functionDeclaration.ast) { if (functionDeclaration.ast) {
if (currentFile->fileName() != functionDeclaration.file->fileName()) { if (currentFile->filePath() != functionDeclaration.file->filePath()) {
ChangeSet declChanges; ChangeSet declChanges;
appendFunctionParameter(functionDeclaration.ast, functionDeclaration.file, &declChanges, appendFunctionParameter(functionDeclaration.ast, functionDeclaration.file, &declChanges,
true); true);
@@ -5634,7 +5641,7 @@ public:
, m_identifierAST(identifierAST) , m_identifierAST(identifierAST)
, m_symbol(symbol) , m_symbol(symbol)
, m_refactoring(snapshot()) , m_refactoring(snapshot())
, m_file(m_refactoring.file(filePath().toString())) , m_file(m_refactoring.file(filePath()))
, m_document(interface.semanticInfo().doc) , m_document(interface.semanticInfo().doc)
{ {
setDescription( setDescription(
@@ -6225,8 +6232,9 @@ public:
const QString &fromFile, const QString &toFile) const QString &fromFile, const QString &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.file(fromFile); m_fromFile = m_changes.file(Utils::FilePath::fromString(fromFile));
m_toFile = (m_type == MoveOutside) ? m_fromFile : m_changes.file(toFile); m_toFile = (m_type == MoveOutside) ? m_fromFile
: m_changes.file(Utils::FilePath::fromString(toFile));
} }
void performMove(FunctionDefinitionAST *funcAST) void performMove(FunctionDefinitionAST *funcAST)
@@ -6234,7 +6242,7 @@ public:
// Determine file, insert position and scope // Determine file, insert position and scope
InsertionLocation l = insertLocationForMethodDefinition( InsertionLocation l = insertLocationForMethodDefinition(
funcAST->symbol, false, NamespaceHandling::Ignore, funcAST->symbol, false, NamespaceHandling::Ignore,
m_changes, m_toFile->fileName()); m_changes, m_toFile->filePath().toString());
const QString prefix = l.prefix(); const QString prefix = l.prefix();
const QString suffix = l.suffix(); const QString suffix = l.suffix();
const int insertPos = m_toFile->position(l.line(), l.column()); const int insertPos = m_toFile->position(l.line(), l.column());
@@ -6242,7 +6250,7 @@ public:
// construct definition // construct definition
const QString funcDec = inlinePrefix( const QString funcDec = inlinePrefix(
m_toFile->fileName(), [this] { return m_type == MoveOutside; }) m_toFile->filePath().toString(), [this] { return m_type == MoveOutside; })
+ definitionSignature(m_operation, funcAST, m_fromFile, m_toFile, + definitionSignature(m_operation, funcAST, m_fromFile, m_toFile,
scopeAtInsertPos); scopeAtInsertPos);
QString funcDef = prefix + funcDec; QString funcDef = prefix + funcDec;
@@ -6495,8 +6503,8 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr fromFile = refactoring.file(m_fromFileName); CppRefactoringFilePtr fromFile = refactoring.file(Utils::FilePath::fromString(m_fromFileName));
CppRefactoringFilePtr toFile = refactoring.file(m_toFileName); CppRefactoringFilePtr toFile = refactoring.file(Utils::FilePath::fromString(m_toFileName));
const QString wholeFunctionText = m_declarationText const QString wholeFunctionText = m_declarationText
+ fromFile->textOf(fromFile->endOf(m_funcAST->declarator), + fromFile->textOf(fromFile->endOf(m_funcAST->declarator),
@@ -6558,7 +6566,7 @@ void MoveFuncDefToDecl::match(const CppQuickFixInterface &interface, QuickFixOpe
return; return;
const CppRefactoringChanges refactoring(interface.snapshot()); const CppRefactoringChanges refactoring(interface.snapshot());
const CppRefactoringFilePtr defFile = refactoring.file(interface.filePath().toString()); const CppRefactoringFilePtr defFile = refactoring.file(interface.filePath());
const ChangeSet::Range defRange = defFile->range(completeDefAST); const ChangeSet::Range defRange = defFile->range(completeDefAST);
// Determine declaration (file, range, text); // Determine declaration (file, range, text);
@@ -6592,7 +6600,7 @@ void MoveFuncDefToDecl::match(const CppQuickFixInterface &interface, QuickFixOpe
declFileName = QString::fromUtf8(matchingClass->fileName(), declFileName = QString::fromUtf8(matchingClass->fileName(),
matchingClass->fileNameLength()); matchingClass->fileNameLength());
const CppRefactoringFilePtr declFile = refactoring.file(declFileName); const CppRefactoringFilePtr declFile = refactoring.file(Utils::FilePath::fromString(declFileName));
ASTPath astPath(declFile->cppDocument()); ASTPath astPath(declFile->cppDocument());
const QList<AST *> path = astPath(s->line(), s->column()); const QList<AST *> path = astPath(s->line(), s->column());
for (int idx = path.size() - 1; idx > 0; --idx) { for (int idx = path.size() - 1; idx > 0; --idx) {
@@ -6617,7 +6625,8 @@ void MoveFuncDefToDecl::match(const CppQuickFixInterface &interface, QuickFixOpe
if (isHeaderFile) if (isHeaderFile)
return; return;
const CppRefactoringFilePtr declFile = refactoring.file(declFileName); const CppRefactoringFilePtr declFile = refactoring.file(
Utils::FilePath::fromString(declFileName));
const LookupContext lc(declFile->cppDocument(), interface.snapshot()); const LookupContext lc(declFile->cppDocument(), interface.snapshot());
const QList<LookupItem> candidates = lc.lookup(func->name(), matchingNamespace); const QList<LookupItem> candidates = lc.lookup(func->name(), matchingNamespace);
for (const LookupItem &candidate : candidates) { for (const LookupItem &candidate : candidates) {
@@ -6669,7 +6678,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr file = refactoring.file(filePath().toString()); CppRefactoringFilePtr file = refactoring.file(filePath());
// Determine return type and new variable name // Determine return type and new variable name
TypeOfExpression typeOfExpression; TypeOfExpression typeOfExpression;
@@ -6878,9 +6887,9 @@ public:
{ {
QTC_ASSERT(m_forAst, return); QTC_ASSERT(m_forAst, return);
const QString filename = currentFile()->fileName(); const Utils::FilePath filePath = currentFile()->filePath();
const CppRefactoringChanges refactoring(snapshot()); const CppRefactoringChanges refactoring(snapshot());
const CppRefactoringFilePtr file = refactoring.file(filename); const CppRefactoringFilePtr file = refactoring.file(filePath);
ChangeSet change; ChangeSet change;
// Optimize post (in|de)crement operator to pre (in|de)crement operator // Optimize post (in|de)crement operator to pre (in|de)crement operator
@@ -7134,7 +7143,7 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
const int startPos = currentFile->startOf(m_literal); const int startPos = currentFile->startOf(m_literal);
const int endPos = currentFile->endOf(m_literal); const int endPos = currentFile->endOf(m_literal);
@@ -7223,7 +7232,7 @@ private:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
currentFile->setChangeSet(m_changes); currentFile->setChangeSet(m_changes);
currentFile->apply(); currentFile->apply();
} }
@@ -7975,7 +7984,8 @@ private:
while (!nodesWithProcessedParents.empty()) { while (!nodesWithProcessedParents.empty()) {
Node &node = nodesWithProcessedParents.back(); Node &node = nodesWithProcessedParents.back();
nodesWithProcessedParents.pop_back(); nodesWithProcessedParents.pop_back();
CppRefactoringFilePtr file = refactoring.file(node.document->fileName()); CppRefactoringFilePtr file = refactoring.file(
Utils::FilePath::fromString(node.document->fileName()));
const bool parentHasUsing = Utils::anyOf(node.includes, &Node::hasGlobalUsingDirective); const bool parentHasUsing = Utils::anyOf(node.includes, &Node::hasGlobalUsingDirective);
const int startPos = parentHasUsing const int startPos = parentHasUsing
? 0 ? 0
@@ -7994,7 +8004,7 @@ private:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.file(filePath().toString()); CppRefactoringFilePtr currentFile = refactoring.file(filePath());
if (m_removeAllAtGlobalScope) { if (m_removeAllAtGlobalScope) {
removeAllUsingsAtGlobalScope(refactoring); removeAllUsingsAtGlobalScope(refactoring);
} else if (refactorFile(currentFile, } else if (refactorFile(currentFile,
@@ -8047,7 +8057,8 @@ private:
if (m_processed.contains(loc.first)) if (m_processed.contains(loc.first))
continue; continue;
CppRefactoringFilePtr file = refactoring.file(loc.first->fileName()); CppRefactoringFilePtr file = refactoring.file(
Utils::FilePath::fromString(loc.first->fileName()));
const bool noGlobalUsing = refactorFile(file, const bool noGlobalUsing = refactorFile(file,
refactoring.snapshot(), refactoring.snapshot(),
file->position(loc.second, 1)); file->position(loc.second, 1));
@@ -8866,7 +8877,7 @@ private:
false, false,
NamespaceHandling::Ignore, NamespaceHandling::Ignore,
m_changes, m_changes,
m_headerFile->fileName(), m_headerFile->filePath().toString(),
&insertedNamespaces); &insertedNamespaces);
implFile = m_headerFile; implFile = m_headerFile;
implCode = symbolAt(m_class, m_headerFile, implLoc); implCode = symbolAt(m_class, m_headerFile, implLoc);
@@ -8974,7 +8985,7 @@ private:
} }
}; };
GenerateConstructorRefactoringHelper helper(this, GenerateConstructorRefactoringHelper helper(this,
currentFile()->fileName(), currentFile()->filePath().toString(),
m_classAST->symbol, m_classAST->symbol,
m_classAST, m_classAST,
accessSpec); accessSpec);

View File

@@ -1465,7 +1465,8 @@ void CppModelManager::renameIncludes(const QString &oldFileName, const QString &
const TextEditor::RefactoringChanges changes; const TextEditor::RefactoringChanges changes;
foreach (Snapshot::IncludeLocation loc, snapshot().includeLocationsOfDocument(oldFileName)) { foreach (Snapshot::IncludeLocation loc, snapshot().includeLocationsOfDocument(oldFileName)) {
TextEditor::RefactoringFilePtr file = changes.file(loc.first->fileName()); TextEditor::RefactoringFilePtr file = changes.file(
Utils::FilePath::fromString(loc.first->fileName()));
const QTextBlock &block = file->document()->findBlockByNumber(loc.second - 1); const QTextBlock &block = file->document()->findBlockByNumber(loc.second - 1);
const int replaceStart = block.text().indexOf(oldFileInfo.fileName()); const int replaceStart = block.text().indexOf(oldFileInfo.fileName());
if (replaceStart > -1) { if (replaceStart > -1) {

View File

@@ -47,13 +47,13 @@ namespace CppTools {
class CppRefactoringChangesData : public TextEditor::RefactoringChangesData class CppRefactoringChangesData : public TextEditor::RefactoringChangesData
{ {
static std::unique_ptr<TextEditor::Indenter> createIndenter(const QString &fileName, static std::unique_ptr<TextEditor::Indenter> createIndenter(const Utils::FilePath &filePath,
QTextDocument *textDocument) QTextDocument *textDocument)
{ {
TextEditor::ICodeStylePreferencesFactory *factory TextEditor::ICodeStylePreferencesFactory *factory
= TextEditor::TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID); = TextEditor::TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID);
std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(textDocument)); std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(textDocument));
indenter->setFileName(Utils::FilePath::fromString(fileName)); indenter->setFileName(filePath);
return indenter; return indenter;
} }
@@ -65,34 +65,36 @@ public:
{} {}
void indentSelection(const QTextCursor &selection, void indentSelection(const QTextCursor &selection,
const QString &fileName, const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const override const TextEditor::TextDocument *textDocument) const override
{ {
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings()); textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings());
} else { } else {
const auto &tabSettings = ProjectExplorer::actualTabSettings(fileName, textDocument); const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath.toString(),
auto indenter = createIndenter(fileName, selection.document()); textDocument);
auto indenter = createIndenter(filePath, selection.document());
indenter->indent(selection, QChar::Null, tabSettings); indenter->indent(selection, QChar::Null, tabSettings);
} }
} }
void reindentSelection(const QTextCursor &selection, void reindentSelection(const QTextCursor &selection,
const QString &fileName, const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const override const TextEditor::TextDocument *textDocument) const override
{ {
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
textDocument->indenter()->reindent(selection, textDocument->tabSettings()); textDocument->indenter()->reindent(selection, textDocument->tabSettings());
} else { } else {
const auto &tabSettings = ProjectExplorer::actualTabSettings(fileName, textDocument); const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath.toString(),
auto indenter = createIndenter(fileName, selection.document()); textDocument);
auto indenter = createIndenter(filePath, selection.document());
indenter->reindent(selection, tabSettings); indenter->reindent(selection, tabSettings);
} }
} }
void fileChanged(const QString &fileName) override void fileChanged(const Utils::FilePath &filePath) override
{ {
m_modelManager->updateSourceFiles(QSet<QString>() << fileName); m_modelManager->updateSourceFiles({filePath.toString()});
} }
Snapshot m_snapshot; Snapshot m_snapshot;
@@ -118,18 +120,19 @@ CppRefactoringFilePtr CppRefactoringChanges::file(TextEditor::TextEditorWidget *
return result; return result;
} }
CppRefactoringFilePtr CppRefactoringChanges::file(const QString &fileName) const CppRefactoringFilePtr CppRefactoringChanges::file(const Utils::FilePath &filePath) const
{ {
CppRefactoringFilePtr result(new CppRefactoringFile(fileName, m_data)); CppRefactoringFilePtr result(new CppRefactoringFile(filePath, m_data));
return result; return result;
} }
CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const QString &fileName) const CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const Utils::FilePath &filePath) const
{ {
QTextDocument *document = nullptr; QTextDocument *document = nullptr;
const QString fileName = filePath.toString();
if (data()->m_workingCopy.contains(fileName)) if (data()->m_workingCopy.contains(fileName))
document = new QTextDocument(QString::fromUtf8(data()->m_workingCopy.source(fileName))); document = new QTextDocument(QString::fromUtf8(data()->m_workingCopy.source(fileName)));
CppRefactoringFilePtr result(new CppRefactoringFile(document, fileName)); CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath));
result->m_data = m_data; result->m_data = m_data;
return result; return result;
@@ -140,15 +143,15 @@ const Snapshot &CppRefactoringChanges::snapshot() const
return data()->m_snapshot; return data()->m_snapshot;
} }
CppRefactoringFile::CppRefactoringFile(const QString &fileName, const QSharedPointer<TextEditor::RefactoringChangesData> &data) CppRefactoringFile::CppRefactoringFile(const Utils::FilePath &filePath, const QSharedPointer<TextEditor::RefactoringChangesData> &data)
: RefactoringFile(fileName, data) : RefactoringFile(filePath, data)
{ {
const Snapshot &snapshot = this->data()->m_snapshot; const Snapshot &snapshot = this->data()->m_snapshot;
m_cppDocument = snapshot.document(fileName); m_cppDocument = snapshot.document(filePath.toString());
} }
CppRefactoringFile::CppRefactoringFile(QTextDocument *document, const QString &fileName) CppRefactoringFile::CppRefactoringFile(QTextDocument *document, const Utils::FilePath &filePath)
: RefactoringFile(document, fileName) : RefactoringFile(document, filePath)
{ } { }
CppRefactoringFile::CppRefactoringFile(TextEditor::TextEditorWidget *editor) CppRefactoringFile::CppRefactoringFile(TextEditor::TextEditorWidget *editor)
@@ -160,7 +163,7 @@ Document::Ptr CppRefactoringFile::cppDocument() const
if (!m_cppDocument || !m_cppDocument->translationUnit() || if (!m_cppDocument || !m_cppDocument->translationUnit() ||
!m_cppDocument->translationUnit()->ast()) { !m_cppDocument->translationUnit()->ast()) {
const QByteArray source = document()->toPlainText().toUtf8(); const QByteArray source = document()->toPlainText().toUtf8();
const QString name = fileName(); const QString name = filePath().toString();
const Snapshot &snapshot = data()->m_snapshot; const Snapshot &snapshot = data()->m_snapshot;
m_cppDocument = snapshot.preprocessedDocument(source, name); m_cppDocument = snapshot.preprocessedDocument(source, name);

View File

@@ -67,8 +67,8 @@ public:
QString textOf(const CPlusPlus::AST *ast) const; QString textOf(const CPlusPlus::AST *ast) const;
protected: protected:
CppRefactoringFile(const QString &fileName, const QSharedPointer<TextEditor::RefactoringChangesData> &data); CppRefactoringFile(const Utils::FilePath &filePath, const QSharedPointer<TextEditor::RefactoringChangesData> &data);
CppRefactoringFile(QTextDocument *document, const QString &fileName); CppRefactoringFile(QTextDocument *document, const Utils::FilePath &filePath);
explicit CppRefactoringFile(TextEditor::TextEditorWidget *editor); explicit CppRefactoringFile(TextEditor::TextEditorWidget *editor);
CppRefactoringChangesData *data() const; CppRefactoringChangesData *data() const;
@@ -86,9 +86,9 @@ public:
static CppRefactoringFilePtr file(TextEditor::TextEditorWidget *editor, static CppRefactoringFilePtr file(TextEditor::TextEditorWidget *editor,
const CPlusPlus::Document::Ptr &document); const CPlusPlus::Document::Ptr &document);
CppRefactoringFilePtr file(const QString &fileName) const; CppRefactoringFilePtr file(const Utils::FilePath &filePath) const;
// safe to use from non-gui threads // safe to use from non-gui threads
CppRefactoringFileConstPtr fileNoEditor(const QString &fileName) const; CppRefactoringFileConstPtr fileNoEditor(const Utils::FilePath &filePath) const;
const CPlusPlus::Snapshot &snapshot() const; const CPlusPlus::Snapshot &snapshot() const;

View File

@@ -276,7 +276,8 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass(
AccessSpec xsSpec, AccessSpec xsSpec,
ForceAccessSpec forceAccessSpec) const ForceAccessSpec forceAccessSpec) const
{ {
const Document::Ptr doc = m_refactoringChanges.file(fileName)->cppDocument(); const Document::Ptr doc = m_refactoringChanges.file(Utils::FilePath::fromString(fileName))
->cppDocument();
if (doc) { if (doc) {
FindInClass find(doc->translationUnit(), clazz); FindInClass find(doc->translationUnit(), clazz);
ClassSpecifierAST *classAST = find(); ClassSpecifierAST *classAST = find();
@@ -620,7 +621,8 @@ static InsertionLocation nextToSurroundingDefinitions(Symbol *declaration,
targetDoc->translationUnit()->getPosition(definitionFunction->endOffset(), &line, &column); targetDoc->translationUnit()->getPosition(definitionFunction->endOffset(), &line, &column);
} else { } else {
// we don't have an offset to the start of the function definition, so we need to manually find it... // we don't have an offset to the start of the function definition, so we need to manually find it...
CppRefactoringFilePtr targetFile = changes.file(QString::fromUtf8(definitionFunction->fileName())); CppRefactoringFilePtr targetFile = changes.file(
Utils::FilePath::fromString(QString::fromUtf8(definitionFunction->fileName())));
if (!targetFile->isValid()) if (!targetFile->isValid())
return noResult; return noResult;
@@ -675,7 +677,8 @@ const QList<InsertionLocation> InsertionPointLocator::methodDefinition(
target = candidate; target = candidate;
} }
CppRefactoringFilePtr targetFile = m_refactoringChanges.file(target); CppRefactoringFilePtr targetFile = m_refactoringChanges.file(
Utils::FilePath::fromString(target));
Document::Ptr doc = targetFile->cppDocument(); Document::Ptr doc = targetFile->cppDocument();
if (doc.isNull()) if (doc.isNull())
return result; return result;
@@ -783,7 +786,7 @@ InsertionLocation insertLocationForMethodDefinition(Symbol *symbol,
{ {
QTC_ASSERT(symbol, return InsertionLocation()); QTC_ASSERT(symbol, return InsertionLocation());
CppRefactoringFilePtr file = refactoring.file(fileName); CppRefactoringFilePtr file = refactoring.file(Utils::FilePath::fromString(fileName));
QStringList requiredNamespaces; QStringList requiredNamespaces;
if (namespaceHandling == NamespaceHandling::CreateMissing) { if (namespaceHandling == NamespaceHandling::CreateMissing) {
requiredNamespaces = getNamespaceNames(symbol); requiredNamespaces = getNamespaceNames(symbol);

View File

@@ -97,7 +97,7 @@ bool applyTextEdits(const DocumentUri &uri, const QList<TextEdit> &edits)
return true; return true;
RefactoringChanges changes; RefactoringChanges changes;
RefactoringFilePtr file; RefactoringFilePtr file;
file = changes.file(uri.toFilePath().toString()); file = changes.file(uri.toFilePath());
file->setChangeSet(editsToChangeSet(edits, file->document())); file->setChangeSet(editsToChangeSet(edits, file->document()));
return file->apply(); return file->apply();
} }

View File

@@ -187,7 +187,8 @@ public:
// stop if we can't create the new file // stop if we can't create the new file
const bool reindent = true; const bool reindent = true;
const bool openEditor = false; const bool openEditor = false;
if (!refactoring.createFile(newFileName, newComponentSource, reindent, openEditor)) const Utils::FilePath newFilePath = Utils::FilePath::fromString(newFileName);
if (!refactoring.createFile(newFilePath, newComponentSource, reindent, openEditor))
return; return;
if (path == QFileInfo(currentFileName).path()) { if (path == QFileInfo(currentFileName).path()) {
@@ -264,7 +265,7 @@ void performComponentFromObjectDef(const QString &fileName, QmlJS::AST::UiObject
{ {
QmlJSRefactoringChanges refactoring(QmlJS::ModelManagerInterface::instance(), QmlJSRefactoringChanges refactoring(QmlJS::ModelManagerInterface::instance(),
QmlJS::ModelManagerInterface::instance()->snapshot()); QmlJS::ModelManagerInterface::instance()->snapshot());
QmlJSRefactoringFilePtr current = refactoring.file(fileName); QmlJSRefactoringFilePtr current = refactoring.file(Utils::FilePath::fromString(fileName));
QmlJSQuickFixInterface interface; QmlJSQuickFixInterface interface;
Operation operation(interface, objDef); Operation operation(interface, objDef);

View File

@@ -56,7 +56,7 @@ void QmlJSQuickFixOperation::perform()
{ {
QmlJSRefactoringChanges refactoring(ModelManagerInterface::instance(), QmlJSRefactoringChanges refactoring(ModelManagerInterface::instance(),
m_interface->semanticInfo().snapshot); m_interface->semanticInfo().snapshot);
QmlJSRefactoringFilePtr current = refactoring.file(fileName()); QmlJSRefactoringFilePtr current = refactoring.file(Utils::FilePath::fromString(fileName()));
performChanges(current, refactoring); performChanges(current, refactoring);
} }

View File

@@ -880,7 +880,8 @@ void QmlOutlineModel::reparentNodes(QmlOutlineItem *targetItem, int row, QList<Q
} }
QmlJSRefactoringChanges refactoring(ModelManagerInterface::instance(), m_semanticInfo.snapshot); QmlJSRefactoringChanges refactoring(ModelManagerInterface::instance(), m_semanticInfo.snapshot);
TextEditor::RefactoringFilePtr file = refactoring.file(m_semanticInfo.document->fileName()); TextEditor::RefactoringFilePtr file = refactoring.file(
Utils::FilePath::fromString(m_semanticInfo.document->fileName()));
file->setChangeSet(changeSet); file->setChangeSet(changeSet);
foreach (const Utils::ChangeSet::Range &range, changedRanges) { foreach (const Utils::ChangeSet::Range &range, changedRanges) {
file->appendIndentRange(range); file->appendIndentRange(range);

View File

@@ -47,7 +47,7 @@ public:
{} {}
void indentSelection(const QTextCursor &selection, void indentSelection(const QTextCursor &selection,
const QString &fileName, const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const override const TextEditor::TextDocument *textDocument) const override
{ {
// ### shares code with QmlJSTextEditor::indent // ### shares code with QmlJSTextEditor::indent
@@ -57,7 +57,7 @@ public:
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next(); const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings = const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textDocument); ProjectExplorer::actualTabSettings(filePath.toString(), textDocument);
CreatorCodeFormatter codeFormatter(tabSettings); CreatorCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
do { do {
@@ -78,19 +78,19 @@ public:
} }
void reindentSelection(const QTextCursor &selection, void reindentSelection(const QTextCursor &selection,
const QString &fileName, const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const override const TextEditor::TextDocument *textDocument) const override
{ {
const TextEditor::TabSettings &tabSettings = const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textDocument); ProjectExplorer::actualTabSettings(filePath.toString(), textDocument);
QmlJSEditor::Internal::Indenter indenter(selection.document()); QmlJSEditor::Internal::Indenter indenter(selection.document());
indenter.reindent(selection, tabSettings); indenter.reindent(selection, tabSettings);
} }
void fileChanged(const QString &fileName) override void fileChanged(const Utils::FilePath &filePath) override
{ {
m_modelManager->updateSourceFiles(QStringList(fileName), true); m_modelManager->updateSourceFiles({filePath.toString()}, true);
} }
ModelManagerInterface *m_modelManager; ModelManagerInterface *m_modelManager;
@@ -103,9 +103,9 @@ QmlJSRefactoringChanges::QmlJSRefactoringChanges(ModelManagerInterface *modelMan
{ {
} }
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file(const QString &fileName) const QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file(const Utils::FilePath &filePath) const
{ {
return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(fileName, m_data)); return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(filePath, m_data));
} }
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file( QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file(
@@ -124,12 +124,13 @@ QmlJSRefactoringChangesData *QmlJSRefactoringChanges::data() const
return static_cast<QmlJSRefactoringChangesData *>(m_data.data()); return static_cast<QmlJSRefactoringChangesData *>(m_data.data());
} }
QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, const QSharedPointer<TextEditor::RefactoringChangesData> &data) QmlJSRefactoringFile::QmlJSRefactoringFile(
: RefactoringFile(fileName, data) const Utils::FilePath &filePath, const QSharedPointer<TextEditor::RefactoringChangesData> &data)
: RefactoringFile(filePath, data)
{ {
// the RefactoringFile is invalid if its not for a file with qml or js code // the RefactoringFile is invalid if its not for a file with qml or js code
if (ModelManagerInterface::guessLanguageOfFile(fileName) == Dialect::NoLanguage) if (ModelManagerInterface::guessLanguageOfFile(filePath.toString()) == Dialect::NoLanguage)
m_fileName.clear(); m_filePath.clear();
} }
QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, Document::Ptr document) QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, Document::Ptr document)
@@ -137,14 +138,14 @@ QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor,
, m_qmljsDocument(document) , m_qmljsDocument(document)
{ {
if (document) if (document)
m_fileName = document->fileName(); m_filePath = Utils::FilePath::fromString(document->fileName());
} }
Document::Ptr QmlJSRefactoringFile::qmljsDocument() const Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
{ {
if (!m_qmljsDocument) { if (!m_qmljsDocument) {
const QString source = document()->toPlainText(); const QString source = document()->toPlainText();
const QString name = fileName(); const QString name = filePath().toString();
const Snapshot &snapshot = data()->m_snapshot; const Snapshot &snapshot = data()->m_snapshot;
Document::MutablePtr newDoc = snapshot.documentFromSource(source, name, Document::MutablePtr newDoc = snapshot.documentFromSource(source, name,

View File

@@ -56,7 +56,8 @@ public:
bool isCursorOn(QmlJS::SourceLocation loc) const; bool isCursorOn(QmlJS::SourceLocation loc) const;
protected: protected:
QmlJSRefactoringFile(const QString &fileName, const QSharedPointer<TextEditor::RefactoringChangesData> &data); QmlJSRefactoringFile(const Utils::FilePath &filePath,
const QSharedPointer<TextEditor::RefactoringChangesData> &data);
QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, QmlJS::Document::Ptr document); QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, QmlJS::Document::Ptr document);
QmlJSRefactoringChangesData *data() const; QmlJSRefactoringChangesData *data() const;
@@ -76,7 +77,7 @@ public:
static QmlJSRefactoringFilePtr file(TextEditor::TextEditorWidget *editor, static QmlJSRefactoringFilePtr file(TextEditor::TextEditorWidget *editor,
const QmlJS::Document::Ptr &document); const QmlJS::Document::Ptr &document);
QmlJSRefactoringFilePtr file(const QString &fileName) const; QmlJSRefactoringFilePtr file(const Utils::FilePath &filePath) const;
const QmlJS::Snapshot &snapshot() const; const QmlJS::Snapshot &snapshot() const;

View File

@@ -519,7 +519,7 @@ QStringList BaseFileFind::replaceAll(const QString &text,
const QList<SearchResultItem> changeItems = it.value(); const QList<SearchResultItem> changeItems = it.value();
ChangeSet changeSet; ChangeSet changeSet;
RefactoringFilePtr file = refactoring.file(fileName); RefactoringFilePtr file = refactoring.file(FilePath::fromString(fileName));
QSet<QPair<int, int> > processed; QSet<QPair<int, int> > processed;
for (const SearchResultItem &item : changeItems) { for (const SearchResultItem &item : changeItems) {
const QPair<int, int> &p = qMakePair(item.mainRange().begin.line, const QPair<int, int> &p = qMakePair(item.mainRange().begin.line,

View File

@@ -76,9 +76,12 @@ RefactoringSelections RefactoringChanges::rangesToSelections(QTextDocument *docu
return selections; return selections;
} }
bool RefactoringChanges::createFile(const QString &fileName, const QString &contents, bool reindent, bool openEditor) const bool RefactoringChanges::createFile(const FilePath &filePath,
const QString &contents,
bool reindent,
bool openEditor) const
{ {
if (QFile::exists(fileName)) if (filePath.exists())
return false; return false;
// Create a text document for the new file: // Create a text document for the new file:
@@ -90,7 +93,7 @@ bool RefactoringChanges::createFile(const QString &fileName, const QString &cont
// Reindent the contents: // Reindent the contents:
if (reindent) { if (reindent) {
cursor.select(QTextCursor::Document); cursor.select(QTextCursor::Document);
m_data->indentSelection(cursor, fileName, nullptr); m_data->indentSelection(cursor, filePath, nullptr);
} }
cursor.endEditBlock(); cursor.endEditBlock();
@@ -98,22 +101,22 @@ bool RefactoringChanges::createFile(const QString &fileName, const QString &cont
TextFileFormat format; TextFileFormat format;
format.codec = EditorManager::defaultTextCodec(); format.codec = EditorManager::defaultTextCodec();
QString error; QString error;
bool saveOk = format.writeFile(Utils::FilePath::fromString(fileName), document->toPlainText(), &error); bool saveOk = format.writeFile(filePath, document->toPlainText(), &error);
delete document; delete document;
if (!saveOk) if (!saveOk)
return false; return false;
m_data->fileChanged(fileName); m_data->fileChanged(filePath);
if (openEditor) if (openEditor)
RefactoringChanges::openEditor(fileName, /*bool activate =*/ false, -1, -1); RefactoringChanges::openEditor(filePath, /*bool activate =*/ false, -1, -1);
return true; return true;
} }
bool RefactoringChanges::removeFile(const QString &fileName) const bool RefactoringChanges::removeFile(const FilePath &filePath) const
{ {
if (!QFile::exists(fileName)) if (!filePath.exists())
return false; return false;
// ### implement! // ### implement!
@@ -121,7 +124,10 @@ bool RefactoringChanges::removeFile(const QString &fileName) const
return true; return true;
} }
TextEditorWidget *RefactoringChanges::openEditor(const QString &fileName, bool activate, int line, int column) TextEditorWidget *RefactoringChanges::openEditor(const FilePath &filePath,
bool activate,
int line,
int column)
{ {
EditorManager::OpenEditorFlags flags = EditorManager::IgnoreNavigationHistory; EditorManager::OpenEditorFlags flags = EditorManager::IgnoreNavigationHistory;
if (activate) if (activate)
@@ -132,7 +138,7 @@ TextEditorWidget *RefactoringChanges::openEditor(const QString &fileName, bool a
// openEditorAt uses a 1-based line and a 0-based column! // openEditorAt uses a 1-based line and a 0-based column!
column -= 1; column -= 1;
} }
IEditor *editor = EditorManager::openEditorAt(fileName, line, column, Id(), flags); IEditor *editor = EditorManager::openEditorAt(Link{filePath, line, column}, Id(), flags);
if (editor) if (editor)
return TextEditorWidget::fromEditor(editor); return TextEditorWidget::fromEditor(editor);
@@ -145,26 +151,27 @@ RefactoringFilePtr RefactoringChanges::file(TextEditorWidget *editor)
return RefactoringFilePtr(new RefactoringFile(editor)); return RefactoringFilePtr(new RefactoringFile(editor));
} }
RefactoringFilePtr RefactoringChanges::file(const QString &fileName) const RefactoringFilePtr RefactoringChanges::file(const FilePath &filePath) const
{ {
return RefactoringFilePtr(new RefactoringFile(fileName, m_data)); return RefactoringFilePtr(new RefactoringFile(filePath, m_data));
} }
RefactoringFile::RefactoringFile(QTextDocument *document, const QString &fileName) RefactoringFile::RefactoringFile(QTextDocument *document, const FilePath &filePath)
: m_fileName(fileName) : m_filePath(filePath)
, m_document(document) , m_document(document)
{ } { }
RefactoringFile::RefactoringFile(TextEditorWidget *editor) RefactoringFile::RefactoringFile(TextEditorWidget *editor)
: m_fileName(editor->textDocument()->filePath().toString()) : m_filePath(editor->textDocument()->filePath())
, m_editor(editor) , m_editor(editor)
{ } { }
RefactoringFile::RefactoringFile(const QString &fileName, const QSharedPointer<RefactoringChangesData> &data) RefactoringFile::RefactoringFile(const FilePath &filePath,
: m_fileName(fileName) const QSharedPointer<RefactoringChangesData> &data)
: m_filePath(filePath)
, m_data(data) , m_data(data)
{ {
QList<IEditor *> editors = DocumentModel::editorsForFilePath(Utils::FilePath::fromString(fileName)); QList<IEditor *> editors = DocumentModel::editorsForFilePath(filePath);
if (!editors.isEmpty()) { if (!editors.isEmpty()) {
auto editorWidget = TextEditorWidget::fromEditor(editors.first()); auto editorWidget = TextEditorWidget::fromEditor(editors.first());
if (editorWidget && !editorWidget->isReadOnly()) if (editorWidget && !editorWidget->isReadOnly())
@@ -179,7 +186,7 @@ RefactoringFile::~RefactoringFile()
bool RefactoringFile::isValid() const bool RefactoringFile::isValid() const
{ {
if (m_fileName.isEmpty()) if (m_filePath.isEmpty())
return false; return false;
return document(); return document();
} }
@@ -195,17 +202,16 @@ QTextDocument *RefactoringFile::mutableDocument() const
return m_editor->document(); return m_editor->document();
if (!m_document) { if (!m_document) {
QString fileContents; QString fileContents;
if (!m_fileName.isEmpty()) { if (!m_filePath.isEmpty()) {
QString error; QString error;
QTextCodec *defaultCodec = EditorManager::defaultTextCodec(); QTextCodec *defaultCodec = EditorManager::defaultTextCodec();
TextFileFormat::ReadResult result = TextFileFormat::readFile(FilePath::fromString( TextFileFormat::ReadResult result = TextFileFormat::readFile(m_filePath,
m_fileName),
defaultCodec, defaultCodec,
&fileContents, &fileContents,
&m_textFileFormat, &m_textFileFormat,
&error); &error);
if (result != TextFileFormat::ReadSuccess) { if (result != TextFileFormat::ReadSuccess) {
qWarning() << "Could not read " << m_fileName << ". Error: " << error; qWarning() << "Could not read " << m_filePath << ". Error: " << error;
m_textFileFormat.codec = nullptr; m_textFileFormat.codec = nullptr;
} }
} }
@@ -219,7 +225,7 @@ const QTextCursor RefactoringFile::cursor() const
{ {
if (m_editor) if (m_editor)
return m_editor->textCursor(); return m_editor->textCursor();
if (!m_fileName.isEmpty()) { if (!m_filePath.isEmpty()) {
if (QTextDocument *doc = mutableDocument()) if (QTextDocument *doc = mutableDocument())
return QTextCursor(doc); return QTextCursor(doc);
} }
@@ -227,9 +233,9 @@ const QTextCursor RefactoringFile::cursor() const
return QTextCursor(); return QTextCursor();
} }
QString RefactoringFile::fileName() const FilePath RefactoringFile::filePath() const
{ {
return m_fileName; return m_filePath;
} }
TextEditorWidget *RefactoringFile::editor() const TextEditorWidget *RefactoringFile::editor() const
@@ -284,7 +290,7 @@ ChangeSet RefactoringFile::changeSet() const
void RefactoringFile::setChangeSet(const ChangeSet &changeSet) void RefactoringFile::setChangeSet(const ChangeSet &changeSet)
{ {
if (m_fileName.isEmpty()) if (m_filePath.isEmpty())
return; return;
m_changes = changeSet; m_changes = changeSet;
@@ -292,7 +298,7 @@ void RefactoringFile::setChangeSet(const ChangeSet &changeSet)
void RefactoringFile::appendIndentRange(const Range &range) void RefactoringFile::appendIndentRange(const Range &range)
{ {
if (m_fileName.isEmpty()) if (m_filePath.isEmpty())
return; return;
m_indentRanges.append(range); m_indentRanges.append(range);
@@ -300,7 +306,7 @@ void RefactoringFile::appendIndentRange(const Range &range)
void RefactoringFile::appendReindentRange(const Range &range) void RefactoringFile::appendReindentRange(const Range &range)
{ {
if (m_fileName.isEmpty()) if (m_filePath.isEmpty())
return; return;
m_reindentRanges.append(range); m_reindentRanges.append(range);
@@ -316,8 +322,8 @@ void RefactoringFile::setOpenEditor(bool activate, int pos)
bool RefactoringFile::apply() bool RefactoringFile::apply()
{ {
// test file permissions // test file permissions
if (!QFileInfo(fileName()).isWritable()) { if (!m_filePath.toFileInfo().isWritable()) {
ReadOnlyFilesDialog roDialog(FilePath::fromString(fileName()), ICore::dialogParent()); ReadOnlyFilesDialog roDialog(m_filePath, ICore::dialogParent());
const QString &failDetailText = QApplication::translate("RefactoringFile::apply", const QString &failDetailText = QApplication::translate("RefactoringFile::apply",
"Refactoring cannot be applied."); "Refactoring cannot be applied.");
roDialog.setShowFailWarning(true, failDetailText); roDialog.setShowFailWarning(true, failDetailText);
@@ -326,11 +332,11 @@ bool RefactoringFile::apply()
} }
// open / activate / goto position // open / activate / goto position
if (m_openEditor && !m_fileName.isEmpty()) { if (m_openEditor && !m_filePath.isEmpty()) {
int line = -1, column = -1; int line = -1, column = -1;
if (m_editorCursorPosition != -1) if (m_editorCursorPosition != -1)
lineAndColumn(m_editorCursorPosition, &line, &column); lineAndColumn(m_editorCursorPosition, &line, &column);
m_editor = RefactoringChanges::openEditor(m_fileName, m_activateEditor, line, column); m_editor = RefactoringChanges::openEditor(m_filePath, m_activateEditor, line, column);
m_openEditor = false; m_openEditor = false;
m_activateEditor = false; m_activateEditor = false;
m_editorCursorPosition = -1; m_editorCursorPosition = -1;
@@ -370,14 +376,15 @@ bool RefactoringFile::apply()
// if this document doesn't have an editor, write the result to a file // if this document doesn't have an editor, write the result to a file
if (!m_editor && m_textFileFormat.codec) { if (!m_editor && m_textFileFormat.codec) {
QTC_ASSERT(!m_fileName.isEmpty(), return false); QTC_ASSERT(!m_filePath.isEmpty(), return false);
QString error; QString error;
// suppress "file has changed" warnings if the file is open in a read-only editor // suppress "file has changed" warnings if the file is open in a read-only editor
Core::FileChangeBlocker block(m_fileName); Core::FileChangeBlocker block(m_filePath.toString());
if (!m_textFileFormat.writeFile(FilePath::fromString(m_fileName), if (!m_textFileFormat.writeFile(m_filePath,
doc->toPlainText(), doc->toPlainText(),
&error)) { &error)) {
qWarning() << "Could not apply changes to" << m_fileName << ". Error: " << error; qWarning() << "Could not apply changes to" << m_filePath
<< ". Error: " << error;
result = false; result = false;
} }
} }
@@ -398,31 +405,35 @@ void RefactoringFile::indentOrReindent(const RefactoringSelections &ranges,
QTextCursor selection(anchor); QTextCursor selection(anchor);
selection.setPosition(position.position(), QTextCursor::KeepAnchor); selection.setPosition(position.position(), QTextCursor::KeepAnchor);
if (indent == Indent) if (indent == Indent)
m_data->indentSelection(selection, m_fileName, document); m_data->indentSelection(selection, m_filePath, document);
else else
m_data->reindentSelection(selection, m_fileName, document); m_data->reindentSelection(selection, m_filePath, document);
} }
} }
void RefactoringFile::fileChanged() void RefactoringFile::fileChanged()
{ {
if (!m_fileName.isEmpty()) if (!m_filePath.isEmpty())
m_data->fileChanged(m_fileName); m_data->fileChanged(m_filePath);
} }
RefactoringChangesData::~RefactoringChangesData() = default; RefactoringChangesData::~RefactoringChangesData() = default;
void RefactoringChangesData::indentSelection(const QTextCursor &, const QString &, const TextDocument *) const void RefactoringChangesData::indentSelection(const QTextCursor &,
const FilePath &,
const TextDocument *) const
{ {
qWarning() << Q_FUNC_INFO << "not implemented"; qWarning() << Q_FUNC_INFO << "not implemented";
} }
void RefactoringChangesData::reindentSelection(const QTextCursor &, const QString &, const TextDocument *) const void RefactoringChangesData::reindentSelection(const QTextCursor &,
const FilePath &,
const TextDocument *) const
{ {
qWarning() << Q_FUNC_INFO << "not implemented"; qWarning() << Q_FUNC_INFO << "not implemented";
} }
void RefactoringChangesData::fileChanged(const QString &) void RefactoringChangesData::fileChanged(const FilePath &)
{ {
} }

View File

@@ -25,14 +25,15 @@
#pragma once #pragma once
#include <utils/changeset.h>
#include <utils/textfileformat.h>
#include <texteditor/texteditor_global.h> #include <texteditor/texteditor_global.h>
#include <utils/changeset.h>
#include <utils/fileutils.h>
#include <utils/textfileformat.h>
#include <QList> #include <QList>
#include <QString>
#include <QSharedPointer>
#include <QPair> #include <QPair>
#include <QSharedPointer>
#include <QString>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QTextDocument; class QTextDocument;
@@ -54,7 +55,6 @@ class TEXTEDITOR_EXPORT RefactoringFile
public: public:
using Range = Utils::ChangeSet::Range; using Range = Utils::ChangeSet::Range;
public:
virtual ~RefactoringFile(); virtual ~RefactoringFile();
bool isValid() const; bool isValid() const;
@@ -62,7 +62,7 @@ public:
const QTextDocument *document() const; const QTextDocument *document() const;
// mustn't use the cursor to change the document // mustn't use the cursor to change the document
const QTextCursor cursor() const; const QTextCursor cursor() const;
QString fileName() const; Utils::FilePath filePath() const;
TextEditorWidget *editor() const; TextEditorWidget *editor() const;
// converts 1-based line and column into 0-based source offset // converts 1-based line and column into 0-based source offset
@@ -84,10 +84,11 @@ public:
protected: protected:
// users may only get const access to RefactoringFiles created through // users may only get const access to RefactoringFiles created through
// this constructor, because it can't be used to apply changes // this constructor, because it can't be used to apply changes
RefactoringFile(QTextDocument *document, const QString &fileName); RefactoringFile(QTextDocument *document, const Utils::FilePath &filePath);
RefactoringFile(TextEditorWidget *editor); RefactoringFile(TextEditorWidget *editor);
RefactoringFile(const QString &fileName, const QSharedPointer<RefactoringChangesData> &data); RefactoringFile(const Utils::FilePath &filePath,
const QSharedPointer<RefactoringChangesData> &data);
QTextDocument *mutableDocument() const; QTextDocument *mutableDocument() const;
// derived classes may want to clear language specific extra data // derived classes may want to clear language specific extra data
@@ -96,8 +97,7 @@ protected:
enum IndentType {Indent, Reindent}; enum IndentType {Indent, Reindent};
void indentOrReindent(const RefactoringSelections &ranges, IndentType indent); void indentOrReindent(const RefactoringSelections &ranges, IndentType indent);
protected: Utils::FilePath m_filePath;
QString m_fileName;
QSharedPointer<RefactoringChangesData> m_data; QSharedPointer<RefactoringChangesData> m_data;
mutable Utils::TextFileFormat m_textFileFormat; mutable Utils::TextFileFormat m_textFileFormat;
mutable QTextDocument *m_document = nullptr; mutable QTextDocument *m_document = nullptr;
@@ -122,22 +122,27 @@ class TEXTEDITOR_EXPORT RefactoringChanges
public: public:
using Range = Utils::ChangeSet::Range; using Range = Utils::ChangeSet::Range;
public:
RefactoringChanges(); RefactoringChanges();
virtual ~RefactoringChanges(); virtual ~RefactoringChanges();
static RefactoringFilePtr file(TextEditorWidget *editor); static RefactoringFilePtr file(TextEditorWidget *editor);
RefactoringFilePtr file(const QString &fileName) const; RefactoringFilePtr file(const Utils::FilePath &filePath) const;
bool createFile(const QString &fileName, const QString &contents, bool reindent = true, bool openEditor = true) const; bool createFile(const Utils::FilePath &filePath,
bool removeFile(const QString &fileName) const; const QString &contents,
bool reindent = true,
bool openEditor = true) const;
bool removeFile(const Utils::FilePath &filePath) const;
protected: protected:
explicit RefactoringChanges(RefactoringChangesData *data); explicit RefactoringChanges(RefactoringChangesData *data);
static TextEditorWidget *openEditor(const QString &fileName, bool activate, int line, int column); static TextEditorWidget *openEditor(const Utils::FilePath &filePath,
static RefactoringSelections rangesToSelections(QTextDocument *document, const QList<Range> &ranges); bool activate,
int line,
int column);
static RefactoringSelections rangesToSelections(QTextDocument *document,
const QList<Range> &ranges);
protected:
QSharedPointer<RefactoringChangesData> m_data; QSharedPointer<RefactoringChangesData> m_data;
friend class RefactoringFile; friend class RefactoringFile;
@@ -152,12 +157,12 @@ public:
virtual ~RefactoringChangesData(); virtual ~RefactoringChangesData();
virtual void indentSelection(const QTextCursor &selection, virtual void indentSelection(const QTextCursor &selection,
const QString &fileName, const Utils::FilePath &filePath,
const TextDocument *textEditor) const; const TextDocument *textEditor) const;
virtual void reindentSelection(const QTextCursor &selection, virtual void reindentSelection(const QTextCursor &selection,
const QString &fileName, const Utils::FilePath &filePath,
const TextDocument *textEditor) const; const TextDocument *textEditor) const;
virtual void fileChanged(const QString &fileName); virtual void fileChanged(const Utils::FilePath &filePath);
}; };
} // namespace TextEditor } // namespace TextEditor

View File

@@ -532,7 +532,7 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet)
if (changeSet.isEmpty()) if (changeSet.isEmpty())
return true; return true;
RefactoringChanges changes; RefactoringChanges changes;
const RefactoringFilePtr file = changes.file(filePath().toString()); const RefactoringFilePtr file = changes.file(filePath());
file->setChangeSet(changeSet); file->setChangeSet(changeSet);
return file->apply(); return file->apply();
} }

View File

@@ -109,9 +109,10 @@ public:
RefactoringChanges() {} RefactoringChanges() {}
virtual ~RefactoringChanges() {} virtual ~RefactoringChanges() {}
RefactoringFilePtr file(const QString &filePath) const RefactoringFilePtr file(const Utils::FilePath &filePath) const
{ {
return RefactoringFilePtr(new RefactoringFile(std::unique_ptr<QTextDocument>(new QTextDocument(readFile(filePath))))); return RefactoringFilePtr(new RefactoringFile(
std::unique_ptr<QTextDocument>(new QTextDocument(readFile(filePath.toString())))));
} }
}; };