From 16c346c806c681627109fae72bf5f7a57ac74143 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 22 May 2024 16:05:22 +0200 Subject: [PATCH] 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 --- src/libs/utils/changeset.cpp | 35 +++++++++++++++++++ src/libs/utils/changeset.h | 6 ++++ .../quickfixes/assigntolocalvariable.cpp | 4 +-- .../quickfixes/bringidentifierintoscope.cpp | 4 +-- .../quickfixes/completeswitchstatement.cpp | 10 +++--- .../quickfixes/convertnumericliteral.cpp | 4 +-- .../quickfixes/converttocamelcase.cpp | 9 ++--- .../quickfixes/createdeclarationfromuse.cpp | 15 ++++---- .../cppeditor/quickfixes/extractfunction.cpp | 7 ++-- .../quickfixes/moveclasstoownfile.cpp | 8 ++--- .../quickfixes/movefunctiondefinition.cpp | 7 ++-- .../rearrangeparamdeclarationlist.cpp | 9 ++--- src/plugins/designer/qtcreatorintegration.cpp | 4 +-- .../qmljscomponentfromobjectdef.cpp | 4 +-- src/plugins/qmljseditor/qmljsquickfixes.cpp | 7 ++-- 15 files changed, 74 insertions(+), 59 deletions(-) diff --git a/src/libs/utils/changeset.cpp b/src/libs/utils/changeset.cpp index c0c54b1e486..881097c6b70 100644 --- a/src/libs/utils/changeset.cpp +++ b/src/libs/utils/changeset.cpp @@ -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)) diff --git a/src/libs/utils/changeset.h b/src/libs/utils/changeset.h index 6370b20d3c2..aee9c99dab5 100644 --- a/src/libs/utils/changeset.h +++ b/src/libs/utils/changeset.h @@ -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); diff --git a/src/plugins/cppeditor/quickfixes/assigntolocalvariable.cpp b/src/plugins/cppeditor/quickfixes/assigntolocalvariable.cpp index 58a9818d8e5..ae63681dc26 100644 --- a/src/plugins/cppeditor/quickfixes/assigntolocalvariable.cpp +++ b/src/plugins/cppeditor/quickfixes/assigntolocalvariable.cpp @@ -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(); diff --git a/src/plugins/cppeditor/quickfixes/bringidentifierintoscope.cpp b/src/plugins/cppeditor/quickfixes/bringidentifierintoscope.cpp index 315ecfa3a85..3c4df4cb76e 100644 --- a/src/plugins/cppeditor/quickfixes/bringidentifierintoscope.cpp +++ b/src/plugins/cppeditor/quickfixes/bringidentifierintoscope.cpp @@ -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; diff --git a/src/plugins/cppeditor/quickfixes/completeswitchstatement.cpp b/src/plugins/cppeditor/quickfixes/completeswitchstatement.cpp index d2125697bc6..15b8e592edb 100644 --- a/src/plugins/cppeditor/quickfixes/completeswitchstatement.cpp +++ b/src/plugins/cppeditor/quickfixes/completeswitchstatement.cpp @@ -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; diff --git a/src/plugins/cppeditor/quickfixes/convertnumericliteral.cpp b/src/plugins/cppeditor/quickfixes/convertnumericliteral.cpp index 2d977a67301..6fb5becf348 100644 --- a/src/plugins/cppeditor/quickfixes/convertnumericliteral.cpp +++ b/src/plugins/cppeditor/quickfixes/convertnumericliteral.cpp @@ -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: diff --git a/src/plugins/cppeditor/quickfixes/converttocamelcase.cpp b/src/plugins/cppeditor/quickfixes/converttocamelcase.cpp index 918b43d1a87..c597a29c115 100644 --- a/src/plugins/cppeditor/quickfixes/converttocamelcase.cpp +++ b/src/plugins/cppeditor/quickfixes/converttocamelcase.cpp @@ -52,13 +52,10 @@ 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; diff --git a/src/plugins/cppeditor/quickfixes/createdeclarationfromuse.cpp b/src/plugins/cppeditor/quickfixes/createdeclarationfromuse.cpp index 57ee646d9f0..f7a760a99e7 100644 --- a/src/plugins/cppeditor/quickfixes/createdeclarationfromuse.cpp +++ b/src/plugins/cppeditor/quickfixes/createdeclarationfromuse.cpp @@ -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()->endOf(simpleNameAST), - declaration); - currentFile()->apply(changes); + currentFile()->apply(ChangeSet::makeReplace( + currentFile()->startOf(binaryAST), + currentFile()->endOf(simpleNameAST), + declaration)); } } diff --git a/src/plugins/cppeditor/quickfixes/extractfunction.cpp b/src/plugins/cppeditor/quickfixes/extractfunction.cpp index 58e5a2ef9d1..46be84fa78e 100644 --- a/src/plugins/cppeditor/quickfixes/extractfunction.cpp +++ b/src/plugins/cppeditor/quickfixes/extractfunction.cpp @@ -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())); } } diff --git a/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp b/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp index 896eb149990..3d1ec64c134 100644 --- a/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp +++ b/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp @@ -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) diff --git a/src/plugins/cppeditor/quickfixes/movefunctiondefinition.cpp b/src/plugins/cppeditor/quickfixes/movefunctiondefinition.cpp index ef7dac7f68d..f6223629ae9 100644 --- a/src/plugins/cppeditor/quickfixes/movefunctiondefinition.cpp +++ b/src/plugins/cppeditor/quickfixes/movefunctiondefinition.cpp @@ -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) diff --git a/src/plugins/cppeditor/quickfixes/rearrangeparamdeclarationlist.cpp b/src/plugins/cppeditor/quickfixes/rearrangeparamdeclarationlist.cpp index 61f922cf601..6917771a67b 100644 --- a/src/plugins/cppeditor/quickfixes/rearrangeparamdeclarationlist.cpp +++ b/src/plugins/cppeditor/quickfixes/rearrangeparamdeclarationlist.cpp @@ -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: diff --git a/src/plugins/designer/qtcreatorintegration.cpp b/src/plugins/designer/qtcreatorintegration.cpp index 2c4cc907dc6..5e6d8900959 100644 --- a/src/plugins/designer/qtcreatorintegration.cpp +++ b/src/plugins/designer/qtcreatorintegration.cpp @@ -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); diff --git a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp index 261715acbd8..ce6473b9880 100644 --- a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp +++ b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp @@ -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); diff --git a/src/plugins/qmljseditor/qmljsquickfixes.cpp b/src/plugins/qmljseditor/qmljsquickfixes.cpp index f60d59e214c..5bb077e8147 100644 --- a/src/plugins/qmljseditor/qmljsquickfixes.cpp +++ b/src/plugins/qmljseditor/qmljsquickfixes.cpp @@ -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()))); } };