Core: Make IDocument::save() return an expected_str<void>

That's today's posh way for success-bool-with-error-string.

Change-Id: Ica56592766a401701d7e9c59cf07fc0929e399e1
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-09-20 11:36:34 +02:00
parent 8c7d4414d4
commit 22e192e45a
32 changed files with 113 additions and 130 deletions

View File

@@ -1517,10 +1517,10 @@ private:
bool isSaveAsAllowed() const override { return false; } bool isSaveAsAllowed() const override { return false; }
bool saveImpl(QString *errorString, const FilePath &filePath, bool autoSave = false) override expected_str<void> saveImpl(const FilePath &filePath, bool autoSave) override
{ {
m_editorWidget->preSave(); m_editorWidget->preSave();
bool result = TextDocument::saveImpl(errorString, filePath, autoSave); expected_str<void> result = TextDocument::saveImpl(filePath, autoSave);
m_editorWidget->postSave(); m_editorWidget->postSave();
return result; return result;
} }

View File

@@ -121,7 +121,7 @@ public:
bool isSaveAsAllowed() const final { return true; } bool isSaveAsAllowed() const final { return true; }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final;
bool saveImpl(QString *errorString, 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); }
void requestNewWindow(quint64 address) { if (m_newWindowRequestHandler) m_newWindowRequestHandler(address); } void requestNewWindow(quint64 address) { if (m_newWindowRequestHandler) m_newWindowRequestHandler(address); }
@@ -2194,16 +2194,13 @@ bool BinEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType
return success; return success;
} }
bool BinEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> BinEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive QTC_ASSERT(!autoSave, return {}); // bineditor does not support autosave - it would be a bit expensive
if (expected_str<void> res = save(this->filePath(), filePath); !res) { if (expected_str<void> res = save(this->filePath(), filePath); !res)
if (errorString) return res;
*errorString = res.error();
return false;
}
setFilePath(filePath); setFilePath(filePath);
return true; return {};
} }
class BinEditorImpl final : public IEditor, public EditorService class BinEditorImpl final : public IEditor, public EditorService

View File

@@ -1953,8 +1953,8 @@ void ClangdTestCompletion::testCompleteAfterProjectChange()
EditorManager::openEditor(project()->projectFilePath())); EditorManager::openEditor(project()->projectFilePath()));
QVERIFY(proFileEditor); QVERIFY(proFileEditor);
proFileEditor->insert("DEFINES += PROJECT_CONFIGURATION_1\n"); proFileEditor->insert("DEFINES += PROJECT_CONFIGURATION_1\n");
QString saveError; const expected_str<void> res = proFileEditor->document()->save();
QVERIFY2(proFileEditor->document()->save(&saveError), qPrintable(saveError)); QVERIFY2(res, qPrintable(res.error()));
QVERIFY(waitForSignalOrTimeout(project(), &Project::anyParsingFinished, timeOutInMs())); QVERIFY(waitForSignalOrTimeout(project(), &Project::anyParsingFinished, timeOutInMs()));
QVERIFY(waitForSignalOrTimeout(LanguageClientManager::instance(), QVERIFY(waitForSignalOrTimeout(LanguageClientManager::instance(),
&LanguageClientManager::clientRemoved, &LanguageClientManager::clientRemoved,

View File

@@ -329,8 +329,7 @@ void ClangFormatConfigWidget::apply()
if (!m_editorWidget->isEnabled()) if (!m_editorWidget->isEnabled())
return; return;
QString errorString; m_editor->document()->save(m_config->filePath());
m_editor->document()->save(&errorString, m_config->filePath());
} }
TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget( TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget(

View File

@@ -38,12 +38,11 @@ void VirtualFileSystemOverlay::update()
if (saved.revision != document->document()->revision()) { if (saved.revision != document->document()->revision()) {
saved.path.removeRecursively(); saved.path.removeRecursively();
saved.revision = document->document()->revision(); saved.revision = document->document()->revision();
QString error;
saved.path = m_root.filePath(doc->filePath().fileName() + ".auto"); saved.path = m_root.filePath(doc->filePath().fileName() + ".auto");
while (saved.path.exists()) while (saved.path.exists())
saved.path = saved.path.stringAppended(".1"); saved.path = saved.path.stringAppended(".1");
if (!doc->save(&error, saved.path, true)) { if (Utils::expected_str<void> res = doc->save(saved.path, true); !res) {
qCDebug(LOG) << error; qCDebug(LOG) << res.error();
continue; continue;
} }
} }

View File

@@ -154,9 +154,7 @@ public:
const Utils::FilePath &filePath, const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override; const Utils::FilePath &realFilePath) override;
bool saveImpl(QString *errorString, Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
const Utils::FilePath &filePath = Utils::FilePath(),
bool autoSave = false) override;
bool setContents(const QByteArray &contents) override; bool setContents(const QByteArray &contents) override;
@@ -411,7 +409,7 @@ Core::IDocument::OpenResult JsonSettingsDocument::open(QString *errorString,
return OpenResult::Success; return OpenResult::Success;
} }
bool JsonSettingsDocument::saveImpl(QString *errorString, const FilePath &newFilePath, bool autoSave) expected_str<void> JsonSettingsDocument::saveImpl(const FilePath &newFilePath, bool autoSave)
{ {
Store store; Store store;
@@ -435,14 +433,12 @@ bool JsonSettingsDocument::saveImpl(QString *errorString, const FilePath &newFil
setFilePath(newFilePath); setFilePath(newFilePath);
} }
auto result = path.writeFileContents(jsonFromStore(store)); expected_str<qint64> result = path.writeFileContents(jsonFromStore(store));
if (!result && errorString) { if (!result)
*errorString = result.error(); return make_unexpected(result.error());
return false;
}
emit changed(); emit changed();
return true; return {};
} }
bool JsonSettingsDocument::isModified() const bool JsonSettingsDocument::isModified() const

View File

@@ -174,7 +174,7 @@ bool BaseFileWizardFactory::postGenerateOpenEditors(const GeneratedFiles &l, QSt
return false; return false;
} }
editor->document()->formatContents(); editor->document()->formatContents();
editor->document()->save(nullptr); editor->document()->save();
} }
} }
return true; return true;

