ScriptEditor.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2022, Dylan Katz <dykatz@uw.edu>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/Stream.h>
  7. #include <LibGUI/Dialog.h>
  8. #include <LibGUI/FilePicker.h>
  9. #include <LibGUI/MessageBox.h>
  10. #include <LibGUI/TabWidget.h>
  11. #include <LibSQL/AST/SyntaxHighlighter.h>
  12. #include "ScriptEditor.h"
  13. namespace SQLStudio {
  14. ScriptEditor::ScriptEditor()
  15. {
  16. set_syntax_highlighter(make<SQL::AST::SyntaxHighlighter>());
  17. set_ruler_visible(true);
  18. }
  19. void ScriptEditor::new_script_with_temp_name(String name)
  20. {
  21. set_name(name);
  22. }
  23. ErrorOr<void> ScriptEditor::open_script_from_file(LexicalPath const& file_path)
  24. {
  25. auto file = TRY(Core::Stream::File::open(file_path.string(), Core::Stream::OpenMode::Read));
  26. auto file_size = TRY(file->size());
  27. auto buffer = TRY(ByteBuffer::create_uninitialized(file_size));
  28. if (!file->read_or_error(buffer))
  29. return Error::from_string_literal("Failed to read from file");
  30. set_text({ buffer.bytes() });
  31. m_path = file_path.string();
  32. set_name(file_path.title());
  33. return {};
  34. }
  35. ErrorOr<bool> ScriptEditor::save()
  36. {
  37. if (m_path.is_empty())
  38. return save_as();
  39. auto file = TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Write));
  40. if (!file->write_or_error(text().bytes()))
  41. return Error::from_string_literal("Failed to write to file");
  42. document().set_unmodified();
  43. return true;
  44. }
  45. ErrorOr<bool> ScriptEditor::save_as()
  46. {
  47. auto maybe_save_path = GUI::FilePicker::get_save_filepath(window(), name(), "sql");
  48. if (!maybe_save_path.has_value())
  49. return false;
  50. auto save_path = maybe_save_path.release_value();
  51. auto file = TRY(Core::Stream::File::open(save_path, Core::Stream::OpenMode::Write));
  52. if (!file->write_or_error(text().bytes()))
  53. return Error::from_string_literal("Failed to write to file");
  54. m_path = save_path;
  55. auto lexical_path = LexicalPath(save_path);
  56. set_name(lexical_path.title());
  57. auto parent = static_cast<GUI::TabWidget*>(parent_widget());
  58. if (parent)
  59. parent->set_tab_title(*this, lexical_path.title());
  60. document().set_unmodified();
  61. return true;
  62. }
  63. ErrorOr<bool> ScriptEditor::attempt_to_close()
  64. {
  65. if (!document().is_modified())
  66. return true;
  67. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path.is_empty() ? name() : m_path, document().undo_stack().last_unmodified_timestamp());
  68. switch (result) {
  69. case GUI::Dialog::ExecResult::Yes:
  70. return save();
  71. case GUI::Dialog::ExecResult::No:
  72. return true;
  73. default:
  74. return false;
  75. }
  76. }
  77. }