TextEditor: Add a convenience overload for RefactoringFile::apply()

... and make use of it.
In most contexts, apply() immediately follows setChangeSet(), so
combining the two can save a lot of code on the call site.

Change-Id: I421001bd47000cb64678a57b19760becf59a4863
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-22 13:31:08 +02:00
parent 708adca904
commit 09f747f7d1
37 changed files with 74 additions and 141 deletions

View File

@@ -71,10 +71,7 @@ QString ClangFixItOperation::firstRefactoringFileContent_forTestOnly() const
void ClangFixItOperation::applyFixitsToFile(TextEditor::RefactoringFile &refactoringFile, void ClangFixItOperation::applyFixitsToFile(TextEditor::RefactoringFile &refactoringFile,
const QList<ClangFixIt> fixIts) const QList<ClangFixIt> fixIts)
{ {
const Utils::ChangeSet changeSet = toChangeSet(refactoringFile, fixIts); refactoringFile.apply(toChangeSet(refactoringFile, fixIts));
refactoringFile.setChangeSet(changeSet);
refactoringFile.apply();
} }
Utils::ChangeSet ClangFixItOperation::toChangeSet( Utils::ChangeSet ClangFixItOperation::toChangeSet(

View File

@@ -328,8 +328,7 @@ void DiagnosticView::suppressCurrentDiagnosticInline()
changeSet.insert(insertStart, newText); changeSet.insert(insertStart, newText);
} }
} }
refactoringFile->setChangeSet(changeSet); refactoringFile->apply(changeSet);
refactoringFile->apply();
} }
filterModel->addSuppressedDiagnostics(diags); filterModel->addSuppressedDiagnostics(diags);

View File

@@ -267,13 +267,11 @@ void FunctionDeclDefLink::apply(CppEditorWidget *editor, bool jumpToMatch)
const int targetStart = newTargetFile->position(targetLine, targetColumn); const int targetStart = newTargetFile->position(targetLine, targetColumn);
const int targetEnd = targetStart + targetInitial.size(); const int targetEnd = targetStart + targetInitial.size();
if (targetInitial == newTargetFile->textOf(targetStart, targetEnd)) { if (targetInitial == newTargetFile->textOf(targetStart, targetEnd)) {
const ChangeSet changeset = changes(snapshot, targetStart);
newTargetFile->setChangeSet(changeset);
if (jumpToMatch) { if (jumpToMatch) {
const int jumpTarget = newTargetFile->position(targetFunction->line(), targetFunction->column()); const int jumpTarget = newTargetFile->position(targetFunction->line(), targetFunction->column());
newTargetFile->setOpenEditor(true, jumpTarget); newTargetFile->setOpenEditor(true, jumpTarget);
} }
newTargetFile->apply(); newTargetFile->apply(changes(snapshot, targetStart));
} else { } else {
ToolTip::show(editor->toolTipPosition(linkSelection), ToolTip::show(editor->toolTipPosition(linkSelection),
Tr::tr("Target file was changed, could not apply changes")); Tr::tr("Target file was changed, could not apply changes"));

View File

@@ -2010,8 +2010,7 @@ void CppModelManager::renameIncludes(const QList<std::pair<FilePath, FilePath>>
newString); newString);
} }
} }
file->setChangeSet(changeSet); file->apply(changeSet);
file->apply();
} }
} }

View File

@@ -54,8 +54,7 @@ private:
varName + QLatin1String(" = ")); varName + QLatin1String(" = "));
ChangeSet changes; ChangeSet changes;
changes.insert(m_insertPos, insertString); changes.insert(m_insertPos, insertString);
m_file->setChangeSet(changes); m_file->apply(changes);
m_file->apply();
// move cursor to new variable name // move cursor to new variable name
QTextCursor c = m_file->cursor(); QTextCursor c = m_file->cursor();

View File