View File

@@ -715,8 +715,7 @@ bool DocumentManager::saveDocument(IDocument *document,
expectFileChange(savePath); // This only matters to other IDocuments which refer to this file expectFileChange(savePath); // This only matters to other IDocuments which refer to this file
bool addWatcher = removeDocument(document); // So that our own IDocument gets no notification at all bool addWatcher = removeDocument(document); // So that our own IDocument gets no notification at all
QString errorString; if (const expected_str<void> res = document->save(savePath, false); !res) {
if (!document->save(&errorString, savePath, false)) {
if (isReadOnly) { if (isReadOnly) {
QFile ofi(savePath.toString()); QFile ofi(savePath.toString());
// Check whether the existing file is writable // Check whether the existing file is writable
@@ -727,7 +726,7 @@ bool DocumentManager::saveDocument(IDocument *document,
*isReadOnly = false; *isReadOnly = false;
} }
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"),
Tr::tr("Error while saving file: %1").arg(errorString)); Tr::tr("Error while saving file: %1").arg(res.error()));
out: out:
ret = false; ret = false;
} }

View File

@@ -343,14 +343,14 @@ IDocument::OpenResult IDocument::open(QString *errorString, const Utils::FilePat
\sa saved() \sa saved()
\sa filePath() \sa filePath()
*/ */
bool IDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) expected_str<void> IDocument::save(const FilePath &filePath, bool autoSave)
{ {
const Utils::FilePath savePath = filePath.isEmpty() ? this->filePath() : filePath; const FilePath savePath = filePath.isEmpty() ? this->filePath() : filePath;
emit aboutToSave(savePath, autoSave); emit aboutToSave(savePath, autoSave);
const bool success = saveImpl(errorString, savePath, autoSave); const expected_str<void> res = saveImpl(savePath, autoSave);
if (success) if (res)
emit saved(savePath, autoSave); emit saved(savePath, autoSave);
return success; return res;
} }
/*! /*!
@@ -360,18 +360,15 @@ bool IDocument::save(QString *errorString, const Utils::FilePath &filePath, bool
document should avoid cleanups or other operations that it does for document should avoid cleanups or other operations that it does for
user-requested saves. user-requested saves.
Use \a errorString to return an error message if saving failed. Returns whether saving was successful, including an error message when it was not.
Returns whether saving was successful.
The default implementation does nothing and returns \c false. The default implementation does nothing and returns \c false.
*/ */
bool IDocument::saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) expected_str<void> IDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
Q_UNUSED(errorString)
Q_UNUSED(filePath) Q_UNUSED(filePath)
Q_UNUSED(autoSave) Q_UNUSED(autoSave)
return false; return make_unexpected(Tr::tr("Not implemented"));
} }
/*! /*!
@@ -653,8 +650,11 @@ void IDocument::setMimeType(const QString &mimeType)
*/ */
bool IDocument::autoSave(QString *errorString, const FilePath &filePath) bool IDocument::autoSave(QString *errorString, const FilePath &filePath)
{ {
if (!save(errorString, filePath, true)) const expected_str<void> res = save(filePath, true);
if (!res) {
*errorString = res.error();
return false; return false;
}
d->autoSavePath = filePath; d->autoSavePath = filePath;
return true; return true;
} }

