forked from qt-creator/qt-creator
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:
@@ -53,12 +53,12 @@ RefactoringFile::RefactoringFile(const FilePath &filePath) : m_filePath(filePath
|
|||||||
|
|
||||||
bool RefactoringFile::create(const QString &contents, bool reindent, bool openInEditor)
|
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;
|
return false;
|
||||||
|
|
||||||
// Create a text document for the new file:
|
// Create a text document for the new file:
|
||||||
auto document = new QTextDocument;
|
m_document = new QTextDocument;
|
||||||
QTextCursor cursor(document);
|
QTextCursor cursor(m_document);
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
cursor.insertText(contents);
|
cursor.insertText(contents);
|
||||||
|
|
||||||
@@ -74,8 +74,9 @@ bool RefactoringFile::create(const QString &contents, bool reindent, bool openIn
|
|||||||
TextFileFormat format;
|
TextFileFormat format;
|
||||||
format.codec = EditorManager::defaultTextCodec();
|
format.codec = EditorManager::defaultTextCodec();
|
||||||
QString error;
|
QString error;
|
||||||
bool saveOk = format.writeFile(m_filePath, document->toPlainText(), &error);
|
bool saveOk = format.writeFile(m_filePath, m_document->toPlainText(), &error);
|
||||||
delete document;
|
delete m_document;
|
||||||
|
m_document = nullptr;
|
||||||
if (!saveOk)
|
if (!saveOk)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user