Core: Return expected_str<void> from IDocument::reload()

... and adjust callers.

Change-Id: Iee41f06e8f6e0019aecff67ce06a6f22ec1a45ee
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-09-20 12:49:55 +02:00
parent c66185ce04
commit 779fcdb18b
29 changed files with 138 additions and 122 deletions

View File

@@ -120,7 +120,7 @@ public:
bool isSaveAsAllowed() const final { return true; } bool isSaveAsAllowed() const final { return true; }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) final;
Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) final; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) final;
void fetchData(quint64 address) const { if (m_fetchDataHandler) m_fetchDataHandler(address); } void fetchData(quint64 address) const { if (m_fetchDataHandler) m_fetchDataHandler(address); }
@@ -2182,16 +2182,19 @@ bool BinEditorDocument::isModified() const
return m_undoStack.size() != m_unmodifiedState; return m_undoStack.size() != m_unmodifiedState;
} }
bool BinEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) expected_str<void> BinEditorDocument::reload(ReloadFlag flag, ChangeType type)
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
emit aboutToReload(); emit aboutToReload();
clear(); clear();
const bool success = (openImpl(errorString, filePath()) == OpenResult::Success); QString errorString;
const bool success = (openImpl(&errorString, filePath()) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
expected_str<void> BinEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) expected_str<void> BinEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)

View File

@@ -1188,19 +1188,18 @@ void DocumentManager::checkForReload()
removeFileInfo(document); removeFileInfo(document);
addFileInfos({document}); addFileInfos({document});
bool success = true; expected_str<void> success;
QString errorString;
// we've got some modification // we've got some modification
document->checkPermissions(); document->checkPermissions();
// check if it's contents or permissions: // check if it's contents or permissions:
if (!type) { if (!type) {
// Only permission change // Only permission change
success = true; success = {};
// now we know it's a content change or file was removed // now we know it's a content change or file was removed
} else if (defaultBehavior == IDocument::ReloadUnmodified && type == IDocument::TypeContents } else if (defaultBehavior == IDocument::ReloadUnmodified && type == IDocument::TypeContents
&& !document->isModified()) { && !document->isModified()) {
// content change, but unmodified (and settings say to reload in this case) // content change, but unmodified (and settings say to reload in this case)
success = document->reload(&errorString, IDocument::FlagReload, *type); success = document->reload(IDocument::FlagReload, *type);
// file was removed or it's a content change and the default behavior for // file was removed or it's a content change and the default behavior for
// unmodified files didn't kick in // unmodified files didn't kick in
} else if (defaultBehavior == IDocument::ReloadUnmodified && type == IDocument::TypeRemoved } else if (defaultBehavior == IDocument::ReloadUnmodified && type == IDocument::TypeRemoved
@@ -1210,7 +1209,7 @@ void DocumentManager::checkForReload()
documentsToClose << document; documentsToClose << document;
} else if (defaultBehavior == IDocument::IgnoreAll) { } else if (defaultBehavior == IDocument::IgnoreAll) {
// content change or removed, but settings say ignore // content change or removed, but settings say ignore
success = document->reload(&errorString, IDocument::FlagIgnore, *type); success = document->reload(IDocument::FlagIgnore, *type);
// either the default behavior is to always ask, // either the default behavior is to always ask,
// or the ReloadUnmodified default behavior didn't kick in, // or the ReloadUnmodified default behavior didn't kick in,
// so do whatever the IDocument wants us to do // so do whatever the IDocument wants us to do
@@ -1221,16 +1220,16 @@ void DocumentManager::checkForReload()
if (type == IDocument::TypeRemoved) if (type == IDocument::TypeRemoved)
documentsToClose << document; documentsToClose << document;
else else
success = document->reload(&errorString, IDocument::FlagReload, *type); success = document->reload(IDocument::FlagReload, *type);
// IDocument wants us to ask // IDocument wants us to ask
} else if (type == IDocument::TypeContents) { } else if (type == IDocument::TypeContents) {
// content change, IDocument wants to ask user // content change, IDocument wants to ask user
if (previousReloadAnswer == ReloadNone || previousReloadAnswer == ReloadNoneAndDiff) { if (previousReloadAnswer == ReloadNone || previousReloadAnswer == ReloadNoneAndDiff) {
// answer already given, ignore // answer already given, ignore
success = document->reload(&errorString, IDocument::FlagIgnore, IDocument::TypeContents); success = document->reload(IDocument::FlagIgnore, IDocument::TypeContents);
} else if (previousReloadAnswer == ReloadAll) { } else if (previousReloadAnswer == ReloadAll) {
// answer already given, reload // answer already given, reload
success = document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents); success = document->reload(IDocument::FlagReload, IDocument::TypeContents);
} else { } else {
// Ask about content change // Ask about content change
previousReloadAnswer = reloadPrompt(document->filePath(), document->isModified(), previousReloadAnswer = reloadPrompt(document->filePath(), document->isModified(),
@@ -1239,12 +1238,12 @@ void DocumentManager::checkForReload()
switch (previousReloadAnswer) { switch (previousReloadAnswer) {
case ReloadAll: case ReloadAll:
case ReloadCurrent: case ReloadCurrent:
success = document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents); success = document->reload(IDocument::FlagReload, IDocument::TypeContents);
break; break;
case ReloadSkipCurrent: case ReloadSkipCurrent:
case ReloadNone: case ReloadNone:
case ReloadNoneAndDiff: case ReloadNoneAndDiff:
success = document->reload(&errorString, IDocument::FlagIgnore, IDocument::TypeContents); success = document->reload(IDocument::FlagIgnore, IDocument::TypeContents);
break; break;
case CloseCurrent: case CloseCurrent:
documentsToClose << document; documentsToClose << document;
@@ -1288,10 +1287,10 @@ void DocumentManager::checkForReload()
} }
} }
if (!success) { if (!success) {
QString errorString = success.error();
if (errorString.isEmpty()) if (errorString.isEmpty())
errorStrings << Tr::tr("Cannot reload %1").arg(document->filePath().toUserOutput()); errorString = Tr::tr("Cannot reload %1").arg(document->filePath().toUserOutput());
else errorStrings << errorString;
errorStrings << errorString;
} }
d->m_blockedIDocument = nullptr; d->m_blockedIDocument = nullptr;

View File

@@ -2630,9 +2630,10 @@ void EditorManagerPrivate::revertToSaved(IDocument *document)
return; return;
} }
} }
QString errorString;
if (!document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents)) const expected_str<void> res = document->reload(IDocument::FlagReload, IDocument::TypeContents);
QMessageBox::critical(ICore::dialogParent(), ::Core::Tr::tr("File Error"), errorString); if (!res)
QMessageBox::critical(ICore::dialogParent(), ::Core::Tr::tr("File Error"), res.error());
} }
void EditorManagerPrivate::autoSuspendDocuments() void EditorManagerPrivate::autoSuspendDocuments()