@@ -292,8 +292,7 @@ private:
insertion.append('\n'); insertion.append('\n');
ChangeSet s; ChangeSet s;
s.insert(insertPos, insertion); s.insert(insertPos, insertion);
file->setChangeSet(s); file->apply(s);
file->apply();
} }
const QString m_className; const QString m_className;
@@ -314,8 +313,7 @@ void AddIncludeForUndefinedIdentifierOp::perform()
ChangeSet changes; ChangeSet changes;
insertNewIncludeDirective(m_include, file, semanticInfo().doc, changes); insertNewIncludeDirective(m_include, file, semanticInfo().doc, changes);
file->setChangeSet(changes); file->apply(changes);
file->apply();
} }
AddForwardDeclForUndefinedIdentifierOp::AddForwardDeclForUndefinedIdentifierOp( AddForwardDeclForUndefinedIdentifierOp::AddForwardDeclForUndefinedIdentifierOp(

View File

@@ -87,8 +87,7 @@ public:
changes.insert(start, QLatin1String("\ncase ") changes.insert(start, QLatin1String("\ncase ")
+ values.join(QLatin1String(":\nbreak;\ncase ")) + values.join(QLatin1String(":\nbreak;\ncase "))
+ QLatin1String(":\nbreak;")); + QLatin1String(":\nbreak;"));
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
CompoundStatementAST *compoundStatement; CompoundStatementAST *compoundStatement;

View File

@@ -67,8 +67,7 @@ public:
break; break;
} }
m_file->setChangeSet(changes); m_file->apply(changes);
m_file->apply();
} }
private: private:

View File

@@ -30,8 +30,7 @@ public:
{ {
ChangeSet changes; ChangeSet changes;
changes.replace(start, end, replacement); changes.replace(start, end, replacement);
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:

View File

@@ -35,8 +35,7 @@ public:
private: private:
void perform() override void perform() override
{ {
currentFile()->setChangeSet(m_changes); currentFile()->apply(m_changes);
currentFile()->apply();
} }
const ChangeSet m_changes; const ChangeSet m_changes;

View File

@@ -275,8 +275,7 @@ public:
changes.insert(endPos, "\"" + str + "\""); changes.insert(endPos, "\"" + str + "\"");
replace = false; replace = false;
} }
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:
@@ -352,8 +351,7 @@ public:
changes.insert(startPos, leading); changes.insert(startPos, leading);
} }
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:
@@ -386,8 +384,7 @@ public:
changes.insert(currentFile()->startOf(stringLiteral), QLatin1String("@")); changes.insert(currentFile()->startOf(stringLiteral), QLatin1String("@"));
} }
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:

View File

@@ -55,8 +55,7 @@ private:
if (m_test) { if (m_test) {
ChangeSet changeSet; ChangeSet changeSet;
changeSet.replace(currentFile()->range(m_nameAst), newName); changeSet.replace(currentFile()->range(m_nameAst), newName);
currentFile()->setChangeSet(changeSet); currentFile()->apply(changeSet);
currentFile()->apply();
} else { } else {
editor()->renameUsages(newName); editor()->renameUsages(newName);
} }

View File

@@ -100,8 +100,7 @@ private:
} }
// Apply the changes. // Apply the changes.
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
const CallAST * const m_callAst; const CallAST * const m_callAst;

View File

@@ -307,14 +307,10 @@ public:
insertAndIndent(m_sourceFile, m_sourceFileInsertionPoint, m_sourceFileCode); insertAndIndent(m_sourceFile, m_sourceFileInsertionPoint, m_sourceFileCode);
} }
if (!m_headerFileChangeSet.isEmpty()) { if (!m_headerFileChangeSet.isEmpty())
m_headerFile->setChangeSet(m_headerFileChangeSet); m_headerFile->apply(m_headerFileChangeSet);
m_headerFile->apply(); if (!m_sourceFileChangeSet.isEmpty())
} m_sourceFile->apply(m_sourceFileChangeSet);
if (!m_sourceFileChangeSet.isEmpty()) {
m_sourceFile->setChangeSet(m_sourceFileChangeSet);
m_sourceFile->apply();
}
} }
bool hasSourceFile() const { return m_headerFile != m_sourceFile; } bool hasSourceFile() const { return m_headerFile != m_sourceFile; }

View File

