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