forked from qt-creator/qt-creator
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:
@@ -100,6 +100,41 @@ void ChangeSet::clear()
|
|||||||
m_error = false;
|
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)
|
bool ChangeSet::replace_helper(int pos, int length, const QString &replacement)
|
||||||
{
|
{
|
||||||
if (hasOverlap(pos, length))
|
if (hasOverlap(pos, length))
|
||||||
|
|||||||
@@ -76,6 +76,12 @@ public:
|
|||||||
|
|
||||||
void clear();
|
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 replace(const Range &range, const QString &replacement);
|
||||||
bool remove(const Range &range);
|
bool remove(const Range &range);
|
||||||
bool move(const Range &range, int to);
|
bool move(const Range &range, int to);
|
||||||
|
|||||||
@@ -52,9 +52,7 @@ private:
|
|||||||
const QString varName = constructVarName();
|
const QString varName = constructVarName();
|
||||||
const QString insertString = type.replace(type.length() - origNameLength, origNameLength,
|
const QString insertString = type.replace(type.length() - origNameLength, origNameLength,
|
||||||
varName + QLatin1String(" = "));
|
varName + QLatin1String(" = "));
|
||||||
ChangeSet changes;
|
m_file->apply(ChangeSet::makeInsert(m_insertPos, insertString));
|
||||||
changes.insert(m_insertPos, insertString);
|
|
||||||
m_file->apply(changes);
|
|
||||||
|
|
||||||
// move cursor to new variable name
|
// move cursor to new variable name
|
||||||
QTextCursor c = m_file->cursor();
|
QTextCursor c = m_file->cursor();
|
||||||
|
|||||||
@@ -290,9 +290,7 @@ private:
|
|||||||
insertion.prepend('\n');
|
insertion.prepend('\n');
|
||||||
if (file->charAt(insertPos) != QChar::ParagraphSeparator)
|
if (file->charAt(insertPos) != QChar::ParagraphSeparator)
|
||||||
insertion.append('\n');
|
insertion.append('\n');
|
||||||
ChangeSet s;
|
file->apply(ChangeSet::makeInsert(insertPos, insertion));
|
||||||
s.insert(insertPos, insertion);
|
|
||||||
file->apply(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString m_className;
|
const QString m_className;
|
||||||
|
|||||||
@@ -82,12 +82,10 @@ public:
|
|||||||
|
|
||||||
void perform() override
|
void perform() override
|
||||||
{
|
{
|
||||||
ChangeSet changes;
|
currentFile()->apply(ChangeSet::makeInsert(
|
||||||
int start = currentFile()->endOf(compoundStatement->lbrace_token);
|
currentFile()->endOf(compoundStatement->lbrace_token),
|
||||||
changes.insert(start, QLatin1String("\ncase ")
|
QLatin1String("\ncase ") + values.join(QLatin1String(":\nbreak;\ncase "))
|
||||||
+ values.join(QLatin1String(":\nbreak;\ncase "))
|
+ QLatin1String(":\nbreak;")));
|
||||||
+ QLatin1String(":\nbreak;"));
|
|
||||||
currentFile()->apply(changes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CompoundStatementAST *compoundStatement;
|
CompoundStatementAST *compoundStatement;
|
||||||
|
|||||||
@@ -28,9 +28,7 @@ public:
|
|||||||
|
|
||||||
void perform() override
|
void perform() override
|
||||||
{
|
{
|
||||||
ChangeSet changes;
|
currentFile()->apply(ChangeSet::makeReplace(start, end, replacement));
|
||||||
changes.replace(start, end, replacement);
|
|
||||||
currentFile()->apply(changes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -52,14 +52,11 @@ private:
|
|||||||
newName[i] = newName.at(i).toUpper();
|
newName[i] = newName.at(i).toUpper();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (m_test) {
|
if (m_test)
|
||||||
ChangeSet changeSet;
|
currentFile()->apply(ChangeSet::makeReplace(currentFile()->range(m_nameAst), newName));
|
||||||
changeSet.replace(currentFile()->range(m_nameAst), newName);
|
else
|
||||||
currentFile()->apply(changeSet);
|
|
||||||
} else {
|
|
||||||
editor()->renameUsages(newName);
|
editor()->renameUsages(newName);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const QString m_name;
|
const QString m_name;
|
||||||
const AST * const m_nameAst;
|
const AST * const m_nameAst;
|
||||||
|
|||||||
@@ -202,10 +202,8 @@ private:
|
|||||||
QTC_ASSERT(loc.isValid(), return);
|
QTC_ASSERT(loc.isValid(), return);
|
||||||
|
|
||||||
CppRefactoringFilePtr targetFile = refactoring.cppFile(filePath);
|
CppRefactoringFilePtr targetFile = refactoring.cppFile(filePath);
|
||||||
const int targetPosition = targetFile->position(loc.line(), loc.column());
|
targetFile->apply(ChangeSet::makeInsert(
|
||||||
ChangeSet target;
|
targetFile->position(loc.line(), loc.column()), loc.prefix() + decl + ";\n"));
|
||||||
target.insert(targetPosition, loc.prefix() + decl + ";\n");
|
|
||||||
targetFile->apply(target);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Class * const m_class;
|
const Class * const m_class;
|
||||||
@@ -236,11 +234,10 @@ public:
|
|||||||
QString declaration = getDeclaration();
|
QString declaration = getDeclaration();
|
||||||
|
|
||||||
if (!declaration.isEmpty()) {
|
if (!declaration.isEmpty()) {
|
||||||
ChangeSet changes;
|
currentFile()->apply(ChangeSet::makeReplace(
|
||||||
changes.replace(currentFile()->startOf(binaryAST),
|
currentFile()->startOf(binaryAST),
|
||||||
currentFile()->endOf(simpleNameAST),
|
currentFile()->endOf(simpleNameAST),
|
||||||
declaration);
|
declaration));
|
||||||
currentFile()->apply(changes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -211,10 +211,9 @@ public:
|
|||||||
const InsertionLocation &location =
|
const InsertionLocation &location =
|
||||||
locator.methodDeclarationInClass(filePath, matchingClass, options.access);
|
locator.methodDeclarationInClass(filePath, matchingClass, options.access);
|
||||||
CppRefactoringFilePtr declFile = refactoring.cppFile(filePath);
|
CppRefactoringFilePtr declFile = refactoring.cppFile(filePath);
|
||||||
change.clear();
|
declFile->apply(ChangeSet::makeInsert(
|
||||||
position = declFile->position(location.line(), location.column());
|
declFile->position(location.line(), location.column()),
|
||||||
change.insert(position, location.prefix() + funcDecl + location.suffix());
|
location.prefix() + funcDecl + location.suffix()));
|
||||||
declFile->apply(change);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -455,14 +455,10 @@ private:
|
|||||||
headerContent.append("\n#endif // " + headerGuard + '\n');
|
headerContent.append("\n#endif // " + headerGuard + '\n');
|
||||||
|
|
||||||
headerFilePath.ensureExistingFile();
|
headerFilePath.ensureExistingFile();
|
||||||
ChangeSet headerChanges;
|
state->factory.cppFile(headerFilePath)->apply(ChangeSet::makeInsert(0, headerContent));
|
||||||
headerChanges.insert(0, headerContent);
|
|
||||||
state->factory.cppFile(headerFilePath)->apply(headerChanges);
|
|
||||||
if (hasSourceContent || mustCreateSourceFile) {
|
if (hasSourceContent || mustCreateSourceFile) {
|
||||||
sourceFilePath.ensureExistingFile();
|
sourceFilePath.ensureExistingFile();
|
||||||
ChangeSet sourceChanges;
|
state->factory.cppFile(sourceFilePath)->apply(ChangeSet::makeInsert(0, sourceContent));
|
||||||
sourceChanges.insert(0, sourceContent);
|
|
||||||
state->factory.cppFile(sourceFilePath)->apply(sourceChanges);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!projectNode)
|
if (!projectNode)
|
||||||
|
|||||||
@@ -274,11 +274,8 @@ private:
|
|||||||
toTarget.remove(m_fromRange);
|
toTarget.remove(m_fromRange);
|
||||||
toFile->setOpenEditor(true, m_toRange.start);
|
toFile->setOpenEditor(true, m_toRange.start);
|
||||||
toFile->apply(toTarget);
|
toFile->apply(toTarget);
|
||||||
if (m_toFilePath != m_fromFilePath) {
|
if (m_toFilePath != m_fromFilePath)
|
||||||
ChangeSet fromTarget;
|
fromFile->apply(ChangeSet::makeRemove(m_fromRange));
|
||||||
fromTarget.remove(m_fromRange);
|
|
||||||
fromFile->apply(fromTarget);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ensureFuncDefAstAndRange(CppRefactoringFile &defFile)
|
void ensureFuncDefAstAndRange(CppRefactoringFile &defFile)
|
||||||
|
|||||||
@@ -38,11 +38,12 @@ public:
|
|||||||
void perform() override
|
void perform() override
|
||||||
{
|
{
|
||||||
int targetEndPos = currentFile()->endOf(m_targetParam);
|
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()->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:
|
private:
|
||||||
|
|||||||
@@ -627,9 +627,7 @@ bool QtCreatorIntegration::navigateToSlot(const QString &objectName,
|
|||||||
const RefactoringFilePtr file = refactoring.file(location.filePath());
|
const RefactoringFilePtr file = refactoring.file(location.filePath());
|
||||||
const int insertionPos = Utils::Text::positionInText(file->document(),
|
const int insertionPos = Utils::Text::positionInText(file->document(),
|
||||||
location.line(), location.column());
|
location.line(), location.column());
|
||||||
ChangeSet changeSet;
|
file->apply(ChangeSet::makeInsert(insertionPos, definition));
|
||||||
changeSet.insert(insertionPos, definition);
|
|
||||||
file->apply(changeSet);
|
|
||||||
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);
|
||||||
|
|||||||
@@ -187,9 +187,7 @@ public:
|
|||||||
for (const QString &property : std::as_const(result))
|
for (const QString &property : std::as_const(result))
|
||||||
replacement += property + QLatin1String(": ") + propertyReader.readAstValue(property) + QLatin1Char('\n');
|
replacement += property + QLatin1String(": ") + propertyReader.readAstValue(property) + QLatin1Char('\n');
|
||||||
|
|
||||||
Utils::ChangeSet changes;
|
currentFile->apply(ChangeSet::makeReplace(start, end, replacement));
|
||||||
changes.replace(start, end, replacement);
|
|
||||||
currentFile->apply(changes);
|
|
||||||
|
|
||||||
Core::IVersionControl *versionControl = Core::VcsManager::findVersionControlForDirectory(
|
Core::IVersionControl *versionControl = Core::VcsManager::findVersionControlForDirectory(
|
||||||
path);
|
path);
|
||||||
|
|||||||
@@ -116,10 +116,9 @@ public:
|
|||||||
const QmlJSRefactoringChanges &,
|
const QmlJSRefactoringChanges &,
|
||||||
const QString &) override
|
const QString &) override
|
||||||
{
|
{
|
||||||
Utils::ChangeSet changes;
|
currentFile->apply(Utils::ChangeSet::makeInsert(
|
||||||
const int insertLoc = _message.location.begin() - _message.location.startColumn + 1;
|
_message.location.begin() - _message.location.startColumn + 1,
|
||||||
changes.insert(insertLoc, QString::fromLatin1("// %1\n").arg(_message.suppressionString()));
|
QString::fromLatin1("// %1\n").arg(_message.suppressionString())));
|
||||||
currentFile->apply(changes);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user