Fix handling of read-only editors when applying refactorings

If e.g. a ".ui" file is open, there is a read-only text editor widget
for the file, even though the file itself is writable.
The application of refactorings or global text-based replace should not
change the content of this read-only editor widget, but instead operate
directly on the file as if it wasn't open in Qt Creator at all.
It should also silently reload these files after modification on disk.

Task-number: QTCREATORBUG-19958
Change-Id: I409d5d03059be4c3520a1031ff0fbfa9feb675bb
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2018-08-20 14:26:51 +02:00
parent 7b6ea357f6
commit 2486e61068

View File

@@ -29,6 +29,7 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/dialogs/readonlyfilesdialog.h> #include <coreplugin/dialogs/readonlyfilesdialog.h>
#include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
@@ -178,8 +179,11 @@ RefactoringFile::RefactoringFile(const QString &fileName, const QSharedPointer<R
, m_appliedOnce(false) , m_appliedOnce(false)
{ {
QList<IEditor *> editors = DocumentModel::editorsForFilePath(fileName); QList<IEditor *> editors = DocumentModel::editorsForFilePath(fileName);
if (!editors.isEmpty()) if (!editors.isEmpty()) {
m_editor = qobject_cast<TextEditorWidget *>(editors.first()->widget()); auto editorWidget = qobject_cast<TextEditorWidget *>(editors.first()->widget());
if (editorWidget && !editorWidget->isReadOnly())
m_editor = editorWidget;
}
} }
RefactoringFile::~RefactoringFile() RefactoringFile::~RefactoringFile()
@@ -378,6 +382,8 @@ bool RefactoringFile::apply()
if (!m_editor && m_textFileFormat.codec) { if (!m_editor && m_textFileFormat.codec) {
QTC_ASSERT(!m_fileName.isEmpty(), return false); QTC_ASSERT(!m_fileName.isEmpty(), return false);
QString error; QString error;
// suppress "file has changed" warnings if the file is open in a read-only editor
Core::FileChangeBlocker block(m_fileName);
if (!m_textFileFormat.writeFile(m_fileName, doc->toPlainText(), &error)) { if (!m_textFileFormat.writeFile(m_fileName, doc->toPlainText(), &error)) {
qWarning() << "Could not apply changes to" << m_fileName << ". Error: " << error; qWarning() << "Could not apply changes to" << m_fileName << ". Error: " << error;
result = false; result = false;