ProjectFile.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/Stream.h>
  8. namespace HackStudio {
  9. ProjectFile::ProjectFile(DeprecatedString const& name)
  10. : m_name(name)
  11. {
  12. }
  13. GUI::TextDocument& ProjectFile::document() const
  14. {
  15. create_document_if_needed();
  16. VERIFY(m_document);
  17. return *m_document;
  18. }
  19. int ProjectFile::vertical_scroll_value() const
  20. {
  21. return m_vertical_scroll_value;
  22. }
  23. void ProjectFile::vertical_scroll_value(int vertical_scroll_value)
  24. {
  25. m_vertical_scroll_value = vertical_scroll_value;
  26. }
  27. int ProjectFile::horizontal_scroll_value() const
  28. {
  29. return m_horizontal_scroll_value;
  30. }
  31. void ProjectFile::horizontal_scroll_value(int horizontal_scroll_value)
  32. {
  33. m_horizontal_scroll_value = horizontal_scroll_value;
  34. }
  35. CodeDocument& ProjectFile::code_document() const
  36. {
  37. create_document_if_needed();
  38. VERIFY(m_document);
  39. return *m_document;
  40. }
  41. void ProjectFile::create_document_if_needed() const
  42. {
  43. if (m_document)
  44. return;
  45. m_document = CodeDocument::create(m_name);
  46. auto file_or_error = Core::Stream::File::open(m_name, Core::Stream::OpenMode::Read);
  47. if (file_or_error.is_error()) {
  48. warnln("Couldn't open '{}': {}", m_name, file_or_error.error());
  49. // This is okay though, we'll just go with an empty document and create the file when saving.
  50. return;
  51. }
  52. auto& file = *file_or_error.value();
  53. m_could_render_text = m_document->set_text(file.read_until_eof().release_value_but_fixme_should_propagate_errors());
  54. }
  55. }