forked from qt-creator/qt-creator
TextEditor: Adding an undo step when a file gets reloaded.
Adds the opportunity to recover unsaved changes of a file that was externally modified and reloaded inside Qt Creator. Task-number: QTCREATORBUG-10281 Change-Id: Ifa7f61daa8a8d336508b0d9c1c300b0f77c207ea Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
@@ -562,7 +562,7 @@ Core::IDocument::OpenResult TextDocument::open(QString *errorString, const QStri
|
|||||||
const QString &realFileName)
|
const QString &realFileName)
|
||||||
{
|
{
|
||||||
emit aboutToOpen(fileName, realFileName);
|
emit aboutToOpen(fileName, realFileName);
|
||||||
OpenResult success = openImpl(errorString, fileName, realFileName);
|
OpenResult success = openImpl(errorString, fileName, realFileName, /*reload =*/ false);
|
||||||
if (success == OpenResult::Success) {
|
if (success == OpenResult::Success) {
|
||||||
Utils::MimeDatabase mdb;
|
Utils::MimeDatabase mdb;
|
||||||
setMimeType(mdb.mimeTypeForFile(fileName).name());
|
setMimeType(mdb.mimeTypeForFile(fileName).name());
|
||||||
@@ -572,7 +572,7 @@ Core::IDocument::OpenResult TextDocument::open(QString *errorString, const QStri
|
|||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, const QString &fileName,
|
Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, const QString &fileName,
|
||||||
const QString &realFileName)
|
const QString &realFileName, bool reload)
|
||||||
{
|
{
|
||||||
QStringList content;
|
QStringList content;
|
||||||
|
|
||||||
@@ -582,31 +582,39 @@ Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, const Q
|
|||||||
const QFileInfo fi(fileName);
|
const QFileInfo fi(fileName);
|
||||||
d->m_fileIsReadOnly = !fi.isWritable();
|
d->m_fileIsReadOnly = !fi.isWritable();
|
||||||
readResult = read(realFileName, &content, errorString);
|
readResult = read(realFileName, &content, errorString);
|
||||||
|
|
||||||
d->m_document.setModified(false);
|
|
||||||
const int chunks = content.size();
|
const int chunks = content.size();
|
||||||
if (chunks == 0) {
|
|
||||||
d->m_document.clear();
|
d->m_document.setUndoRedoEnabled(reload);
|
||||||
} else if (chunks == 1) {
|
QTextCursor c(&d->m_document);
|
||||||
d->m_document.setPlainText(content.at(0));
|
c.beginEditBlock();
|
||||||
|
if (reload) {
|
||||||
|
c.select(QTextCursor::Document);
|
||||||
|
c.removeSelectedText();
|
||||||
} else {
|
} else {
|
||||||
|
d->m_document.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chunks == 1) {
|
||||||
|
c.insertText(content.at(0));
|
||||||
|
} else if (chunks > 1) {
|
||||||
QFutureInterface<void> interface;
|
QFutureInterface<void> interface;
|
||||||
interface.setProgressRange(0, chunks);
|
interface.setProgressRange(0, chunks);
|
||||||
ProgressManager::addTask(interface.future(), tr("Opening File"), Constants::TASK_OPEN_FILE);
|
ProgressManager::addTask(interface.future(), tr("Opening File"),
|
||||||
|
Constants::TASK_OPEN_FILE);
|
||||||
interface.reportStarted();
|
interface.reportStarted();
|
||||||
d->m_document.setUndoRedoEnabled(false);
|
|
||||||
QTextCursor c(&d->m_document);
|
|
||||||
c.beginEditBlock();
|
|
||||||
d->m_document.clear();
|
|
||||||
for (int i = 0; i < chunks; ++i) {
|
for (int i = 0; i < chunks; ++i) {
|
||||||
c.insertText(content.at(i));
|
c.insertText(content.at(i));
|
||||||
interface.setProgressValue(i + 1);
|
interface.setProgressValue(i + 1);
|
||||||
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
||||||
}
|
}
|
||||||
c.endEditBlock();
|
|
||||||
d->m_document.setUndoRedoEnabled(true);
|
|
||||||
interface.reportFinished();
|
interface.reportFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.endEditBlock();
|
||||||
|
d->m_document.setUndoRedoEnabled(true);
|
||||||
|
|
||||||
TextDocumentLayout *documentLayout =
|
TextDocumentLayout *documentLayout =
|
||||||
qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout());
|
qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout());
|
||||||
QTC_ASSERT(documentLayout, return OpenResult::CannotHandle);
|
QTC_ASSERT(documentLayout, return OpenResult::CannotHandle);
|
||||||
@@ -636,7 +644,8 @@ bool TextDocument::reload(QString *errorString)
|
|||||||
if (documentLayout)
|
if (documentLayout)
|
||||||
marks = documentLayout->documentClosing(); // removes text marks non-permanently
|
marks = documentLayout->documentClosing(); // removes text marks non-permanently
|
||||||
|
|
||||||
bool success = (openImpl(errorString, filePath().toString(), filePath().toString()) == OpenResult::Success);
|
const QString &file = filePath().toString();
|
||||||
|
bool success = openImpl(errorString, file, file, /*reload =*/ true) == OpenResult::Success;
|
||||||
|
|
||||||
if (documentLayout)
|
if (documentLayout)
|
||||||
documentLayout->documentReloaded(marks, this); // re-adds text marks
|
documentLayout->documentReloaded(marks, this); // re-adds text marks
|
||||||
|
|||||||
@@ -149,7 +149,8 @@ protected slots:
|
|||||||
virtual void applyFontSettings();
|
virtual void applyFontSettings();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OpenResult openImpl(QString *errorString, const QString &fileName, const QString &realFileName);
|
OpenResult openImpl(QString *errorString, const QString &fileName, const QString &realFileName,
|
||||||
|
bool reload);
|
||||||
void cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument);
|
void cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument);
|
||||||
void ensureFinalNewLine(QTextCursor &cursor);
|
void ensureFinalNewLine(QTextCursor &cursor);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user