View File

@@ -467,12 +467,11 @@ IDocument::ReloadBehavior IDocument::reloadBehavior(ChangeTrigger trigger, Chang
\sa reloadFinished() \sa reloadFinished()
\sa changed() \sa changed()
*/ */
bool IDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) Utils::expected_str<void> IDocument::reload(ReloadFlag flag, ChangeType type)
{ {
Q_UNUSED(errorString)
Q_UNUSED(flag) Q_UNUSED(flag)
Q_UNUSED(type) Q_UNUSED(type)
return true; return {};
} }
/*! /*!

View File

@@ -100,7 +100,7 @@ public:
void setSuspendAllowed(bool value); void setSuspendAllowed(bool value);
virtual ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const; virtual ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
virtual bool reload(QString *errorString, ReloadFlag flag, ChangeType type); virtual Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type);
void checkPermissions(); void checkPermissions();

View File

@@ -189,11 +189,11 @@ bool FormWindowFile::isSaveAsAllowed() const
return true; return true;
} }
bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType type) expected_str<void> FormWindowFile::reload(ReloadFlag flag, ChangeType type)
{ {
if (flag == FlagIgnore) { if (flag == FlagIgnore) {
if (!m_formWindow || type != TypeContents) if (!m_formWindow || type != TypeContents)
return true; return {};
const bool wasModified = m_formWindow->isDirty(); const bool wasModified = m_formWindow->isDirty();
{ {
Utils::GuardLocker locker(m_modificationChangedGuard); Utils::GuardLocker locker(m_modificationChangedGuard);
@@ -203,13 +203,16 @@ bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty
} }
if (!wasModified) if (!wasModified)
updateIsModified(); updateIsModified();
return true; return {};
} else { } else {
emit aboutToReload(); emit aboutToReload();
QString errorString;
const bool success const bool success
= (open(errorString, filePath(), filePath()) == OpenResult::Success); = (open(&errorString, filePath(), filePath()) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
} }

View File

@@ -33,7 +33,7 @@ public:
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
bool isModified() const override; bool isModified() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
QString fallbackSaveAsFileName() const override; QString fallbackSaveAsFileName() const override;
bool supportsCodec(const QTextCodec *codec) const override; bool supportsCodec(const QTextCodec *codec) const override;

View File

@@ -261,20 +261,22 @@ expected_str<void> DiffEditorDocument::saveImpl(const FilePath &filePath, bool a
void DiffEditorDocument::reload() void DiffEditorDocument::reload()
{ {
if (m_controller) { if (m_controller)
m_controller->requestReload(); m_controller->requestReload();
} else { else
QString errorMessage; reload(Core::IDocument::FlagReload, Core::IDocument::TypeContents);
reload(&errorMessage, Core::IDocument::FlagReload, Core::IDocument::TypeContents);
}
} }
bool DiffEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) expected_str<void> DiffEditorDocument::reload(ReloadFlag flag, ChangeType type)
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
return open(errorString, filePath(), filePath()) == OpenResult::Success; QString errorString;
bool success = open(&errorString, filePath(), filePath()) == OpenResult::Success;
if (!success)
return make_unexpected(errorString);
return {};
} }
Core::IDocument::OpenResult DiffEditorDocument::open(QString *errorString, const FilePath &filePath, Core::IDocument::OpenResult DiffEditorDocument::open(QString *errorString, const FilePath &filePath,
@@ -314,8 +316,7 @@ bool DiffEditorDocument::selectEncoding()
switch (result.action) { switch (result.action) {
case CodecSelectorResult::Reload: { case CodecSelectorResult::Reload: {
setCodec(result.codec); setCodec(result.codec);
QString errorMessage; return bool(reload(Core::IDocument::FlagReload, Core::IDocument::TypeContents));
return reload(&errorMessage, Core::IDocument::FlagReload, Core::IDocument::TypeContents);
} }
case CodecSelectorResult::Save: case CodecSelectorResult::Save:
setCodec(result.codec); setCodec(result.codec);

View File

@@ -61,7 +61,7 @@ public:
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
void reload(); void reload();
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
OpenResult open(QString *errorString, const Utils::FilePath &filePath, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override; const Utils::FilePath &realFilePath) override;
bool selectEncoding(); bool selectEncoding();

View File

@@ -191,8 +191,7 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch),
FilePath::fromString(contentsCopyDir), 0, patchAction)) { FilePath::fromString(contentsCopyDir), 0, patchAction)) {
QString errorString; if (textDocument->reload(FilePath::fromString(contentsCopyFileName)))
if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName)))
m_document->reload(); m_document->reload();
} }
} }

View File

@@ -84,7 +84,7 @@ public:
return BehaviorSilent; return BehaviorSilent;
} }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; expected_str<void> reload(ReloadFlag flag, ChangeType type) final;
private: private:
GenericProject *m_project = nullptr; GenericProject *m_project = nullptr;
@@ -695,15 +695,14 @@ void GenericProject::configureAsExampleProject(Kit *kit)
setup(infoList); setup(infoList);
} }
bool GenericProjectFile::reload(QString *errorString, IDocument::ReloadFlag flag, IDocument::ChangeType type) expected_str<void> GenericProjectFile::reload(IDocument::ReloadFlag flag, IDocument::ChangeType type)
{ {
Q_UNUSED(errorString)
Q_UNUSED(flag) Q_UNUSED(flag)
Q_UNUSED(type) Q_UNUSED(type)
if (Target *t = m_project->activeTarget()) if (Target *t = m_project->activeTarget())
static_cast<GenericBuildSystem *>(t->buildSystem())->refresh(m_options); static_cast<GenericBuildSystem *>(t->buildSystem())->refresh(m_options);
return true; return {};
} }
void GenericProject::editFilesTriggered() void GenericProject::editFilesTriggered()

View File

@@ -25,6 +25,8 @@
#include <QGraphicsSvgItem> #include <QGraphicsSvgItem>
#endif #endif
using namespace Utils;
namespace ImageViewer::Internal { namespace ImageViewer::Internal {
class MovieItem : public QObject, public QGraphicsPixmapItem class MovieItem : public QObject, public QGraphicsPixmapItem
@@ -144,17 +146,19 @@ Core::IDocument::ReloadBehavior ImageViewerFile::reloadBehavior(ChangeTrigger st
return BehaviorAsk; return BehaviorAsk;
} }
bool ImageViewerFile::reload(QString *errorString, expected_str<void> ImageViewerFile::reload(Core::IDocument::ReloadFlag flag,
Core::IDocument::ReloadFlag flag, Core::IDocument::ChangeType type)
Core::IDocument::ChangeType type)
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
emit aboutToReload(); emit aboutToReload();
bool success = (openImpl(errorString, filePath()) == OpenResult::Success); QString errorString;
bool success = (openImpl(&errorString, filePath()) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
QMovie *ImageViewerFile::movie() const QMovie *ImageViewerFile::movie() const

View File

@@ -39,7 +39,7 @@ public:
const Utils::FilePath &realFilePath) override; const Utils::FilePath &realFilePath) override;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
QMovie *movie() const; QMovie *movie() const;

View File

@@ -11,8 +11,6 @@
#include "qmt/config/configcontroller.h" #include "qmt/config/configcontroller.h"
#include "qmt/infrastructure/ioexceptions.h" #include "qmt/infrastructure/ioexceptions.h"
#include "qmt/model_controller/modelcontroller.h"
#include "qmt/model/mdiagram.h"
#include "qmt/project_controller/projectcontroller.h" #include "qmt/project_controller/projectcontroller.h"
#include "qmt/project/project.h" #include "qmt/project/project.h"
@@ -91,24 +89,22 @@ bool ModelDocument::isSaveAsAllowed() const
return true; return true;
} }
bool ModelDocument::reload(QString *errorString, Core::IDocument::ReloadFlag flag, Utils::expected_str<void> ModelDocument::reload(Core::IDocument::ReloadFlag flag,
Core::IDocument::ChangeType type) Core::IDocument::ChangeType type)
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
try { try {
d->documentController->loadProject(filePath()); d->documentController->loadProject(filePath());
} catch (const qmt::FileNotFoundException &ex) { } catch (const qmt::FileNotFoundException &ex) {
*errorString = ex.errorMessage(); return make_unexpected(ex.errorMessage());
return false;
} catch (const qmt::Exception &ex) { } catch (const qmt::Exception &ex) {
*errorString = Tr::tr("Could not open \"%1\" for reading: %2.") return make_unexpected(Tr::tr("Could not open \"%1\" for reading: %2.")
.arg(filePath().toUserOutput(), ex.errorMessage()); .arg(filePath().toUserOutput(), ex.errorMessage()));
return false;
} }
emit contentSet(); emit contentSet();
return true; return {};
} }
ExtDocumentController *ModelDocument::documentController() const ExtDocumentController *ModelDocument::documentController() const

View File

@@ -33,7 +33,7 @@ public:
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
bool isModified() const override; bool isModified() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
ExtDocumentController *documentController() const; ExtDocumentController *documentController() const;

View File

@@ -145,14 +145,13 @@ public:
return BehaviorSilent; return BehaviorSilent;
} }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final expected_str<void> reload(ReloadFlag flag, ChangeType type) final
{ {
Q_UNUSED(errorString)
Q_UNUSED(flag) Q_UNUSED(flag)
Q_UNUSED(type) Q_UNUSED(type)
emit m_project->projectFileIsDirty(filePath()); emit m_project->projectFileIsDirty(filePath());
return true; return {};
} }
private: private:

View File

@@ -45,15 +45,19 @@ Core::IDocument::ReloadBehavior TaskFile::reloadBehavior(ChangeTrigger state, Ch
return BehaviorSilent; return BehaviorSilent;
} }
bool TaskFile::reload(QString *errorString, ReloadFlag flag, ChangeType type) expected_str<void> TaskFile::reload(ReloadFlag flag, ChangeType type)
{ {
Q_UNUSED(flag) Q_UNUSED(flag)
if (type == TypeRemoved) { if (type == TypeRemoved) {
deleteLater(); deleteLater();
return true; return {};
} }
return load(errorString, filePath()); QString errorString;
bool success = load(&errorString, filePath());
if (!success)
return make_unexpected(errorString);
return {};
} }
static Task::TaskType typeFrom(const QString &typeName) static Task::TaskType typeFrom(const QString &typeName)

View File

@@ -24,7 +24,7 @@ public:
TaskFile(QObject *parent); TaskFile(QObject *parent);
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
bool load(QString *errorString, const Utils::FilePath &fileName); bool load(QString *errorString, const Utils::FilePath &fileName);

View File

@@ -929,9 +929,10 @@ void QmakePriFile::save(const QStringList &lines)
QStringList errorStrings; QStringList errorStrings;
Core::IDocument *document = Core::DocumentModel::documentForFilePath(filePath()); Core::IDocument *document = Core::DocumentModel::documentForFilePath(filePath());
if (document) { if (document) {
QString errorString; expected_str<void> res =
if (!document->reload(&errorString, Core::IDocument::FlagReload, Core::IDocument::TypeContents)) document->reload(Core::IDocument::FlagReload, Core::IDocument::TypeContents);
errorStrings << errorString; if (!res)
errorStrings << res.error();
} }
if (!errorStrings.isEmpty()) if (!errorStrings.isEmpty())
QMessageBox::warning(Core::ICore::dialogParent(), Tr::tr("File Error"), QMessageBox::warning(Core::ICore::dialogParent(), Tr::tr("File Error"),

View File

@@ -101,14 +101,13 @@ public:
Q_UNUSED(type) Q_UNUSED(type)
return BehaviorSilent; return BehaviorSilent;
} }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override expected_str<void> reload(ReloadFlag flag, ChangeType type) override
{ {
Q_UNUSED(errorString)
Q_UNUSED(flag) Q_UNUSED(flag)
Q_UNUSED(type) Q_UNUSED(type)
if (m_priFile) if (m_priFile)
m_priFile->scheduleUpdate(); m_priFile->scheduleUpdate();
return true; return {};
} }
void setPriFile(QmakePriFile *priFile) { m_priFile = priFile; } void setPriFile(QmakePriFile *priFile) { m_priFile = priFile; }

View File

@@ -56,7 +56,7 @@ public:
bool shouldAutoSave() const final { return m_shouldAutoSave; } bool shouldAutoSave() const final { return m_shouldAutoSave; }
bool isModified() const final { return m_model.dirty(); } bool isModified() const final { return m_model.dirty(); }
bool isSaveAsAllowed() const final { return true; } bool isSaveAsAllowed() const final { return true; }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; expected_str<void> reload(ReloadFlag flag, ChangeType type) final;
void setFilePath(const FilePath &newName) final; void setFilePath(const FilePath &newName) final;
void setBlockDirtyChanged(bool value) { m_blockDirtyChanged = value; } void setBlockDirtyChanged(bool value) { m_blockDirtyChanged = value; }
@@ -280,15 +280,18 @@ void ResourceEditorImpl::restoreState(const QByteArray &state)
m_resourceEditor->restoreState(splitterState); m_resourceEditor->restoreState(splitterState);
} }
bool ResourceEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) expected_str<void> ResourceEditorDocument::reload(ReloadFlag flag, ChangeType type)
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
emit aboutToReload(); emit aboutToReload();
const bool success = (open(errorString, filePath(), filePath()) == OpenResult::Success); QString errorString;
const bool success = (open(&errorString, filePath(), filePath()) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
void ResourceEditorDocument::dirtyChanged(bool dirty) void ResourceEditorDocument::dirtyChanged(bool dirty)

View File

@@ -47,14 +47,13 @@ public:
return BehaviorSilent; return BehaviorSilent;
} }
bool reload(QString *, ReloadFlag, ChangeType type) final expected_str<void> reload(ReloadFlag, ChangeType) final
{ {
Q_UNUSED(type)
FolderNode *parent = m_node->parentFolderNode(); FolderNode *parent = m_node->parentFolderNode();
QTC_ASSERT(parent, return false); QTC_ASSERT(parent, return make_unexpected(QString()));
parent->replaceSubtree(m_node, std::make_unique<ResourceTopLevelNode>( parent->replaceSubtree(m_node, std::make_unique<ResourceTopLevelNode>(
m_node->filePath(), parent->filePath(), m_node->contents())); m_node->filePath(), parent->filePath(), m_node->contents()));
return true; return {};
} }
private: private:

View File

@@ -110,16 +110,19 @@ bool ScxmlEditorDocument::isModified() const
return m_designWidget && m_designWidget->isDirty(); return m_designWidget && m_designWidget->isDirty();
} }
bool ScxmlEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) Utils::expected_str<void> ScxmlEditorDocument::reload(ReloadFlag flag, ChangeType type)
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
emit aboutToReload(); emit aboutToReload();
emit reloadRequested(errorString, filePath().toString()); QString errorString;
const bool success = errorString->isEmpty(); emit reloadRequested(&errorString, filePath().toString());
const bool success = errorString.isEmpty();
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
bool ScxmlEditorDocument::supportsCodec(const QTextCodec *codec) const bool ScxmlEditorDocument::supportsCodec(const QTextCodec *codec) const

View File

@@ -33,7 +33,7 @@ public:
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
bool isModified() const override; bool isModified() const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
bool supportsCodec(const QTextCodec *codec) const override; bool supportsCodec(const QTextCodec *codec) const override;
// Internal // Internal

View File

@@ -74,19 +74,21 @@ void ObjectsMapDocument::setModified(bool modified)
emit changed(); emit changed();
} }
bool ObjectsMapDocument::reload(QString *errorString, expected_str<void> ObjectsMapDocument::reload(Core::IDocument::ReloadFlag flag,
Core::IDocument::ReloadFlag flag, Core::IDocument::ChangeType type)
Core::IDocument::ChangeType type)
{ {
Q_UNUSED(type); Q_UNUSED(type);
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return {};
emit aboutToReload(); emit aboutToReload();
const bool success = (openImpl(errorString, filePath(), filePath()) == OpenResult::Success); QString errorString;
const bool success = (openImpl(&errorString, filePath(), filePath()) == OpenResult::Success);
if (success) if (success)
setModified(false); setModified(false);
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
bool ObjectsMapDocument::buildObjectsMapTree(const QByteArray &contents) bool ObjectsMapDocument::buildObjectsMapTree(const QByteArray &contents)

View File

@@ -26,7 +26,7 @@ public:
bool isModified() const override { return m_isModified; } bool isModified() const override { return m_isModified; }
void setModified(bool modified); void setModified(bool modified);
bool isSaveAsAllowed() const override { return true; } bool isSaveAsAllowed() const override { return true; }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
bool shouldAutoSave() const override { return true; } bool shouldAutoSave() const override { return true; }
bool setContents(const QByteArray &contents) override; bool setContents(const QByteArray &contents) override;

View File

@@ -791,19 +791,19 @@ Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString,
return OpenResult::Success; return OpenResult::Success;
} }
bool TextDocument::reload(QString *errorString, QTextCodec *codec) expected_str<void> TextDocument::reload(QTextCodec *codec)
{ {
QTC_ASSERT(codec, return false); QTC_ASSERT(codec, return make_unexpected(QString("No codec given")));
setCodec(codec); setCodec(codec);
return reload(errorString); return reload();
} }
bool TextDocument::reload(QString *errorString) expected_str<void> TextDocument::reload()
{ {
return reload(errorString, filePath()); return reload(filePath());
} }
bool TextDocument::reload(QString *errorString, const FilePath &realFilePath) expected_str<void> TextDocument::reload(const FilePath &realFilePath)
{ {
emit aboutToReload(); emit aboutToReload();
auto documentLayout = auto documentLayout =
@@ -811,13 +811,16 @@ bool TextDocument::reload(QString *errorString, const FilePath &realFilePath)
if (documentLayout) if (documentLayout)
documentLayout->documentAboutToReload(this); // removes text marks non-permanently documentLayout->documentAboutToReload(this); // removes text marks non-permanently
bool success = openImpl(errorString, filePath(), realFilePath, /*reload =*/true) QString errorString;
bool success = openImpl(&errorString, filePath(), realFilePath, /*reload =*/true)
== OpenResult::Success; == OpenResult::Success;
if (documentLayout) if (documentLayout)
documentLayout->documentReloaded(this); // re-adds text marks documentLayout->documentReloaded(this); // re-adds text marks
emit reloadFinished(success); emit reloadFinished(success);
return success; if (!success)
return make_unexpected(errorString);
return {};
} }
bool TextDocument::setPlainText(const QString &text) bool TextDocument::setPlainText(const QString &text)
@@ -834,24 +837,24 @@ bool TextDocument::setPlainText(const QString &text)
return true; return true;
} }
bool TextDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) expected_str<void> TextDocument::reload(ReloadFlag flag, ChangeType type)
{ {
if (flag == FlagIgnore) { if (flag == FlagIgnore) {
if (type != TypeContents) if (type != TypeContents)
return true; return {};
const bool wasModified = document()->isModified(); const bool wasModified = document()->isModified();
{ {
Utils::GuardLocker locker(d->m_modificationChangedGuard); GuardLocker locker(d->m_modificationChangedGuard);
// hack to ensure we clean the clear state in QTextDocument // hack to ensure we clean the clear state in QTextDocument
document()->setModified(false); document()->setModified(false);
document()->setModified(true); document()->setModified(true);
} }
if (!wasModified) if (!wasModified)
modificationChanged(true); modificationChanged(true);
return true; return {};
} }
return reload(errorString); return reload();
} }
void TextDocument::resetSyntaxHighlighter(const std::function<SyntaxHighlighter *()> &creator) void TextDocument::resetSyntaxHighlighter(const std::function<SyntaxHighlighter *()> &creator)