View File

@@ -68,7 +68,7 @@ public:
virtual OpenResult open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath); virtual OpenResult open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
bool save(QString *errorString, const Utils::FilePath &filePath = Utils::FilePath(), bool autoSave = false); Utils::expected_str<void> save(const Utils::FilePath &filePath = {}, bool autoSave = false);
virtual QByteArray contents() const; virtual QByteArray contents() const;
virtual bool setContents(const QByteArray &contents); virtual bool setContents(const QByteArray &contents);
@@ -131,9 +131,8 @@ signals:
void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName); void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName);
protected: protected:
virtual bool saveImpl(QString *errorString, virtual Utils::expected_str<void>
const Utils::FilePath &filePath = Utils::FilePath(), saveImpl(const Utils::FilePath &filePath = {}, bool autoSave = false);
bool autoSave = false);
private: private:
Internal::IDocumentPrivate *d; Internal::IDocumentPrivate *d;

View File

@@ -489,10 +489,10 @@ TextEditor::TabSettings CppEditorDocument::tabSettings() const
return indenter()->tabSettings().value_or(TextEditor::TextDocument::tabSettings()); return indenter()->tabSettings().value_or(TextEditor::TextDocument::tabSettings());
} }
bool CppEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> CppEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
if (!indenter()->formatOnSave() || autoSave) if (!indenter()->formatOnSave() || autoSave)
return TextEditor::TextDocument::saveImpl(errorString, filePath, autoSave); return TextEditor::TextDocument::saveImpl(filePath, autoSave);
auto *layout = qobject_cast<TextEditor::TextDocumentLayout *>(document()->documentLayout()); auto *layout = qobject_cast<TextEditor::TextDocumentLayout *>(document()->documentLayout());
const int documentRevision = layout->lastSaveRevision; const int documentRevision = layout->lastSaveRevision;
@@ -530,7 +530,7 @@ bool CppEditorDocument::saveImpl(QString *errorString, const FilePath &filePath,
settings.m_cleanWhitespace = false; settings.m_cleanWhitespace = false;
setStorageSettings(settings); setStorageSettings(settings);
return TextEditor::TextDocument::saveImpl(errorString, filePath, autoSave); return TextEditor::TextDocument::saveImpl(filePath, autoSave);
} }
bool CppEditorDocument::usesClangd() const bool CppEditorDocument::usesClangd() const

View File

@@ -67,12 +67,9 @@ signals:
protected: protected:
void applyFontSettings() override; void applyFontSettings() override;
bool saveImpl(QString *errorString, Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
const Utils::FilePath &filePath = Utils::FilePath(),
bool autoSave = false) override;
private: private:
void invalidateFormatterCache(); void invalidateFormatterCache();
void onFilePathChanged(const Utils::FilePath &oldPath, const Utils::FilePath &newPath); void onFilePathChanged(const Utils::FilePath &oldPath, const Utils::FilePath &newPath);
void onMimeTypeChanged(); void onMimeTypeChanged();

View File

