Utils: Add factory functions for ChangeSet

This greatly simplifies code that produces change sets with a single
EditOp.

Change-Id: If042bb91e5132b7141d88404cfbd3ba12632b52d
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-22 16:05:22 +02:00
parent 63c6ba1ba3
commit 16c346c806
15 changed files with 74 additions and 59 deletions

View File

@@ -100,6 +100,41 @@ void ChangeSet::clear()
m_error = false;
}
ChangeSet ChangeSet::makeReplace(const Range &range, const QString &replacement)
{
ChangeSet c;
c.replace(range, replacement);
return c;
}
ChangeSet ChangeSet::makeReplace(int start, int end, const QString &replacement)
{
ChangeSet c;
c.replace(start, end, replacement);
return c;
}
ChangeSet ChangeSet::makeRemove(const Range &range)
{
ChangeSet c;
c.remove(range);
return c;
}
ChangeSet ChangeSet::makeFlip(int start1, int end1, int start2, int end2)
{
ChangeSet c;
c.flip(start1, end1, start2, end2);
return c;
}
ChangeSet ChangeSet::makeInsert(int pos, const QString &text)
{
ChangeSet c;
c.insert(pos, text);
return c;
}
bool ChangeSet::replace_helper(int pos, int length, const QString &replacement)
{
if (hasOverlap(pos, length))

View File

@@ -76,6 +76,12 @@ public:
void clear();
static ChangeSet makeReplace(const Range &range, const QString &replacement);
static ChangeSet makeReplace(int start, int end, const QString &replacement);
static ChangeSet makeRemove(const Range &range);
static ChangeSet makeFlip(int start1, int end1, int start2, int end2);
static ChangeSet makeInsert(int pos, const QString &text);
bool replace(const Range &range, const QString &replacement);
bool remove(const Range &range);
bool move(const Range &range, int to);

View File

@@ -52,9 +52,7 @@ private:
const QString varName = constructVarName();
const QString insertString = type.replace(type.length() - origNameLength, origNameLength,
varName + QLatin1String(" = "));
ChangeSet changes;
changes.insert(m_insertPos, insertString);
m_file->apply(changes);
m_file->apply(ChangeSet::makeInsert(m_insertPos, insertString));
// move cursor to new variable name
QTextCursor c = m_file->cursor();

View File

@@ -290,9 +290,7 @@ private:
insertion.prepend('\n');
if (file->charAt(insertPos) != QChar::ParagraphSeparator)
insertion.append('\n');
ChangeSet s;
s.insert(insertPos, insertion);
file->apply(s);
file->apply(ChangeSet::makeInsert(insertPos, insertion));
}
const QString m_className;

View File

@@ -82,12 +82,10 @@ public:
void perform() override
{
ChangeSet changes;
int start = currentFile()->endOf(compoundStatement->lbrace_token);
changes.insert(start, QLatin1String("\ncase ")
+ values.join(QLatin1String(":\nbreak;\ncase "))
+ QLatin1String(":\nbreak;"));
currentFile()->apply(changes);
currentFile()->apply(ChangeSet::makeInsert(
currentFile()->endOf(compoundStatement->lbrace_token),
QLatin1String("\ncase ") + values.join(QLatin1String(":\nbreak;\ncase "))
+ QLatin1String(":\nbreak;")));
}
CompoundStatementAST *compoundStatement;

View File

@@ -28,9 +28,7 @@ public:
void perform() override
{
ChangeSet changes;
changes.replace(start, end, replacement);
currentFile()->apply(changes);
currentFile()->apply(ChangeSet::makeReplace(start, end, replacement));
}
private:

View File

@@ -52,14 +52,11 @@ private:
newName[i] = newName.at(i).toUpper();
}
}
if (m_test) {
ChangeSet changeSet;
changeSet.replace(currentFile()->range(m_nameAst), newName);
currentFile()->apply(changeSet);
} else {
if (m_test)
currentFile()->apply(ChangeSet::makeReplace(currentFile()->range(m_nameAst), newName));
else
editor()->renameUsages(newName);
}
}
const QString m_name;
const AST * const m_nameAst;

View File

@@ -202,10 +202,8 @@ private:
QTC_ASSERT(loc.isValid(), return);
CppRefactoringFilePtr targetFile = refactoring.cppFile(filePath);
const int targetPosition = targetFile->position(loc.line(), loc.column());
ChangeSet target;
target.insert(targetPosition, loc.prefix() + decl + ";\n");
targetFile->apply(target);
targetFile->apply(ChangeSet::makeInsert(
targetFile->position(loc.line(), loc.column()), loc.prefix() + decl + ";\n"));
}
const Class * const m_class;
@@ -236,11 +234,10 @@ public:
QString declaration = getDeclaration();
if (!declaration.isEmpty()) {
ChangeSet changes;
changes.replace(currentFile()->startOf(binaryAST),
currentFile()->apply(ChangeSet::makeReplace(
currentFile()->startOf(binaryAST),
currentFile()->endOf(simpleNameAST),
declaration);
currentFile()->apply(changes);
declaration));
}
}