@@ -868,9 +868,8 @@ public:
// Write header file // Write header file
if (!headerChangeSet.isEmpty()) { if (!headerChangeSet.isEmpty()) {
headerFile->setChangeSet(headerChangeSet);
headerFile->setOpenEditor(true, m_insertPosDecl); headerFile->setOpenEditor(true, m_insertPosDecl);
headerFile->apply(); headerFile->apply(headerChangeSet);
} }
// Insert in implementation file // Insert in implementation file
@@ -921,10 +920,8 @@ public:
implementationChangeSet.insert(insertPos, QLatin1String("\n\n") + defText); implementationChangeSet.insert(insertPos, QLatin1String("\n\n") + defText);
} }
if (!implementationChangeSet.isEmpty()) { if (!implementationChangeSet.isEmpty())
implementationFile->setChangeSet(implementationChangeSet); implementationFile->apply(implementationChangeSet);
implementationFile->apply();
}
} }
} }

View File

@@ -113,9 +113,8 @@ public:
ChangeSet target; ChangeSet target;
target.insert(targetPosition, loc.prefix() + m_decl); target.insert(targetPosition, loc.prefix() + m_decl);
targetFile->setChangeSet(target);
targetFile->setOpenEditor(true, targetPosition); targetFile->setOpenEditor(true, targetPosition);
targetFile->apply(); targetFile->apply(target);
} }
static QString generateDeclaration(const Function *function) static QString generateDeclaration(const Function *function)
@@ -206,8 +205,7 @@ private:
const int targetPosition = targetFile->position(loc.line(), loc.column()); const int targetPosition = targetFile->position(loc.line(), loc.column());
ChangeSet target; ChangeSet target;
target.insert(targetPosition, loc.prefix() + decl + ";\n"); target.insert(targetPosition, loc.prefix() + decl + ";\n");
targetFile->setChangeSet(target); targetFile->apply(target);
targetFile->apply();
} }
const Class * const m_class; const Class * const m_class;
@@ -242,8 +240,7 @@ public:
changes.replace(currentFile()->startOf(binaryAST), changes.replace(currentFile()->startOf(binaryAST),
currentFile()->endOf(simpleNameAST), currentFile()->endOf(simpleNameAST),
declaration); declaration);
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
} }

View File

@@ -202,8 +202,7 @@ public:
ChangeSet change; ChangeSet change;
change.insert(position, funcDef); change.insert(position, funcDef);
change.replace(m_extractionStart, m_extractionEnd, funcCall); change.replace(m_extractionStart, m_extractionEnd, funcCall);
currentFile()->setChangeSet(change); currentFile()->apply(change);
currentFile()->apply();
// Write declaration, if necessary. // Write declaration, if necessary.
if (matchingClass) { if (matchingClass) {
@@ -215,8 +214,7 @@ public:
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());
declFile->setChangeSet(change); declFile->apply(change);
declFile->apply();
} }
} }

View File

@@ -201,15 +201,13 @@ public:
ChangeSet declChanges; ChangeSet declChanges;
appendFunctionParameter(functionDeclaration.ast, functionDeclaration.file, &declChanges, appendFunctionParameter(functionDeclaration.ast, functionDeclaration.file, &declChanges,
true); true);
functionDeclaration.file->setChangeSet(declChanges); functionDeclaration.file->apply(declChanges);
functionDeclaration.file->apply();
} else { } else {
appendFunctionParameter(functionDeclaration.ast, currentFile(), &changes, appendFunctionParameter(functionDeclaration.ast, currentFile(), &changes,
true); true);
} }
} }
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
QTextCursor c = currentFile()->cursor(); QTextCursor c = currentFile()->cursor();
c.setPosition(c.position() - parameterName().length()); c.setPosition(c.position() - parameterName().length());
editor()->setTextCursor(c); editor()->setTextCursor(c);

View File