@@ -84,31 +84,35 @@ Core::IDocument::OpenResult FormWindowFile::open(QString *errorString,
return OpenResult::Success; return OpenResult::Success;
} }
bool FormWindowFile::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> FormWindowFile::saveImpl(const FilePath &filePath, bool autoSave)
{ {
QTC_ASSERT(m_formWindow, return false); QTC_ASSERT(m_formWindow, return make_unexpected(QString()));
if (filePath.isEmpty()) if (filePath.isEmpty())
return false; return make_unexpected(QString());
const QString oldFormName = m_formWindow->fileName(); const QString oldFormName = m_formWindow->fileName();
if (!autoSave) if (!autoSave)
m_formWindow->setFileName(filePath.toString()); m_formWindow->setFileName(filePath.toString());
const bool writeOK = writeFile(filePath, errorString); QString errorString;
const bool writeOK = writeFile(filePath, &errorString);
m_shouldAutoSave = false; m_shouldAutoSave = false;
if (autoSave) if (autoSave) {
return writeOK; if (writeOK)
return {};
return make_unexpected(errorString);
}
if (!writeOK) { if (!writeOK) {
m_formWindow->setFileName(oldFormName); m_formWindow->setFileName(oldFormName);
return false; return make_unexpected(errorString);
} }
m_formWindow->setDirty(false); m_formWindow->setDirty(false);
setFilePath(filePath); setFilePath(filePath);
updateIsModified(); updateIsModified();
return true; return {};
} }
QByteArray FormWindowFile::contents() const QByteArray FormWindowFile::contents() const

View File

@@ -52,7 +52,7 @@ public:
void updateIsModified(); void updateIsModified();
protected: protected:
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
private: private:
void slotFormWindowRemoved(QDesignerFormWindowInterface *w); void slotFormWindowRemoved(QDesignerFormWindowInterface *w);

View File

@@ -234,18 +234,18 @@ bool DiffEditorDocument::isSaveAsAllowed() const
return state() == LoadOK; return state() == LoadOK;
} }
bool DiffEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> DiffEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
Q_UNUSED(errorString)
Q_UNUSED(autoSave) Q_UNUSED(autoSave)
QString errorString;
if (state() != LoadOK) if (state() != LoadOK)
return false; return make_unexpected(errorString);
const bool ok = write(filePath, format(), plainText(), errorString); const bool ok = write(filePath, format(), plainText(), &errorString);
if (!ok) if (!ok)
return false; return make_unexpected(errorString);
setController(nullptr); setController(nullptr);
setDescription({}); setDescription({});
@@ -256,7 +256,7 @@ bool DiffEditorDocument::saveImpl(QString *errorString, const FilePath &filePath
setPreferredDisplayName({}); setPreferredDisplayName({});
emit temporaryStateChanged(); emit temporaryStateChanged();
return true; return {};
} }
void DiffEditorDocument::reload() void DiffEditorDocument::reload()

View File

@@ -75,7 +75,7 @@ signals:
void descriptionChanged(); void descriptionChanged();
protected: protected:
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
private: private:
void beginReload(); void beginReload();

View File

@@ -19,7 +19,7 @@
#include <utils/id.h> #include <utils/id.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
using Utils::FilePath; using namespace Utils;
namespace ModelEditor { namespace ModelEditor {
namespace Internal { namespace Internal {
@@ -54,19 +54,16 @@ Core::IDocument::OpenResult ModelDocument::open(QString *errorString,
return result; return result;
} }
bool ModelDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> ModelDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
if (!d->documentController) { if (!d->documentController)
*errorString = Tr::tr("No model loaded. Cannot save."); return make_unexpected(Tr::tr("No model loaded. Cannot save."));
return false;
}
d->documentController->projectController()->setFileName(filePath); d->documentController->projectController()->setFileName(filePath);
try { try {
d->documentController->projectController()->save(); d->documentController->projectController()->save();
} catch (const qmt::Exception &ex) { } catch (const qmt::Exception &ex) {
*errorString = ex.errorMessage(); return make_unexpected(ex.errorMessage());
return false;
} }
if (autoSave) { if (autoSave) {
@@ -76,7 +73,7 @@ bool ModelDocument::saveImpl(QString *errorString, const FilePath &filePath, boo
emit changed(); emit changed();
} }
return true; return {};
} }
bool ModelDocument::shouldAutoSave() const bool ModelDocument::shouldAutoSave() const

View File

@@ -40,7 +40,7 @@ public:
OpenResult load(QString *errorString, const Utils::FilePath &fileName); OpenResult load(QString *errorString, const Utils::FilePath &fileName);
protected: protected:
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
private: private:
ModelDocumentPrivate *d; ModelDocumentPrivate *d;

View File

@@ -435,7 +435,7 @@ void JsonWizard::openFiles(const JsonWizard::GeneratorFiles &files)
bool openedSomething = stringValue("DoNotOpenFile") == "true"; bool openedSomething = stringValue("DoNotOpenFile") == "true";
static const auto formatFile = [](Core::IEditor *editor) { static const auto formatFile = [](Core::IEditor *editor) {
editor->document()->formatContents(); editor->document()->formatContents();
editor->document()->save(nullptr); editor->document()->save();
}; };
for (const JsonWizard::GeneratorFile &f : files) { for (const JsonWizard::GeneratorFile &f : files) {
const Core::GeneratedFile &file = f.file; const Core::GeneratedFile &file = f.file;

View File

@@ -242,10 +242,9 @@ void AssetExporter::onQmlFileLoaded()
.arg(designDocument->displayName())); .arg(designDocument->displayName()));
} else { } else {
exportComponent(m_view->rootModelNode()); exportComponent(m_view->rootModelNode());
QString error; if (Utils::expected_str<void> res = m_view->saveQmlFile(); !res) {
if (!m_view->saveQmlFile(&error)) {
ExportNotification::addError(tr("Error saving component file. %1") ExportNotification::addError(tr("Error saving component file. %1")
.arg(error.isEmpty()? tr("Unknown") : error)); .arg(res.error().isEmpty()? tr("Unknown") : res.error()));
} }
} }
notifyProgress((m_totalFileCount - m_exportFiles.count()) * 0.8 / m_totalFileCount); notifyProgress((m_totalFileCount - m_exportFiles.count()) * 0.8 / m_totalFileCount);

