Commit 71cbd765 authored by Christian Kamm's avatar Christian Kamm

RefactoringChanges: Fix problems with encodings.

Task-number: QTCREATORBUG-6140
Change-Id: Iedbfca6987bffa2670df39f5f461690218f6da14
Reviewed-on: http://codereview.qt-project.org/5233Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent 7c7562f2
......@@ -37,7 +37,7 @@
#include <coreplugin/editormanager/editormanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QtCore/QFile>
#include <QtCore/QSet>
......@@ -120,10 +120,12 @@ bool RefactoringChanges::createFile(const QString &fileName, const QString &cont
}
if (!editor) {
Utils::FileSaver saver(fileName);
saver.write(document->toPlainText().toUtf8());
Utils::TextFileFormat format;
format.codec = Core::EditorManager::instance()->defaultTextCodec();
QString error;
bool saveOk = format.writeFile(fileName, document->toPlainText(), &error);
delete document;
if (!saver.finalize(Core::ICore::instance()->mainWindow()))
if (!saveOk)
return false;
}
......@@ -219,11 +221,19 @@ QTextDocument *RefactoringFile::mutableDocument() const
return m_editor->document();
else if (!m_document) {
QString fileContents;
if (!m_fileName.isEmpty() && QFile::exists(m_fileName)) {
Utils::FileReader reader;
if (reader.fetch(m_fileName, Core::ICore::instance()->mainWindow()))
fileContents = QString::fromUtf8(reader.data());
if (!m_fileName.isEmpty()) {
QString error;
QTextCodec *defaultCodec = Core::EditorManager::instance()->defaultTextCodec();
Utils::TextFileFormat::ReadResult result = Utils::TextFileFormat::readFile(
m_fileName, defaultCodec,
&fileContents, &m_textFileFormat,
&error);
if (result != Utils::TextFileFormat::ReadSuccess) {
qWarning() << "Could not read " << m_fileName << ". Error: " << error;
m_textFileFormat.codec = 0;
}
}
// always make a QTextDocument to avoid excessive null checks
m_document = new QTextDocument(fileContents);
}
return m_document;
......@@ -325,7 +335,10 @@ void RefactoringFile::apply()
// apply changes, if any
if (m_data && !(m_indentRanges.isEmpty() && m_changes.isEmpty())) {
QTextDocument *doc = mutableDocument();
if (doc) {
if (!doc)
return;
{
QTextCursor c(doc);
c.beginEditBlock();
......@@ -346,10 +359,11 @@ void RefactoringFile::apply()
}
// if this document doesn't have an editor, write the result to a file
if (!m_editor && !m_fileName.isEmpty()) {
Utils::FileSaver saver(m_fileName);
saver.write(doc->toPlainText().toUtf8());
saver.finalize(Core::ICore::instance()->mainWindow());
if (!m_editor && m_textFileFormat.codec) {
QTC_ASSERT(!m_fileName.isEmpty(), return);
QString error;
if (!m_textFileFormat.writeFile(m_fileName, doc->toPlainText(), &error))
qWarning() << "Could not apply changes to" << m_fileName << ". Error: " << error;
}
fileChanged();
......
......@@ -34,6 +34,7 @@
#define REFACTORINGCHANGES_H
#include <utils/changeset.h>
#include <utils/textfileformat.h>
#include <texteditor/texteditor_global.h>
#include <QtCore/QList>
......@@ -83,7 +84,10 @@ public:
void apply();
protected:
// users may only get const access to RefactoringFiles created through
// this constructor, because it can't be used to apply changes
RefactoringFile(QTextDocument *document, const QString &fileName);
RefactoringFile(BaseTextEditorWidget *editor);
RefactoringFile(const QString &fileName, const QSharedPointer<RefactoringChangesData> &data);
......@@ -94,6 +98,7 @@ protected:
protected:
QString m_fileName;
QSharedPointer<RefactoringChangesData> m_data;
mutable Utils::TextFileFormat m_textFileFormat;
mutable QTextDocument *m_document;
BaseTextEditorWidget *m_editor;
Utils::ChangeSet m_changes;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment