From 22e192e45ab5ec681952e48caf3298485b9c86b6 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 20 Sep 2024 11:36:34 +0200 Subject: [PATCH] Core: Make IDocument::save() return an expected_str That's today's posh way for success-bool-with-error-string. Change-Id: Ica56592766a401701d7e9c59cf07fc0929e399e1 Reviewed-by: Eike Ziller --- src/plugins/android/androidmanifesteditor.cpp | 4 ++-- src/plugins/bineditor/bineditorplugin.cpp | 15 +++++------- .../clangcodemodel/test/clangdtests.cpp | 4 ++-- .../clangformat/clangformatconfigwidget.cpp | 3 +-- .../clangtools/virtualfilesystemoverlay.cpp | 5 ++-- .../compilerexplorereditor.cpp | 16 +++++-------- .../coreplugin/basefilewizardfactory.cpp | 2 +- src/plugins/coreplugin/documentmanager.cpp | 5 ++-- src/plugins/coreplugin/idocument.cpp | 24 +++++++++---------- src/plugins/coreplugin/idocument.h | 7 +++--- src/plugins/cppeditor/cppeditordocument.cpp | 6 ++--- src/plugins/cppeditor/cppeditordocument.h | 5 +--- src/plugins/designer/formwindowfile.cpp | 20 +++++++++------- src/plugins/designer/formwindowfile.h | 2 +- src/plugins/diffeditor/diffeditordocument.cpp | 12 +++++----- src/plugins/diffeditor/diffeditordocument.h | 2 +- src/plugins/modeleditor/modeldocument.cpp | 15 +++++------- src/plugins/modeleditor/modeldocument.h | 2 +- .../projectexplorer/jsonwizard/jsonwizard.cpp | 2 +- .../assetexporterplugin/assetexporter.cpp | 5 ++-- .../assetexporterplugin/assetexporterview.cpp | 7 +++--- .../assetexporterplugin/assetexporterview.h | 2 +- src/plugins/resourceeditor/resourceeditor.cpp | 14 +++++------ .../scxmleditor/scxmleditordocument.cpp | 11 ++++----- src/plugins/scxmleditor/scxmleditordocument.h | 2 +- src/plugins/squish/objectsmapdocument.cpp | 17 ++++++------- src/plugins/squish/objectsmapdocument.h | 2 +- src/plugins/texteditor/refactoringchanges.cpp | 2 +- src/plugins/texteditor/textdocument.cpp | 11 +++++---- src/plugins/texteditor/textdocument.h | 2 +- src/plugins/vcsbase/submiteditorfile.cpp | 15 ++++++------ src/plugins/vcsbase/submiteditorfile.h | 2 +- 32 files changed, 113 insertions(+), 130 deletions(-) diff --git a/src/plugins/android/androidmanifesteditor.cpp b/src/plugins/android/androidmanifesteditor.cpp index 9e59768cff7..7517ea18d51 100644 --- a/src/plugins/android/androidmanifesteditor.cpp +++ b/src/plugins/android/androidmanifesteditor.cpp @@ -1517,10 +1517,10 @@ private: bool isSaveAsAllowed() const override { return false; } - bool saveImpl(QString *errorString, const FilePath &filePath, bool autoSave = false) override + expected_str saveImpl(const FilePath &filePath, bool autoSave) override { m_editorWidget->preSave(); - bool result = TextDocument::saveImpl(errorString, filePath, autoSave); + expected_str result = TextDocument::saveImpl(filePath, autoSave); m_editorWidget->postSave(); return result; } diff --git a/src/plugins/bineditor/bineditorplugin.cpp b/src/plugins/bineditor/bineditorplugin.cpp index e3c2a67d700..fdf923f6c1f 100644 --- a/src/plugins/bineditor/bineditorplugin.cpp +++ b/src/plugins/bineditor/bineditorplugin.cpp @@ -121,7 +121,7 @@ public: bool isSaveAsAllowed() const final { return true; } bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) final; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) final; void fetchData(quint64 address) const { if (m_fetchDataHandler) m_fetchDataHandler(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; } -bool BinEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str BinEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) { - QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive - if (expected_str res = save(this->filePath(), filePath); !res) { - if (errorString) - *errorString = res.error(); - return false; - } + QTC_ASSERT(!autoSave, return {}); // bineditor does not support autosave - it would be a bit expensive + if (expected_str res = save(this->filePath(), filePath); !res) + return res; setFilePath(filePath); - return true; + return {}; } class BinEditorImpl final : public IEditor, public EditorService diff --git a/src/plugins/clangcodemodel/test/clangdtests.cpp b/src/plugins/clangcodemodel/test/clangdtests.cpp index ea08e90900b..b0662cfca64 100644 --- a/src/plugins/clangcodemodel/test/clangdtests.cpp +++ b/src/plugins/clangcodemodel/test/clangdtests.cpp @@ -1953,8 +1953,8 @@ void ClangdTestCompletion::testCompleteAfterProjectChange() EditorManager::openEditor(project()->projectFilePath())); QVERIFY(proFileEditor); proFileEditor->insert("DEFINES += PROJECT_CONFIGURATION_1\n"); - QString saveError; - QVERIFY2(proFileEditor->document()->save(&saveError), qPrintable(saveError)); + const expected_str res = proFileEditor->document()->save(); + QVERIFY2(res, qPrintable(res.error())); QVERIFY(waitForSignalOrTimeout(project(), &Project::anyParsingFinished, timeOutInMs())); QVERIFY(waitForSignalOrTimeout(LanguageClientManager::instance(), &LanguageClientManager::clientRemoved, diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 2fb12251778..56a51ef83de 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -329,8 +329,7 @@ void ClangFormatConfigWidget::apply() if (!m_editorWidget->isEnabled()) return; - QString errorString; - m_editor->document()->save(&errorString, m_config->filePath()); + m_editor->document()->save(m_config->filePath()); } TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget( diff --git a/src/plugins/clangtools/virtualfilesystemoverlay.cpp b/src/plugins/clangtools/virtualfilesystemoverlay.cpp index 1c26f59a2d9..037059d9b4b 100644 --- a/src/plugins/clangtools/virtualfilesystemoverlay.cpp +++ b/src/plugins/clangtools/virtualfilesystemoverlay.cpp @@ -38,12 +38,11 @@ void VirtualFileSystemOverlay::update() if (saved.revision != document->document()->revision()) { saved.path.removeRecursively(); saved.revision = document->document()->revision(); - QString error; saved.path = m_root.filePath(doc->filePath().fileName() + ".auto"); while (saved.path.exists()) saved.path = saved.path.stringAppended(".1"); - if (!doc->save(&error, saved.path, true)) { - qCDebug(LOG) << error; + if (Utils::expected_str res = doc->save(saved.path, true); !res) { + qCDebug(LOG) << res.error(); continue; } } diff --git a/src/plugins/compilerexplorer/compilerexplorereditor.cpp b/src/plugins/compilerexplorer/compilerexplorereditor.cpp index 986ea457a5c..ce6969ddbbb 100644 --- a/src/plugins/compilerexplorer/compilerexplorereditor.cpp +++ b/src/plugins/compilerexplorer/compilerexplorereditor.cpp @@ -154,9 +154,7 @@ public: const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; - bool saveImpl(QString *errorString, - const Utils::FilePath &filePath = Utils::FilePath(), - bool autoSave = false) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; bool setContents(const QByteArray &contents) override; @@ -411,7 +409,7 @@ Core::IDocument::OpenResult JsonSettingsDocument::open(QString *errorString, return OpenResult::Success; } -bool JsonSettingsDocument::saveImpl(QString *errorString, const FilePath &newFilePath, bool autoSave) +expected_str JsonSettingsDocument::saveImpl(const FilePath &newFilePath, bool autoSave) { Store store; @@ -435,14 +433,12 @@ bool JsonSettingsDocument::saveImpl(QString *errorString, const FilePath &newFil setFilePath(newFilePath); } - auto result = path.writeFileContents(jsonFromStore(store)); - if (!result && errorString) { - *errorString = result.error(); - return false; - } + expected_str result = path.writeFileContents(jsonFromStore(store)); + if (!result) + return make_unexpected(result.error()); emit changed(); - return true; + return {}; } bool JsonSettingsDocument::isModified() const diff --git a/src/plugins/coreplugin/basefilewizardfactory.cpp b/src/plugins/coreplugin/basefilewizardfactory.cpp index 0e4652b7e53..60000388ad6 100644 --- a/src/plugins/coreplugin/basefilewizardfactory.cpp +++ b/src/plugins/coreplugin/basefilewizardfactory.cpp @@ -174,7 +174,7 @@ bool BaseFileWizardFactory::postGenerateOpenEditors(const GeneratedFiles &l, QSt return false; } editor->document()->formatContents(); - editor->document()->save(nullptr); + editor->document()->save(); } } return true; diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index bc666e0cdf6..89a4d24976b 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -715,8 +715,7 @@ bool DocumentManager::saveDocument(IDocument *document, 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 - QString errorString; - if (!document->save(&errorString, savePath, false)) { + if (const expected_str res = document->save(savePath, false); !res) { if (isReadOnly) { QFile ofi(savePath.toString()); // Check whether the existing file is writable @@ -727,7 +726,7 @@ bool DocumentManager::saveDocument(IDocument *document, *isReadOnly = false; } 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: ret = false; } diff --git a/src/plugins/coreplugin/idocument.cpp b/src/plugins/coreplugin/idocument.cpp index ff766f60697..84495188491 100644 --- a/src/plugins/coreplugin/idocument.cpp +++ b/src/plugins/coreplugin/idocument.cpp @@ -343,14 +343,14 @@ IDocument::OpenResult IDocument::open(QString *errorString, const Utils::FilePat \sa saved() \sa filePath() */ -bool IDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) +expected_str 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); - const bool success = saveImpl(errorString, savePath, autoSave); - if (success) + const expected_str res = saveImpl(savePath, autoSave); + if (res) 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 user-requested saves. - Use \a errorString to return an error message if saving failed. - - Returns whether saving was successful. + Returns whether saving was successful, including an error message when it was not. The default implementation does nothing and returns \c false. */ -bool IDocument::saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) +expected_str IDocument::saveImpl(const FilePath &filePath, bool autoSave) { - Q_UNUSED(errorString) Q_UNUSED(filePath) 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) { - if (!save(errorString, filePath, true)) + const expected_str res = save(filePath, true); + if (!res) { + *errorString = res.error(); return false; + } d->autoSavePath = filePath; return true; } diff --git a/src/plugins/coreplugin/idocument.h b/src/plugins/coreplugin/idocument.h index 8fbca1d02f7..02d306282af 100644 --- a/src/plugins/coreplugin/idocument.h +++ b/src/plugins/coreplugin/idocument.h @@ -68,7 +68,7 @@ public: 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 save(const Utils::FilePath &filePath = {}, bool autoSave = false); virtual QByteArray contents() const; virtual bool setContents(const QByteArray &contents); @@ -131,9 +131,8 @@ signals: void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName); protected: - virtual bool saveImpl(QString *errorString, - const Utils::FilePath &filePath = Utils::FilePath(), - bool autoSave = false); + virtual Utils::expected_str + saveImpl(const Utils::FilePath &filePath = {}, bool autoSave = false); private: Internal::IDocumentPrivate *d; diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index a9d331d2c88..dc94c8fe64b 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -489,10 +489,10 @@ TextEditor::TabSettings CppEditorDocument::tabSettings() const return indenter()->tabSettings().value_or(TextEditor::TextDocument::tabSettings()); } -bool CppEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str CppEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) { if (!indenter()->formatOnSave() || autoSave) - return TextEditor::TextDocument::saveImpl(errorString, filePath, autoSave); + return TextEditor::TextDocument::saveImpl(filePath, autoSave); auto *layout = qobject_cast(document()->documentLayout()); const int documentRevision = layout->lastSaveRevision; @@ -530,7 +530,7 @@ bool CppEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, settings.m_cleanWhitespace = false; setStorageSettings(settings); - return TextEditor::TextDocument::saveImpl(errorString, filePath, autoSave); + return TextEditor::TextDocument::saveImpl(filePath, autoSave); } bool CppEditorDocument::usesClangd() const diff --git a/src/plugins/cppeditor/cppeditordocument.h b/src/plugins/cppeditor/cppeditordocument.h index 82f2415cd9d..fc379fb1ae6 100644 --- a/src/plugins/cppeditor/cppeditordocument.h +++ b/src/plugins/cppeditor/cppeditordocument.h @@ -67,12 +67,9 @@ signals: protected: void applyFontSettings() override; - bool saveImpl(QString *errorString, - const Utils::FilePath &filePath = Utils::FilePath(), - bool autoSave = false) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: - void invalidateFormatterCache(); void onFilePathChanged(const Utils::FilePath &oldPath, const Utils::FilePath &newPath); void onMimeTypeChanged(); diff --git a/src/plugins/designer/formwindowfile.cpp b/src/plugins/designer/formwindowfile.cpp index aab54d96313..9138cee5e64 100644 --- a/src/plugins/designer/formwindowfile.cpp +++ b/src/plugins/designer/formwindowfile.cpp @@ -84,31 +84,35 @@ Core::IDocument::OpenResult FormWindowFile::open(QString *errorString, return OpenResult::Success; } -bool FormWindowFile::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str FormWindowFile::saveImpl(const FilePath &filePath, bool autoSave) { - QTC_ASSERT(m_formWindow, return false); + QTC_ASSERT(m_formWindow, return make_unexpected(QString())); if (filePath.isEmpty()) - return false; + return make_unexpected(QString()); const QString oldFormName = m_formWindow->fileName(); if (!autoSave) m_formWindow->setFileName(filePath.toString()); - const bool writeOK = writeFile(filePath, errorString); + QString errorString; + const bool writeOK = writeFile(filePath, &errorString); m_shouldAutoSave = false; - if (autoSave) - return writeOK; + if (autoSave) { + if (writeOK) + return {}; + return make_unexpected(errorString); + } if (!writeOK) { m_formWindow->setFileName(oldFormName); - return false; + return make_unexpected(errorString); } m_formWindow->setDirty(false); setFilePath(filePath); updateIsModified(); - return true; + return {}; } QByteArray FormWindowFile::contents() const diff --git a/src/plugins/designer/formwindowfile.h b/src/plugins/designer/formwindowfile.h index a392e1ee59a..55690ed77da 100644 --- a/src/plugins/designer/formwindowfile.h +++ b/src/plugins/designer/formwindowfile.h @@ -52,7 +52,7 @@ public: void updateIsModified(); protected: - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: void slotFormWindowRemoved(QDesignerFormWindowInterface *w); diff --git a/src/plugins/diffeditor/diffeditordocument.cpp b/src/plugins/diffeditor/diffeditordocument.cpp index 9b456a152df..2768dacddd9 100644 --- a/src/plugins/diffeditor/diffeditordocument.cpp +++ b/src/plugins/diffeditor/diffeditordocument.cpp @@ -234,18 +234,18 @@ bool DiffEditorDocument::isSaveAsAllowed() const return state() == LoadOK; } -bool DiffEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str DiffEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) { - Q_UNUSED(errorString) Q_UNUSED(autoSave) + QString errorString; 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) - return false; + return make_unexpected(errorString); setController(nullptr); setDescription({}); @@ -256,7 +256,7 @@ bool DiffEditorDocument::saveImpl(QString *errorString, const FilePath &filePath setPreferredDisplayName({}); emit temporaryStateChanged(); - return true; + return {}; } void DiffEditorDocument::reload() diff --git a/src/plugins/diffeditor/diffeditordocument.h b/src/plugins/diffeditor/diffeditordocument.h index 9dcf017dccd..512fb60b2d5 100644 --- a/src/plugins/diffeditor/diffeditordocument.h +++ b/src/plugins/diffeditor/diffeditordocument.h @@ -75,7 +75,7 @@ signals: void descriptionChanged(); protected: - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: void beginReload(); diff --git a/src/plugins/modeleditor/modeldocument.cpp b/src/plugins/modeleditor/modeldocument.cpp index 00630341f98..9143301e26d 100644 --- a/src/plugins/modeleditor/modeldocument.cpp +++ b/src/plugins/modeleditor/modeldocument.cpp @@ -19,7 +19,7 @@ #include #include -using Utils::FilePath; +using namespace Utils; namespace ModelEditor { namespace Internal { @@ -54,19 +54,16 @@ Core::IDocument::OpenResult ModelDocument::open(QString *errorString, return result; } -bool ModelDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str ModelDocument::saveImpl(const FilePath &filePath, bool autoSave) { - if (!d->documentController) { - *errorString = Tr::tr("No model loaded. Cannot save."); - return false; - } + if (!d->documentController) + return make_unexpected(Tr::tr("No model loaded. Cannot save.")); d->documentController->projectController()->setFileName(filePath); try { d->documentController->projectController()->save(); } catch (const qmt::Exception &ex) { - *errorString = ex.errorMessage(); - return false; + return make_unexpected(ex.errorMessage()); } if (autoSave) { @@ -76,7 +73,7 @@ bool ModelDocument::saveImpl(QString *errorString, const FilePath &filePath, boo emit changed(); } - return true; + return {}; } bool ModelDocument::shouldAutoSave() const diff --git a/src/plugins/modeleditor/modeldocument.h b/src/plugins/modeleditor/modeldocument.h index 6b2037e007d..e53c161445e 100644 --- a/src/plugins/modeleditor/modeldocument.h +++ b/src/plugins/modeleditor/modeldocument.h @@ -40,7 +40,7 @@ public: OpenResult load(QString *errorString, const Utils::FilePath &fileName); protected: - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: ModelDocumentPrivate *d; diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp index 3d846604ba5..ae4592f97fe 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp @@ -435,7 +435,7 @@ void JsonWizard::openFiles(const JsonWizard::GeneratorFiles &files) bool openedSomething = stringValue("DoNotOpenFile") == "true"; static const auto formatFile = [](Core::IEditor *editor) { editor->document()->formatContents(); - editor->document()->save(nullptr); + editor->document()->save(); }; for (const JsonWizard::GeneratorFile &f : files) { const Core::GeneratedFile &file = f.file; diff --git a/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp b/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp index df938129c24..096002131bf 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp +++ b/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp @@ -242,10 +242,9 @@ void AssetExporter::onQmlFileLoaded() .arg(designDocument->displayName())); } else { exportComponent(m_view->rootModelNode()); - QString error; - if (!m_view->saveQmlFile(&error)) { + if (Utils::expected_str res = m_view->saveQmlFile(); !res) { 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); diff --git a/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.cpp b/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.cpp index e81dfa519f3..70de208da33 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.cpp +++ b/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.cpp @@ -49,13 +49,14 @@ bool AssetExporterView::loadQmlFile(const Utils::FilePath &path, uint timeoutSec return true; } -bool AssetExporterView::saveQmlFile(QString *error) const +Utils::expected_str AssetExporterView::saveQmlFile() const { if (!m_currentEditor) { 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) diff --git a/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.h b/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.h index a6f08dd49e2..c61649875db 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.h +++ b/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.h @@ -28,7 +28,7 @@ public: AssetExporterView(ExternalDependenciesInterface &externalDependencies); bool loadQmlFile(const Utils::FilePath &path, uint timeoutSecs = 10); - bool saveQmlFile(QString *error) const; + Utils::expected_str saveQmlFile() const; void modelAttached(Model *model) override; void instanceInformationsChanged(const QMultiHash &informationChangeHash) override; diff --git a/src/plugins/resourceeditor/resourceeditor.cpp b/src/plugins/resourceeditor/resourceeditor.cpp index 87ab217740e..e428a6e1baf 100644 --- a/src/plugins/resourceeditor/resourceeditor.cpp +++ b/src/plugins/resourceeditor/resourceeditor.cpp @@ -67,7 +67,7 @@ signals: void loaded(bool success); private: - bool saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) final; + Utils::expected_str saveImpl(const FilePath &filePath, bool autoSave) final; void dirtyChanged(bool); RelativeResourceModel m_model; @@ -209,22 +209,20 @@ IDocument::OpenResult ResourceEditorDocument::open(QString *errorString, return OpenResult::Success; } -bool ResourceEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str ResourceEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) { if (debugResourceEditorW) qDebug() << ">ResourceEditorW::saveImpl: " << filePath; if (filePath.isEmpty()) - return false; + return make_unexpected(QString()); // FIXME: better message m_blockDirtyChanged = true; m_model.setFilePath(filePath); if (!m_model.save()) { - if (errorString) - *errorString = m_model.errorMessage(); m_model.setFilePath(this->filePath()); m_blockDirtyChanged = false; - return false; + return make_unexpected(m_model.errorMessage()); } m_shouldAutoSave = false; @@ -232,14 +230,14 @@ bool ResourceEditorDocument::saveImpl(QString *errorString, const FilePath &file m_model.setFilePath(this->filePath()); m_model.setDirty(true); m_blockDirtyChanged = false; - return true; + return {}; } setFilePath(filePath); m_blockDirtyChanged = false; emit changed(); - return true; + return {}; } bool ResourceEditorDocument::setContents(const QByteArray &contents) diff --git a/src/plugins/scxmleditor/scxmleditordocument.cpp b/src/plugins/scxmleditor/scxmleditordocument.cpp index 0af7c6c0a6d..e0f0adac86e 100644 --- a/src/plugins/scxmleditor/scxmleditordocument.cpp +++ b/src/plugins/scxmleditor/scxmleditordocument.cpp @@ -58,23 +58,22 @@ Core::IDocument::OpenResult ScxmlEditorDocument::open(QString *errorString, return OpenResult::Success; } -bool ScxmlEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +Utils::expected_str ScxmlEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) { if (filePath.isEmpty()) - return false; + return make_unexpected(QString()); bool dirty = m_designWidget->isDirty(); m_designWidget->setFileName(filePath.toString()); if (!m_designWidget->save()) { - *errorString = m_designWidget->errorMessage(); m_designWidget->setFileName(this->filePath().toString()); - return false; + return make_unexpected(m_designWidget->errorMessage()); } if (autoSave) { m_designWidget->setFileName(this->filePath().toString()); m_designWidget->save(); - return true; + return {}; } setFilePath(filePath); @@ -82,7 +81,7 @@ bool ScxmlEditorDocument::saveImpl(QString *errorString, const FilePath &filePat if (dirty != m_designWidget->isDirty()) emit changed(); - return true; + return {}; } void ScxmlEditorDocument::setFilePath(const FilePath &newName) diff --git a/src/plugins/scxmleditor/scxmleditordocument.h b/src/plugins/scxmleditor/scxmleditordocument.h index 1bb4d06669e..5c6b8f1a3f4 100644 --- a/src/plugins/scxmleditor/scxmleditordocument.h +++ b/src/plugins/scxmleditor/scxmleditordocument.h @@ -46,7 +46,7 @@ signals: void reloadRequested(QString *errorString, const QString &); protected: - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: QPointer m_designWidget; diff --git a/src/plugins/squish/objectsmapdocument.cpp b/src/plugins/squish/objectsmapdocument.cpp index ba5f70b0cd1..7d168049125 100644 --- a/src/plugins/squish/objectsmapdocument.cpp +++ b/src/plugins/squish/objectsmapdocument.cpp @@ -13,6 +13,8 @@ #include +using namespace Utils; + namespace Squish { namespace Internal { @@ -40,25 +42,20 @@ Core::IDocument::OpenResult ObjectsMapDocument::open(QString *errorString, return result; } -bool ObjectsMapDocument::saveImpl(QString *errorString, - const Utils::FilePath &filePath, - bool autoSave) +expected_str ObjectsMapDocument::saveImpl(const FilePath &filePath, bool autoSave) { if (filePath.isEmpty()) - return false; + return make_unexpected(QString()); const bool writeOk = writeFile(filePath); - if (!writeOk) { - if (errorString) - *errorString = Tr::tr("Failed to write \"%1\"").arg(filePath.toUserOutput()); - return false; - } + if (!writeOk) + return make_unexpected(Tr::tr("Failed to write \"%1\"").arg(filePath.toUserOutput())); if (!autoSave) { setModified(false); setFilePath(filePath); } - return true; + return {}; } Utils::FilePath ObjectsMapDocument::fallbackSaveAsPath() const diff --git a/src/plugins/squish/objectsmapdocument.h b/src/plugins/squish/objectsmapdocument.h index 9a5bcb4cb4d..aa87f864d72 100644 --- a/src/plugins/squish/objectsmapdocument.h +++ b/src/plugins/squish/objectsmapdocument.h @@ -34,7 +34,7 @@ public: ObjectsMapModel *model() const { return m_contentModel; } protected: - bool saveImpl(QString *errorString, const Utils::FilePath &fileName, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &fileName, bool autoSave) override; private: OpenResult openImpl(QString *error, diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp index d1a11b4e9ca..2025d7bbf13 100644 --- a/src/plugins/texteditor/refactoringchanges.cpp +++ b/src/plugins/texteditor/refactoringchanges.cpp @@ -280,7 +280,7 @@ bool RefactoringFile::apply() fileChanged(); if (withUnmodifiedEditor && EditorManager::autoSaveAfterRefactoring()) - m_editor->textDocument()->save(nullptr, m_filePath, false); + m_editor->textDocument()->save(m_filePath, false); } } diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index c8c088006cf..afd35a49888 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -589,7 +589,7 @@ QTextDocument *TextDocument::document() const * 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()). */ -bool TextDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +Utils::expected_str TextDocument::saveImpl(const FilePath &filePath, bool autoSave) { 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 if (autoSave && undos < d->m_document.availableUndoSteps()) { @@ -660,16 +661,16 @@ bool TextDocument::saveImpl(QString *errorString, const FilePath &filePath, bool } if (!ok) - return false; + return make_unexpected(errorString); d->m_autoSaveRevision = d->m_document.revision(); if (autoSave) - return true; + return {}; // inform about the new filename d->m_document.setModified(false); // also triggers update of the block revisions setFilePath(filePath.absoluteFilePath()); emit changed(); - return true; + return {}; } QByteArray TextDocument::contents() const diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index f08d18f2456..300d5654a2a 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -162,7 +162,7 @@ signals: protected: virtual void applyFontSettings(); - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: OpenResult openImpl(QString *errorString, diff --git a/src/plugins/vcsbase/submiteditorfile.cpp b/src/plugins/vcsbase/submiteditorfile.cpp index fcde5593571..229f82a748f 100644 --- a/src/plugins/vcsbase/submiteditorfile.cpp +++ b/src/plugins/vcsbase/submiteditorfile.cpp @@ -67,20 +67,21 @@ void SubmitEditorFile::setModified(bool modified) emit changed(); } -bool SubmitEditorFile::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) +expected_str SubmitEditorFile::saveImpl(const FilePath &filePath, bool autoSave) { FileSaver saver(filePath, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); saver.write(m_editor->fileContents()); - if (!saver.finalize(errorString)) - return false; + QString errorString; + if (!saver.finalize(&errorString)) + return make_unexpected(errorString); if (autoSave) - return true; + return {}; setFilePath(filePath.absoluteFilePath()); setModified(false); - if (!errorString->isEmpty()) - return false; + if (!errorString.isEmpty()) + return make_unexpected(errorString); emit changed(); - return true; + return {}; } IDocument::ReloadBehavior SubmitEditorFile::reloadBehavior(ChangeTrigger state, ChangeType type) const diff --git a/src/plugins/vcsbase/submiteditorfile.h b/src/plugins/vcsbase/submiteditorfile.h index d2b71abb921..e2771cdc229 100644 --- a/src/plugins/vcsbase/submiteditorfile.h +++ b/src/plugins/vcsbase/submiteditorfile.h @@ -28,7 +28,7 @@ public: void setModified(bool modified = true); protected: - bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; + Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) override; private: bool m_modified;