ProjectFile.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ProjectFile.h"
  7. #include <LibCore/File.h>
  8. #include <string.h>
  9. namespace HackStudio {
  10. ProjectFile::ProjectFile(const String& name)
  11. : m_name(name)
  12. {
  13. }
  14. GUI::TextDocument& ProjectFile::document() const
  15. {
  16. create_document_if_needed();
  17. VERIFY(m_document);
  18. return *m_document;
  19. }
  20. int ProjectFile::vertical_scroll_value() const
  21. {
  22. return m_vertical_scroll_value;
  23. }
  24. void ProjectFile::vertical_scroll_value(int vertical_scroll_value)
  25. {
  26. m_vertical_scroll_value = vertical_scroll_value;
  27. }
  28. int ProjectFile::horizontal_scroll_value() const
  29. {
  30. return m_horizontal_scroll_value;
  31. }
  32. void ProjectFile::horizontal_scroll_value(int horizontal_scroll_value)
  33. {
  34. m_horizontal_scroll_value = horizontal_scroll_value;
  35. }
  36. CodeDocument& ProjectFile::code_document() const
  37. {
  38. create_document_if_needed();
  39. VERIFY(m_document);
  40. return *m_document;
  41. }
  42. void ProjectFile::create_document_if_needed() const
  43. {
  44. if (m_document)
  45. return;
  46. m_document = CodeDocument::create(m_name);
  47. auto file_or_error = Core::File::open(m_name, Core::OpenMode::ReadOnly);
  48. if (file_or_error.is_error()) {
  49. warnln("Couldn't open '{}': {}", m_name, file_or_error.error());
  50. // This is okay though, we'll just go with an empty document and create the file when saving.
  51. return;
  52. }
  53. auto& file = *file_or_error.value();
  54. m_could_render_text = m_document->set_text(file.read_all());
  55. }
  56. }