aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/refactoringchanges.cpp
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2023-11-16 14:07:17 +0100
committerChristian Kandeler <christian.kandeler@qt.io>2023-11-17 08:31:45 +0000
commit156569fcc737685bd8326b3a903e14777cba935b (patch)
tree4d0760b668d9665ad880ee5195dc524987fe3444 /src/plugins/texteditor/refactoringchanges.cpp
parent583a9dcea1008d86e3bb33dd735136024b0df0bd (diff)
TextEditor: Move code from RefactoringChanges to RefactoringFile
Preparation for de-polymorphisation of RefactoringChangesData. Change-Id: Ia2a8f8e2a3a403f809e67c907d3474e7c1a52417 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Diffstat (limited to 'src/plugins/texteditor/refactoringchanges.cpp')
-rw-r--r--src/plugins/texteditor/refactoringchanges.cpp69
1 files changed, 37 insertions, 32 deletions
diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp
index e9016d3cb6..fdff256758 100644
--- a/src/plugins/texteditor/refactoringchanges.cpp
+++ b/src/plugins/texteditor/refactoringchanges.cpp
@@ -54,37 +54,7 @@ bool RefactoringChanges::createFile(const FilePath &filePath,
bool reindent,
bool openEditor) const
{
- if (filePath.exists())
- return false;
-
- // Create a text document for the new file:
- auto document = new QTextDocument;
- QTextCursor cursor(document);
- cursor.beginEditBlock();
- cursor.insertText(contents);
-
- // Reindent the contents:
- if (reindent) {
- cursor.select(QTextCursor::Document);
- m_data->indentSelection(cursor, filePath, nullptr);
- }
- cursor.endEditBlock();
-
- // Write the file to disk:
- TextFileFormat format;
- format.codec = EditorManager::defaultTextCodec();
- QString error;
- bool saveOk = format.writeFile(filePath, document->toPlainText(), &error);
- delete document;
- if (!saveOk)
- return false;
-
- m_data->fileChanged(filePath);
-
- if (openEditor)
- RefactoringChanges::openEditor(filePath, /*bool activate =*/ false, -1, -1);
-
- return true;
+ return file(filePath)->create(contents, reindent, openEditor);
}
TextEditorWidget *RefactoringChanges::openEditor(const FilePath &filePath,
@@ -134,6 +104,41 @@ RefactoringFile::RefactoringFile(const FilePath &filePath,
}
}
+bool RefactoringFile::create(const QString &contents, bool reindent, bool openEditor)
+{
+ if (m_filePath.isEmpty() || m_filePath.exists() || m_editor)
+ return false;
+
+ // Create a text document for the new file:
+ auto document = new QTextDocument;
+ QTextCursor cursor(document);
+ cursor.beginEditBlock();
+ cursor.insertText(contents);
+
+ // Reindent the contents:
+ if (reindent) {
+ cursor.select(QTextCursor::Document);
+ m_data->indentSelection(cursor, m_filePath, nullptr);
+ }
+ cursor.endEditBlock();
+
+ // Write the file to disk:
+ TextFileFormat format;
+ format.codec = EditorManager::defaultTextCodec();
+ QString error;
+ bool saveOk = format.writeFile(m_filePath, document->toPlainText(), &error);
+ delete document;
+ if (!saveOk)
+ return false;
+
+ fileChanged();
+
+ if (openEditor)
+ RefactoringChanges::openEditor(m_filePath, /*bool activate =*/ false, -1, -1);
+
+ return true;
+}
+
RefactoringFile::~RefactoringFile()
{
delete m_document;
@@ -443,7 +448,7 @@ void RefactoringFile::doFormatting()
formattingRanges.push_back({line, line});
}
- static const QString clangFormatLineRemovalBlocker("// QTC_TEMP");
+ static const QString clangFormatLineRemovalBlocker("");
for (const RangeInLines &r : std::as_const(formattingRanges)) {
QTextBlock b = m_editor->document()->findBlockByNumber(r.startLine - 1);
while (true) {