View File

@@ -49,13 +49,14 @@ bool AssetExporterView::loadQmlFile(const Utils::FilePath &path, uint timeoutSec
return true; return true;
} }
bool AssetExporterView::saveQmlFile(QString *error) const Utils::expected_str<void> AssetExporterView::saveQmlFile() const
{ {
if (!m_currentEditor) { if (!m_currentEditor) {
qCDebug(loggerWarn) << "Saving QML file failed. No editor."; qCDebug(loggerWarn) << "Saving QML file failed. No editor.";
return false; return Utils::make_unexpected(QString("Saving QML file failed. No editor."));
} }
return m_currentEditor->document()->save(error);
return m_currentEditor->document()->save();
} }
void AssetExporterView::modelAttached(Model *model) void AssetExporterView::modelAttached(Model *model)

View File

@@ -28,7 +28,7 @@ public:
AssetExporterView(ExternalDependenciesInterface &externalDependencies); AssetExporterView(ExternalDependenciesInterface &externalDependencies);
bool loadQmlFile(const Utils::FilePath &path, uint timeoutSecs = 10); bool loadQmlFile(const Utils::FilePath &path, uint timeoutSecs = 10);
bool saveQmlFile(QString *error) const; Utils::expected_str<void> saveQmlFile() const;
void modelAttached(Model *model) override; void modelAttached(Model *model) override;
void instanceInformationsChanged(const QMultiHash<ModelNode, InformationName> &informationChangeHash) override; void instanceInformationsChanged(const QMultiHash<ModelNode, InformationName> &informationChangeHash) override;

View File

@@ -67,7 +67,7 @@ signals:
void loaded(bool success); void loaded(bool success);
private: private:
bool saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) final; Utils::expected_str<void> saveImpl(const FilePath &filePath, bool autoSave) final;
void dirtyChanged(bool); void dirtyChanged(bool);
RelativeResourceModel m_model; RelativeResourceModel m_model;
@@ -209,22 +209,20 @@ IDocument::OpenResult ResourceEditorDocument::open(QString *errorString,
return OpenResult::Success; return OpenResult::Success;
} }
bool ResourceEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> ResourceEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
if (debugResourceEditorW) if (debugResourceEditorW)
qDebug() << ">ResourceEditorW::saveImpl: " << filePath; qDebug() << ">ResourceEditorW::saveImpl: " << filePath;
if (filePath.isEmpty()) if (filePath.isEmpty())
return false; return make_unexpected(QString()); // FIXME: better message
m_blockDirtyChanged = true; m_blockDirtyChanged = true;
m_model.setFilePath(filePath); m_model.setFilePath(filePath);
if (!m_model.save()) { if (!m_model.save()) {
if (errorString)
*errorString = m_model.errorMessage();
m_model.setFilePath(this->filePath()); m_model.setFilePath(this->filePath());
m_blockDirtyChanged = false; m_blockDirtyChanged = false;
return false; return make_unexpected(m_model.errorMessage());
} }
m_shouldAutoSave = false; m_shouldAutoSave = false;
@@ -232,14 +230,14 @@ bool ResourceEditorDocument::saveImpl(QString *errorString, const FilePath &file
m_model.setFilePath(this->filePath()); m_model.setFilePath(this->filePath());
m_model.setDirty(true); m_model.setDirty(true);
m_blockDirtyChanged = false; m_blockDirtyChanged = false;
return true; return {};
} }
setFilePath(filePath); setFilePath(filePath);
m_blockDirtyChanged = false; m_blockDirtyChanged = false;
emit changed(); emit changed();
return true; return {};
} }
bool ResourceEditorDocument::setContents(const QByteArray &contents) bool ResourceEditorDocument::setContents(const QByteArray &contents)