View File

@@ -105,7 +105,7 @@ public:
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
bool isModified() const override; bool isModified() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; Utils::expected_str<void> reload(ReloadFlag flag, ChangeType type) override;
void setFilePath(const Utils::FilePath &newName) override; void setFilePath(const Utils::FilePath &newName) override;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
@@ -117,8 +117,8 @@ public:
OpenResult open(QString *errorString, const Utils::FilePath &filePath, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override; const Utils::FilePath &realFilePath) override;
virtual bool reload(QString *errorString); virtual Utils::expected_str<void> reload();
bool reload(QString *errorString, const Utils::FilePath &realFilePath); Utils::expected_str<void> reload(const Utils::FilePath &realFilePath);
bool setPlainText(const QString &text); bool setPlainText(const QString &text);
QTextDocument *document() const; QTextDocument *document() const;
@@ -127,7 +127,7 @@ public:
void resetSyntaxHighlighter(const SyntaxHighLighterCreator &creator); void resetSyntaxHighlighter(const SyntaxHighLighterCreator &creator);
SyntaxHighlighter *syntaxHighlighter() const; SyntaxHighlighter *syntaxHighlighter() const;
bool reload(QString *errorString, QTextCodec *codec); Utils::expected_str<void> reload(QTextCodec *codec);
void cleanWhitespace(const QTextCursor &cursor); void cleanWhitespace(const QTextCursor &cursor);
virtual void triggerPendingUpdates(); virtual void triggerPendingUpdates();

View File

@@ -1862,9 +1862,8 @@ void TextEditorWidget::selectEncoding()
const CodecSelectorResult result = Core::askForCodec(Core::ICore::dialogParent(), doc); const CodecSelectorResult result = Core::askForCodec(Core::ICore::dialogParent(), doc);
switch (result.action) { switch (result.action) {
case Core::CodecSelectorResult::Reload: { case Core::CodecSelectorResult::Reload: {
QString errorString; if (expected_str<void> res = doc->reload(result.codec); !res) {
if (!doc->reload(&errorString, result.codec)) { QMessageBox::critical(this, Tr::tr("File Error"), res.error());
QMessageBox::critical(this, Tr::tr("File Error"), errorString);
break; break;
} }
break; break;