ProjectFile.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ProjectFile.h"
  27. #include <LibCore/File.h>
  28. #include <string.h>
  29. namespace HackStudio {
  30. ProjectFile::ProjectFile(const String& name)
  31. : m_name(name)
  32. {
  33. }
  34. GUI::TextDocument& ProjectFile::document() const
  35. {
  36. create_document_if_needed();
  37. VERIFY(m_document);
  38. return *m_document;
  39. }
  40. int ProjectFile::vertical_scroll_value() const
  41. {
  42. return m_vertical_scroll_value;
  43. }
  44. void ProjectFile::vertical_scroll_value(int vertical_scroll_value)
  45. {
  46. m_vertical_scroll_value = vertical_scroll_value;
  47. }
  48. int ProjectFile::horizontal_scroll_value() const
  49. {
  50. return m_horizontal_scroll_value;
  51. }
  52. void ProjectFile::horizontal_scroll_value(int horizontal_scroll_value)
  53. {
  54. m_horizontal_scroll_value = horizontal_scroll_value;
  55. }
  56. CodeDocument& ProjectFile::code_document() const
  57. {
  58. create_document_if_needed();
  59. VERIFY(m_document);
  60. return *m_document;
  61. }
  62. void ProjectFile::create_document_if_needed() const
  63. {
  64. if (m_document)
  65. return;
  66. m_document = CodeDocument::create(m_name);
  67. auto file_or_error = Core::File::open(m_name, Core::File::ReadOnly);
  68. if (file_or_error.is_error()) {
  69. warnln("Couldn't open '{}': {}", m_name, file_or_error.error());
  70. // This is okay though, we'll just go with an empty document and create the file when saving.
  71. return;
  72. }
  73. auto& file = *file_or_error.value();
  74. m_could_render_text = m_document->set_text(file.read_all());
  75. }
  76. }