@@ -114,9 +114,8 @@ public:
target->replace(targetPos - 1, targetPos, QLatin1String("\n {\n\n}")); // replace ';' target->replace(targetPos - 1, targetPos, QLatin1String("\n {\n\n}")); // replace ';'
if (!changeSet) { if (!changeSet) {
targetFile->setChangeSet(*target);
targetFile->setOpenEditor(true, targetPos); targetFile->setOpenEditor(true, targetPos);
targetFile->apply(); targetFile->apply(*target);
// Move cursor inside definition // Move cursor inside definition
QTextCursor c = targetFile->cursor(); QTextCursor c = targetFile->cursor();
@@ -190,9 +189,8 @@ public:
target->insert(targetPos, loc.prefix() + defText + loc.suffix()); target->insert(targetPos, loc.prefix() + defText + loc.suffix());
if (!changeSet) { if (!changeSet) {
targetFile->setChangeSet(*target);
targetFile->setOpenEditor(true, targetPos); targetFile->setOpenEditor(true, targetPos);
targetFile->apply(); targetFile->apply(*target);
// Move cursor inside definition // Move cursor inside definition
QTextCursor c = targetFile->cursor(); QTextCursor c = targetFile->cursor();
@@ -454,11 +452,8 @@ private:
this, loc, setting.defPos, finder.decl()->declarator_list->value, this, loc, setting.defPos, finder.decl()->declarator_list->value,
setting.func->asDeclaration(),targetFilePath, &changeSet); setting.func->asDeclaration(),targetFilePath, &changeSet);
} }
for (auto it = changeSets.cbegin(); it != changeSets.cend(); ++it) { for (auto it = changeSets.cbegin(); it != changeSets.cend(); ++it)
const CppRefactoringFilePtr file = refactoring.cppFile(it.key()); refactoring.cppFile(it.key())->apply(it.value());
file->setChangeSet(it.value());
file->apply();
}
} }
ClassSpecifierAST *m_classAST = nullptr; ClassSpecifierAST *m_classAST = nullptr;

View File

@@ -41,8 +41,7 @@ public:
if (!replacement.isEmpty()) if (!replacement.isEmpty())
changes.replace(currentFile()->range(binary->binary_op_token), replacement); changes.replace(currentFile()->range(binary->binary_op_token), replacement);
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:
@@ -94,8 +93,7 @@ public:
changes.insert(currentFile()->endOf(binary), QLatin1String(")")); changes.insert(currentFile()->endOf(binary), QLatin1String(")"));
} }
changes.replace(currentFile()->range(binary->binary_op_token), replacement); changes.replace(currentFile()->range(binary->binary_op_token), replacement);
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:
@@ -134,8 +132,7 @@ public:
changes.insert(start, QLatin1String("!(")); changes.insert(start, QLatin1String("!("));
changes.insert(end, QLatin1String(")")); changes.insert(end, QLatin1String(")"));
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
}; };

View File

@@ -441,8 +441,7 @@ private:
.append('\n'); .append('\n');
changes.remove(rangeToMove); changes.remove(rangeToMove);
} }
refactoringFile->setChangeSet(changes); refactoringFile->apply(changes);
refactoringFile->apply();
} }
if (!namespaceNames.isEmpty()) { if (!namespaceNames.isEmpty()) {
@@ -455,19 +454,15 @@ private:
if (!fileSettings.headerPragmaOnce) if (!fileSettings.headerPragmaOnce)
headerContent.append("\n#endif // " + headerGuard + '\n'); headerContent.append("\n#endif // " + headerGuard + '\n');
CppRefactoringFilePtr headerFile = state->factory.cppFile(headerFilePath);
headerFilePath.ensureExistingFile(); headerFilePath.ensureExistingFile();
ChangeSet headerChanges; ChangeSet headerChanges;
headerChanges.insert(0, headerContent); headerChanges.insert(0, headerContent);
headerFile->setChangeSet(headerChanges); state->factory.cppFile(headerFilePath)->apply(headerChanges);
headerFile->apply();
if (hasSourceContent || mustCreateSourceFile) { if (hasSourceContent || mustCreateSourceFile) {
sourceFilePath.ensureExistingFile(); sourceFilePath.ensureExistingFile();
CppRefactoringFilePtr sourceFile = state->factory.cppFile(sourceFilePath);
ChangeSet sourceChanges; ChangeSet sourceChanges;
sourceChanges.insert(0, sourceContent); sourceChanges.insert(0, sourceContent);
sourceFile->setChangeSet(sourceChanges); state->factory.cppFile(sourceFilePath)->apply(sourceChanges);
sourceFile->apply();
} }
if (!projectNode) if (!projectNode)

View File

@@ -135,14 +135,10 @@ public:
void applyChanges() void applyChanges()
{ {
if (!m_toFileChangeSet.isEmpty()) { if (!m_toFileChangeSet.isEmpty())
m_toFile->setChangeSet(m_toFileChangeSet); m_toFile->apply(m_toFileChangeSet);
m_toFile->apply(); if (!m_fromFileChangeSet.isEmpty())
} m_fromFile->apply(m_fromFileChangeSet);
if (!m_fromFileChangeSet.isEmpty()) {
m_fromFile->setChangeSet(m_fromFileChangeSet);
m_fromFile->apply();
}
} }
private: private:
@@ -278,14 +274,12 @@ private:
toTarget.replace(m_toRange, wholeFunctionText); toTarget.replace(m_toRange, wholeFunctionText);
if (m_toFilePath == m_fromFilePath) if (m_toFilePath == m_fromFilePath)
toTarget.remove(m_fromRange); toTarget.remove(m_fromRange);
toFile->setChangeSet(toTarget);
toFile->setOpenEditor(true, m_toRange.start); toFile->setOpenEditor(true, m_toRange.start);
toFile->apply(); toFile->apply(toTarget);
if (m_toFilePath != m_fromFilePath) { if (m_toFilePath != m_fromFilePath) {
ChangeSet fromTarget; ChangeSet fromTarget;
fromTarget.remove(m_fromRange); fromTarget.remove(m_fromRange);
fromFile->setChangeSet(fromTarget); fromFile->apply(fromTarget);
fromFile->apply();
} }
} }

