diff --git a/Userland/DevTools/SQLStudio/ScriptEditor.cpp b/Userland/DevTools/SQLStudio/ScriptEditor.cpp index 5b5d82f278..0ad9807301 100644 --- a/Userland/DevTools/SQLStudio/ScriptEditor.cpp +++ b/Userland/DevTools/SQLStudio/ScriptEditor.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Dylan Katz + * Copyright (c) 2022, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -37,15 +38,22 @@ ErrorOr ScriptEditor::open_script_from_file(LexicalPath const& file_path) return {}; } +static ErrorOr save_text_to_file(StringView filename, DeprecatedString text) +{ + auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Write)); + + if (!text.is_empty()) + TRY(file->write_entire_buffer(text.bytes())); + + return {}; +} + ErrorOr ScriptEditor::save() { if (m_path.is_empty()) return save_as(); - auto file = TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Write)); - auto editor_text = text(); - TRY(file->write_entire_buffer(editor_text.bytes())); - + TRY(save_text_to_file(m_path, text())); document().set_unmodified(); return true; } @@ -55,12 +63,9 @@ ErrorOr ScriptEditor::save_as() auto maybe_save_path = GUI::FilePicker::get_save_filepath(window(), name(), "sql"); if (!maybe_save_path.has_value()) return false; + auto save_path = maybe_save_path.release_value(); - - auto file = TRY(Core::Stream::File::open(save_path, Core::Stream::OpenMode::Write)); - auto editor_text = text(); - TRY(file->write_entire_buffer(editor_text.bytes())); - + TRY(save_text_to_file(save_path, text())); m_path = save_path; auto lexical_path = LexicalPath(save_path);