From ac0ad40efe90626527b820cac99003076ad4fb4d Mon Sep 17 00:00:00 2001 From: Audun Sutterud Date: Wed, 21 Aug 2024 16:16:10 +0200 Subject: [PATCH] TextEditor: Fix indentation of created files RefactoringFile::create() does not ensure that created files are properly indented. This can be observed when refactoring operations are used to move a component into a separate file. Fixes: QTCREATORBUG-31084 Change-Id: I4d2dc4fbab21fb869baea8ab4b751c7e18a3ca59 Reviewed-by: Christian Kandeler --- src/plugins/texteditor/refactoringchanges.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp index c8688f2fba5..d1a11b4e9ca 100644 --- a/src/plugins/texteditor/refactoringchanges.cpp +++ b/src/plugins/texteditor/refactoringchanges.cpp @@ -53,12 +53,12 @@ RefactoringFile::RefactoringFile(const FilePath &filePath) : m_filePath(filePath bool RefactoringFile::create(const QString &contents, bool reindent, bool openInEditor) { - if (m_filePath.isEmpty() || m_filePath.exists() || m_editor) + if (m_filePath.isEmpty() || m_filePath.exists() || m_editor || m_document) return false; // Create a text document for the new file: - auto document = new QTextDocument; - QTextCursor cursor(document); + m_document = new QTextDocument; + QTextCursor cursor(m_document); cursor.beginEditBlock(); cursor.insertText(contents); @@ -74,8 +74,9 @@ bool RefactoringFile::create(const QString &contents, bool reindent, bool openIn TextFileFormat format; format.codec = EditorManager::defaultTextCodec(); QString error; - bool saveOk = format.writeFile(m_filePath, document->toPlainText(), &error); - delete document; + bool saveOk = format.writeFile(m_filePath, m_document->toPlainText(), &error); + delete m_document; + m_document = nullptr; if (!saveOk) return false;