View File

@@ -41,9 +41,8 @@ public:
Utils::ChangeSet changes; Utils::ChangeSet changes;
changes.flip(currentFile()->startOf(m_currentParam), currentFile()->endOf(m_currentParam), changes.flip(currentFile()->startOf(m_currentParam), currentFile()->endOf(m_currentParam),
currentFile()->startOf(m_targetParam), targetEndPos); currentFile()->startOf(m_targetParam), targetEndPos);
currentFile()->setChangeSet(changes);
currentFile()->setOpenEditor(false, targetEndPos); currentFile()->setOpenEditor(false, targetEndPos);
currentFile()->apply(); currentFile()->apply(changes);
} }
private: private:

View File

@@ -41,8 +41,7 @@ public:
void perform() override void perform() override
{ {
currentFile()->setChangeSet(m_change); currentFile()->apply(m_change);
currentFile()->apply();
} }
private: private:

View File

@@ -360,14 +360,12 @@ private:
targetChangeSet.insert(insertionPos, QString(insertionColumn, ' ')); targetChangeSet.insert(insertionPos, QString(insertionColumn, ' '));
if (targetFile == sourceFile) if (targetFile == sourceFile)
removeAtSource(targetChangeSet); removeAtSource(targetChangeSet);
targetFile->setChangeSet(targetChangeSet); const bool targetFileSuccess = targetFile->apply(targetChangeSet);
const bool targetFileSuccess = targetFile->apply();
if (targetFile == sourceFile || !targetFileSuccess) if (targetFile == sourceFile || !targetFileSuccess)
return; return;
ChangeSet sourceChangeSet; ChangeSet sourceChangeSet;
removeAtSource(sourceChangeSet); removeAtSource(sourceChangeSet);
sourceFile->setChangeSet(sourceChangeSet); sourceFile->apply(sourceChangeSet);
sourceFile->apply();
} }
const Symbol * const m_symbol; const Symbol * const m_symbol;

View File