View File

@@ -211,10 +211,9 @@ public:
const InsertionLocation &location =
locator.methodDeclarationInClass(filePath, matchingClass, options.access);
CppRefactoringFilePtr declFile = refactoring.cppFile(filePath);
change.clear();
position = declFile->position(location.line(), location.column());
change.insert(position, location.prefix() + funcDecl + location.suffix());
declFile->apply(change);
declFile->apply(ChangeSet::makeInsert(
declFile->position(location.line(), location.column()),
location.prefix() + funcDecl + location.suffix()));
}
}

View File

@@ -455,14 +455,10 @@ private:
headerContent.append("\n#endif // " + headerGuard + '\n');
headerFilePath.ensureExistingFile();
ChangeSet headerChanges;
headerChanges.insert(0, headerContent);
state->factory.cppFile(headerFilePath)->apply(headerChanges);
state->factory.cppFile(headerFilePath)->apply(ChangeSet::makeInsert(0, headerContent));
if (hasSourceContent || mustCreateSourceFile) {
sourceFilePath.ensureExistingFile();
ChangeSet sourceChanges;
sourceChanges.insert(0, sourceContent);
state->factory.cppFile(sourceFilePath)->apply(sourceChanges);
state->factory.cppFile(sourceFilePath)->apply(ChangeSet::makeInsert(0, sourceContent));
}
if (!projectNode)

View File

@@ -274,11 +274,8 @@ private:
toTarget.remove(m_fromRange);
toFile->setOpenEditor(true, m_toRange.start);
toFile->apply(toTarget);
if (m_toFilePath != m_fromFilePath) {
ChangeSet fromTarget;
fromTarget.remove(m_fromRange);
fromFile->apply(fromTarget);
}
if (m_toFilePath != m_fromFilePath)
fromFile->apply(ChangeSet::makeRemove(m_fromRange));
}
void ensureFuncDefAstAndRange(CppRefactoringFile &defFile)

View File

@@ -38,11 +38,12 @@ public:
void perform() override
{
int targetEndPos = currentFile()->endOf(m_targetParam);
Utils::ChangeSet changes;
changes.flip(currentFile()->startOf(m_currentParam), currentFile()->endOf(m_currentParam),
currentFile()->startOf(m_targetParam), targetEndPos);
currentFile()->setOpenEditor(false, targetEndPos);
currentFile()->apply(changes);
currentFile()->apply(Utils::ChangeSet::makeFlip(
currentFile()->startOf(m_currentParam),
currentFile()->endOf(m_currentParam),
currentFile()->startOf(m_targetParam),
targetEndPos));
}
private:

View File

@@ -627,9 +627,7 @@ bool QtCreatorIntegration::navigateToSlot(const QString &objectName,
const RefactoringFilePtr file = refactoring.file(location.filePath());
const int insertionPos = Utils::Text::positionInText(file->document(),
location.line(), location.column());
ChangeSet changeSet;
changeSet.insert(insertionPos, definition);
file->apply(changeSet);
file->apply(ChangeSet::makeInsert(insertionPos, definition));
const int indentationPos = file->document()->toPlainText().indexOf('}', insertionPos) - 1;
QTextCursor cursor(editor->textDocument()->document());
cursor.setPosition(indentationPos);

View File

@@ -187,9 +187,7 @@ public:
for (const QString &property : std::as_const(result))
replacement += property + QLatin1String(": ") + propertyReader.readAstValue(property) + QLatin1Char('\n');
Utils::ChangeSet changes;
changes.replace(start, end, replacement);
currentFile->apply(changes);
currentFile->apply(ChangeSet::makeReplace(start, end, replacement));
Core::IVersionControl *versionControl = Core::VcsManager::findVersionControlForDirectory(
path);

View File

@@ -116,10 +116,9 @@ public:
const QmlJSRefactoringChanges &,
const QString &) override
{
Utils::ChangeSet changes;
const int insertLoc = _message.location.begin() - _message.location.startColumn + 1;
changes.insert(insertLoc, QString::fromLatin1("// %1\n").arg(_message.suppressionString()));
currentFile->apply(changes);
currentFile->apply(Utils::ChangeSet::makeInsert(
_message.location.begin() - _message.location.startColumn + 1,
QString::fromLatin1("// %1\n").arg(_message.suppressionString())));
}
};