From 02cc2e0f8fca9744327c25a17f2ad3cece7f25fc Mon Sep 17 00:00:00 2001 From: david072 Date: Tue, 31 Oct 2023 20:40:00 +0100 Subject: [PATCH] HackStudio: Don't crash when saving is denied on build When running build while having unsaved changes in HackStudio, it asks whether you want to save the unsaved files with a separate dialog. When you click "Yes" to saving the files, but deny the save-file dialog, HackStudio would crash, since we were expecting there to be a file to save to. Now, we check whether a file was picked, and if not, we abort the build. --- Userland/DevTools/HackStudio/EditorWrapper.cpp | 10 ++++++++-- Userland/DevTools/HackStudio/EditorWrapper.h | 2 +- Userland/DevTools/HackStudio/HackStudioWidget.cpp | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Userland/DevTools/HackStudio/EditorWrapper.cpp b/Userland/DevTools/HackStudio/EditorWrapper.cpp index b52ad842db..82edf5e7d8 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.cpp +++ b/Userland/DevTools/HackStudio/EditorWrapper.cpp @@ -71,18 +71,24 @@ void EditorWrapper::set_filename(DeprecatedString const& filename) update_diff(); } -void EditorWrapper::save() +bool EditorWrapper::save() { if (filename().is_empty()) { auto file_picker_action = GUI::CommonActions::make_save_as_action([&](auto&) { Optional save_path = GUI::FilePicker::get_save_filepath(window(), "file"sv, "txt"sv, project_root().value()); - set_filename(save_path.value()); + if (save_path.has_value()) + set_filename(save_path.value()); }); file_picker_action->activate(); + + if (filename().is_empty()) + return false; } editor().write_to_file(filename()).release_value_but_fixme_should_propagate_errors(); update_diff(); editor().update(); + + return true; } void EditorWrapper::update_diff() diff --git a/Userland/DevTools/HackStudio/EditorWrapper.h b/Userland/DevTools/HackStudio/EditorWrapper.h index ba11dea944..30f97a4c8f 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.h +++ b/Userland/DevTools/HackStudio/EditorWrapper.h @@ -30,7 +30,7 @@ public: Editor& editor() { return *m_editor; } Editor const& editor() const { return *m_editor; } - void save(); + bool save(); LanguageClient& language_client(); diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index f625226f96..d0b70fa481 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -1678,7 +1678,8 @@ HackStudioWidget::ContinueDecision HackStudioWidget::warn_unsaved_changes(Deprec if (result == GUI::MessageBox::ExecResult::Yes) { for (auto& editor_wrapper : m_all_editor_wrappers) { if (editor_wrapper->editor().document().is_modified()) { - editor_wrapper->save(); + if (!editor_wrapper->save()) + return ContinueDecision::No; } } }