View File

@@ -58,23 +58,22 @@ Core::IDocument::OpenResult ScxmlEditorDocument::open(QString *errorString,
return OpenResult::Success; return OpenResult::Success;
} }
bool ScxmlEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) Utils::expected_str<void> ScxmlEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
if (filePath.isEmpty()) if (filePath.isEmpty())
return false; return make_unexpected(QString());
bool dirty = m_designWidget->isDirty(); bool dirty = m_designWidget->isDirty();
m_designWidget->setFileName(filePath.toString()); m_designWidget->setFileName(filePath.toString());
if (!m_designWidget->save()) { if (!m_designWidget->save()) {
*errorString = m_designWidget->errorMessage();
m_designWidget->setFileName(this->filePath().toString()); m_designWidget->setFileName(this->filePath().toString());
return false; return make_unexpected(m_designWidget->errorMessage());
} }
if (autoSave) { if (autoSave) {
m_designWidget->setFileName(this->filePath().toString()); m_designWidget->setFileName(this->filePath().toString());
m_designWidget->save(); m_designWidget->save();
return true; return {};
} }
setFilePath(filePath); setFilePath(filePath);
@@ -82,7 +81,7 @@ bool ScxmlEditorDocument::saveImpl(QString *errorString, const FilePath &filePat
if (dirty != m_designWidget->isDirty()) if (dirty != m_designWidget->isDirty())
emit changed(); emit changed();
return true; return {};
} }
void ScxmlEditorDocument::setFilePath(const FilePath &newName) void ScxmlEditorDocument::setFilePath(const FilePath &newName)

View File

@@ -46,7 +46,7 @@ signals:
void reloadRequested(QString *errorString, const QString &); void reloadRequested(QString *errorString, const QString &);
protected: protected:
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
private: private:
QPointer<Common::MainWidget> m_designWidget; QPointer<Common::MainWidget> m_designWidget;

View File

@@ -13,6 +13,8 @@
#include <QtCore5Compat/QTextCodec> #include <QtCore5Compat/QTextCodec>
using namespace Utils;
namespace Squish { namespace Squish {
namespace Internal { namespace Internal {
@@ -40,25 +42,20 @@ Core::IDocument::OpenResult ObjectsMapDocument::open(QString *errorString,
return result; return result;
} }
bool ObjectsMapDocument::saveImpl(QString *errorString, expected_str<void> ObjectsMapDocument::saveImpl(const FilePath &filePath, bool autoSave)
const Utils::FilePath &filePath,
bool autoSave)
{ {
if (filePath.isEmpty()) if (filePath.isEmpty())
return false; return make_unexpected(QString());
const bool writeOk = writeFile(filePath); const bool writeOk = writeFile(filePath);
if (!writeOk) { if (!writeOk)
if (errorString) return make_unexpected(Tr::tr("Failed to write \"%1\"").arg(filePath.toUserOutput()));
*errorString = Tr::tr("Failed to write \"%1\"").arg(filePath.toUserOutput());
return false;
}
if (!autoSave) { if (!autoSave) {
setModified(false); setModified(false);
setFilePath(filePath); setFilePath(filePath);
} }
return true; return {};
} }
Utils::FilePath ObjectsMapDocument::fallbackSaveAsPath() const Utils::FilePath ObjectsMapDocument::fallbackSaveAsPath() const

View File

@@ -34,7 +34,7 @@ public:
ObjectsMapModel *model() const { return m_contentModel; } ObjectsMapModel *model() const { return m_contentModel; }
protected: protected:
bool saveImpl(QString *errorString, const Utils::FilePath &fileName, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &fileName, bool autoSave) override;
private: private:
OpenResult openImpl(QString *error, OpenResult openImpl(QString *error,

View File

@@ -280,7 +280,7 @@ bool RefactoringFile::apply()
fileChanged(); fileChanged();
if (withUnmodifiedEditor && EditorManager::autoSaveAfterRefactoring()) if (withUnmodifiedEditor && EditorManager::autoSaveAfterRefactoring())
m_editor->textDocument()->save(nullptr, m_filePath, false); m_editor->textDocument()->save(m_filePath, false);
} }
} }

View File

@@ -589,7 +589,7 @@ QTextDocument *TextDocument::document() const
* If \a autoSave is true, the cursor will be restored and some signals suppressed * If \a autoSave is true, the cursor will be restored and some signals suppressed
* and we do not clean up the text file (cleanWhitespace(), ensureFinalNewLine()). * and we do not clean up the text file (cleanWhitespace(), ensureFinalNewLine()).
*/ */
bool TextDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) Utils::expected_str<void> TextDocument::saveImpl(const FilePath &filePath, bool autoSave)
{ {
QTextCursor cursor(&d->m_document); QTextCursor cursor(&d->m_document);
@@ -644,7 +644,8 @@ bool TextDocument::saveImpl(QString *errorString, const FilePath &filePath, bool
} }
} }
const bool ok = write(filePath, saveFormat, plainText(), errorString); QString errorString;
const bool ok = write(filePath, saveFormat, plainText(), &errorString);
// restore text cursor and scroll bar positions // restore text cursor and scroll bar positions
if (autoSave && undos < d->m_document.availableUndoSteps()) { if (autoSave && undos < d->m_document.availableUndoSteps()) {
@@ -660,16 +661,16 @@ bool TextDocument::saveImpl(QString *errorString, const FilePath &filePath, bool
} }
if (!ok) if (!ok)
return false; return make_unexpected(errorString);
d->m_autoSaveRevision = d->m_document.revision(); d->m_autoSaveRevision = d->m_document.revision();
if (autoSave) if (autoSave)
return true; return {};
// inform about the new filename // inform about the new filename
d->m_document.setModified(false); // also triggers update of the block revisions d->m_document.setModified(false); // also triggers update of the block revisions
setFilePath(filePath.absoluteFilePath()); setFilePath(filePath.absoluteFilePath());
emit changed(); emit changed();
return true; return {};
} }
QByteArray TextDocument::contents() const QByteArray TextDocument::contents() const

View File

@@ -162,7 +162,7 @@ signals:
protected: protected:
virtual void applyFontSettings(); virtual void applyFontSettings();
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
private: private:
OpenResult openImpl(QString *errorString, OpenResult openImpl(QString *errorString,

View File

@@ -67,20 +67,21 @@ void SubmitEditorFile::setModified(bool modified)
emit changed(); emit changed();
} }
bool SubmitEditorFile::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) expected_str<void> SubmitEditorFile::saveImpl(const FilePath &filePath, bool autoSave)
{ {
FileSaver saver(filePath, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); FileSaver saver(filePath, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
saver.write(m_editor->fileContents()); saver.write(m_editor->fileContents());
if (!saver.finalize(errorString)) QString errorString;
return false; if (!saver.finalize(&errorString))
return make_unexpected(errorString);
if (autoSave) if (autoSave)
return true; return {};
setFilePath(filePath.absoluteFilePath()); setFilePath(filePath.absoluteFilePath());
setModified(false); setModified(false);
if (!errorString->isEmpty()) if (!errorString.isEmpty())
return false; return make_unexpected(errorString);
emit changed(); emit changed();
return true; return {};
} }
IDocument::ReloadBehavior SubmitEditorFile::reloadBehavior(ChangeTrigger state, ChangeType type) const IDocument::ReloadBehavior SubmitEditorFile::reloadBehavior(ChangeTrigger state, ChangeType type) const

View File

@@ -28,7 +28,7 @@ public:
void setModified(bool modified = true); void setModified(bool modified = true);
protected: protected:
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; Utils::expected_str<void> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
private: private:
bool m_modified; bool m_modified;