Utils: add convenience function to ChangeSet

Creating a QTextCursor just for the ChangeSet gives no benefit for the
calling code, but reduces the readability.

Change-Id: I34acb6083b6f7ab54fce042e29cd6e80498338ef
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2023-07-04 11:15:26 +02:00
parent e1c97d4e47
commit 81e8c67686
6 changed files with 12 additions and 8 deletions

View File

@@ -318,6 +318,12 @@ void ChangeSet::apply(QTextCursor *textCursor)
m_cursor = nullptr;
}
void ChangeSet::apply(QTextDocument *document)
{
QTextCursor c(document);
apply(&c);
}
QString ChangeSet::textAt(int pos, int length)
{
if (m_string) {

View File

@@ -10,6 +10,7 @@
QT_BEGIN_NAMESPACE
class QTextCursor;
class QTextDocument;
QT_END_NAMESPACE
namespace Utils {
@@ -76,6 +77,7 @@ public:
void apply(QString *s);
void apply(QTextCursor *textCursor);
void apply(QTextDocument *document);
private:
// length-based API.

View File

@@ -76,8 +76,7 @@ static void applyRefactorings(QTextDocument *textDocument, TextEditorWidget *edi
Utils::ChangeSet change = formatter.format(cppDocument->translationUnit()->ast());
// Apply change
QTextCursor cursor(textDocument);
change.apply(&cursor);
change.apply(textDocument);
}
// ------------------ CppCodeStyleSettingsWidget

View File

@@ -135,8 +135,7 @@ public:
{
Utils::ChangeSet change;
change.insert(m_position, QLatin1String(text));
QTextCursor cursor(m_textDocument);
change.apply(&cursor);
change.apply(m_textDocument);
m_position += text.length();
}

View File

@@ -107,8 +107,7 @@ public:
Utils::ChangeSet change = formatter.format(ast); // ChangeSet may be empty.
// Apply change
QTextCursor changeCursor(qtextDocument);
change.apply(&changeCursor);
change.apply(qtextDocument);
// Compare
QCOMPARE(qtextDocument->toPlainText(), expectedSource);

View File

@@ -318,8 +318,7 @@ void QuickToolBar::removeProperty(const QString &propertyName)
Utils::ChangeSet changeSet;
Rewriter rewriter(m_doc->source(), &changeSet, m_propertyOrder);
rewriter.removeBindingByName(initializer, propertyName);
QTextCursor tc(m_editorWidget->document());
changeSet.apply(&tc);
changeSet.apply(m_editorWidget->document());
}
}
}