@@ -179,8 +179,7 @@ public:
changes.move(currentFile()->range(condition), insertPos); changes.move(currentFile()->range(condition), insertPos);
changes.insert(insertPos, QLatin1String(";\n")); changes.insert(insertPos, QLatin1String(";\n"));
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
ASTMatcher matcher; ASTMatcher matcher;
@@ -219,8 +218,7 @@ public:
changes.copy(currentFile()->range(core), insertPos); changes.copy(currentFile()->range(core), insertPos);
changes.insert(insertPos, QLatin1String(";\n")); changes.insert(insertPos, QLatin1String(";\n"));
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
ASTMatcher matcher; ASTMatcher matcher;
@@ -265,8 +263,7 @@ public:
changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression)); changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression));
changes.insert(currentFile()->endOf(pattern), QLatin1String("\n}")); changes.insert(currentFile()->endOf(pattern), QLatin1String("\n}"));
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
void splitOrCondition() const void splitOrCondition() const
@@ -293,8 +290,7 @@ public:
const int lExprEnd = currentFile()->endOf(condition->left_expression); const int lExprEnd = currentFile()->endOf(condition->left_expression);
changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression)); changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression));
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:
@@ -376,8 +372,7 @@ public:
change.replace(exprRange, varName); change.replace(exprRange, varName);
} }
file->setChangeSet(change); file->apply(change);
file->apply();
// Select variable name and trigger symbol rename // Select variable name and trigger symbol rename
if (renamePos != -1) { if (renamePos != -1) {

View File

@@ -67,8 +67,7 @@ public:
prevDeclarator = declarator; prevDeclarator = declarator;
} }
currentFile()->setChangeSet(changes); currentFile()->apply(changes);
currentFile()->apply();
} }
private: private:

View File

@@ -629,8 +629,7 @@ bool QtCreatorIntegration::navigateToSlot(const QString &objectName,
location.line(), location.column()); location.line(), location.column());
ChangeSet changeSet; ChangeSet changeSet;
changeSet.insert(insertionPos, definition); changeSet.insert(insertionPos, definition);
file->setChangeSet(changeSet); file->apply(changeSet);
file->apply();
const int indentationPos = file->document()->toPlainText().indexOf('}', insertionPos) - 1; const int indentationPos = file->document()->toPlainText().indexOf('}', insertionPos) - 1;
QTextCursor cursor(editor->textDocument()->document()); QTextCursor cursor(editor->textDocument()->document());
cursor.setPosition(indentationPos); cursor.setPosition(indentationPos);

View File

@@ -97,8 +97,7 @@ bool applyTextEdits(const Client *client,
if (edits.isEmpty()) if (edits.isEmpty())
return true; return true;
const RefactoringFilePtr file = client->createRefactoringFile(filePath); const RefactoringFilePtr file = client->createRefactoringFile(filePath);
file->setChangeSet(editsToChangeSet(edits, file->document())); return file->apply(editsToChangeSet(edits, file->document()));
return file->apply();
} }
void applyTextEdit(TextDocumentManipulatorInterface &manipulator, void applyTextEdit(TextDocumentManipulatorInterface &manipulator,

View File

@@ -189,8 +189,7 @@ public:
Utils::ChangeSet changes; Utils::ChangeSet changes;
changes.replace(start, end, replacement); changes.replace(start, end, replacement);
currentFile->setChangeSet(changes); currentFile->apply(changes);
currentFile->apply();
Core::IVersionControl *versionControl = Core::VcsManager::findVersionControlForDirectory( Core::IVersionControl *versionControl = Core::VcsManager::findVersionControlForDirectory(
path); path);

View File

@@ -70,8 +70,7 @@ public:
changes.insert(currentFile->startOf(_objectInitializer->rbraceToken), changes.insert(currentFile->startOf(_objectInitializer->rbraceToken),
QLatin1String("\n")); QLatin1String("\n"));
currentFile->setChangeSet(changes); currentFile->apply(changes);
currentFile->apply();
} }
}; };
@@ -120,8 +119,7 @@ public:
Utils::ChangeSet changes; Utils::ChangeSet changes;
const int insertLoc = _message.location.begin() - _message.location.startColumn + 1; const int insertLoc = _message.location.begin() - _message.location.startColumn + 1;
changes.insert(insertLoc, QString::fromLatin1("// %1\n").arg(_message.suppressionString())); changes.insert(insertLoc, QString::fromLatin1("// %1\n").arg(_message.suppressionString()));
currentFile->setChangeSet(changes); currentFile->apply(changes);
currentFile->apply();
} }
}; };

View File

