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 <christian.kandeler@qt.io>
This commit is contained in:
Audun Sutterud
2024-08-21 16:16:10 +02:00
parent ea88b32124
commit ac0ad40efe

View File

@@ -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;