@@ -146,8 +146,7 @@ public:
" id: %2\n" " id: %2\n"
" sourceComponent: %1\n" " sourceComponent: %1\n"
"}\n").arg(componentId, loaderId)); "}\n").arg(componentId, loaderId));
currentFile->setChangeSet(changes); currentFile->apply(changes);
currentFile->apply();
} }
}; };

View File

@@ -897,10 +897,8 @@ void QmlOutlineModel::reparentNodes(QmlOutlineItem *targetItem, int row, QList<Q
changedRanges << range; changedRanges << range;
} }
QmlJSRefactoringChanges refactoring(ModelManagerInterface::instance(), m_semanticInfo.snapshot); QmlJSRefactoringChanges(ModelManagerInterface::instance(), m_semanticInfo.snapshot)
TextEditor::RefactoringFilePtr file = refactoring.file(m_semanticInfo.document->fileName()); .file(m_semanticInfo.document->fileName())->apply(changeSet);
file->setChangeSet(changeSet);
file->apply();
} }
void QmlOutlineModel::moveObjectMember(AST::Node *toMove, void QmlOutlineModel::moveObjectMember(AST::Node *toMove,

View File

@@ -594,8 +594,7 @@ FilePaths BaseFileFind::replaceAll(const QString &text, const SearchResultItems
item.mainRange().end.column + 1); item.mainRange().end.column + 1);
changeSet.replace(start, end, replacement); changeSet.replace(start, end, replacement);
} }
file->setChangeSet(changeSet); file->apply(changeSet);
file->apply();
} }
return changes.keys(); return changes.keys();

View File

@@ -287,6 +287,12 @@ bool RefactoringFile::apply()
return result; return result;
} }
bool RefactoringFile::apply(const Utils::ChangeSet &changeSet)
{
setChangeSet(changeSet);
return apply();
}
void RefactoringFile::setupFormattingRanges(const QList<ChangeSet::EditOp> &replaceList) void RefactoringFile::setupFormattingRanges(const QList<ChangeSet::EditOp> &replaceList)
{ {
QTextDocument * const doc = m_editor ? m_editor->document() : m_document; QTextDocument * const doc = m_editor ? m_editor->document() : m_document;
@@ -359,7 +365,7 @@ void RefactoringFile::doFormatting()
Utils::sort(m_formattingCursors, [](const auto &tc1, const auto &tc2) { Utils::sort(m_formattingCursors, [](const auto &tc1, const auto &tc2) {
return tc1.first.selectionStart() < tc2.first.selectionStart(); return tc1.first.selectionStart() < tc2.first.selectionStart();
}); });
static const QString clangFormatLineRemovalBlocker("// QTC_TEMP"); static const QString clangFormatLineRemovalBlocker("");
for (auto &[formattingCursor, _] : m_formattingCursors) { for (auto &[formattingCursor, _] : m_formattingCursors) {
const QTextBlock firstBlock = document->findBlock(formattingCursor.selectionStart()); const QTextBlock firstBlock = document->findBlock(formattingCursor.selectionStart());
const QTextBlock lastBlock = document->findBlock(formattingCursor.selectionEnd()); const QTextBlock lastBlock = document->findBlock(formattingCursor.selectionEnd());

View File

@@ -60,6 +60,7 @@ public:
void setChangeSet(const Utils::ChangeSet &changeSet); void setChangeSet(const Utils::ChangeSet &changeSet);
void setOpenEditor(bool activate = false, int pos = -1); void setOpenEditor(bool activate = false, int pos = -1);
bool apply(); bool apply();
bool apply(const Utils::ChangeSet &changeSet);
bool create(const QString &contents, bool reindent, bool openInEditor); bool create(const QString &contents, bool reindent, bool openInEditor);
protected: protected:

View File

@@ -522,10 +522,7 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet)
{ {
if (changeSet.isEmpty()) if (changeSet.isEmpty())
return true; return true;
PlainRefactoringFileFactory changes; return PlainRefactoringFileFactory().file(filePath())->apply(changeSet);
const RefactoringFilePtr file = changes.file(filePath());
file->setChangeSet(changeSet);
return file->apply();
} }
// the blocks list must be sorted // the blocks list must be sorted