|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com> |
| 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 | + |
| 27 | +#include "FileDB.h" |
| 28 | + |
| 29 | +#include <AK/LexicalPath.h> |
| 30 | +#include <LibCore/File.h> |
| 31 | + |
| 32 | +RefPtr<const GUI::TextDocument> FileDB::get(const String& file_name) const |
| 33 | +{ |
| 34 | + auto absolute_path = to_absolute_path(file_name); |
| 35 | + auto document_optional = m_open_files.get(absolute_path); |
| 36 | + if (!document_optional.has_value()) |
| 37 | + return create_from_filesystem(absolute_path); |
| 38 | + |
| 39 | + return document_optional.value(); |
| 40 | +} |
| 41 | + |
| 42 | +RefPtr<GUI::TextDocument> FileDB::get(const String& file_name) |
| 43 | +{ |
| 44 | + auto absolute_path = to_absolute_path(file_name); |
| 45 | + return adopt(*const_cast<GUI::TextDocument*>(reinterpret_cast<const FileDB*>(this)->get(absolute_path).leak_ref())); |
| 46 | +} |
| 47 | + |
| 48 | +bool FileDB::is_open(String file_name) const |
| 49 | +{ |
| 50 | + return m_open_files.contains(to_absolute_path(file_name)); |
| 51 | +} |
| 52 | + |
| 53 | +bool FileDB::add(const String& file_name, int fd) |
| 54 | +{ |
| 55 | + auto document = create_from_fd(fd); |
| 56 | + if (!document) |
| 57 | + return false; |
| 58 | + |
| 59 | + m_open_files.set(to_absolute_path(file_name), document.release_nonnull()); |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +String FileDB::to_absolute_path(const String& file_name) const |
| 64 | +{ |
| 65 | + if (LexicalPath { file_name }.is_absolute()) { |
| 66 | + return file_name; |
| 67 | + } |
| 68 | + ASSERT(!m_project_root.is_null()); |
| 69 | + return LexicalPath { String::formatted("{}/{}", m_project_root, file_name) }.string(); |
| 70 | +} |
| 71 | + |
| 72 | +RefPtr<GUI::TextDocument> FileDB::create_from_filesystem(const String& file_name) const |
| 73 | +{ |
| 74 | + auto file = Core::File::open(to_absolute_path(file_name), Core::IODevice::ReadOnly); |
| 75 | + if (file.is_error()) { |
| 76 | + dbgln("failed to create document for {} from filesystem", file_name); |
| 77 | + return nullptr; |
| 78 | + } |
| 79 | + return create_from_file(*file.value()); |
| 80 | +} |
| 81 | + |
| 82 | +RefPtr<GUI::TextDocument> FileDB::create_from_fd(int fd) const |
| 83 | +{ |
| 84 | + auto file = Core::File::construct(); |
| 85 | + if (!file->open(fd, Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) { |
| 86 | + errno = file->error(); |
| 87 | + perror("open"); |
| 88 | + dbgln("Failed to open project file"); |
| 89 | + return nullptr; |
| 90 | + } |
| 91 | + return create_from_file(*file); |
| 92 | +} |
| 93 | + |
| 94 | +class DefaultDocumentClient final : public GUI::TextDocument::Client { |
| 95 | +public: |
| 96 | + virtual ~DefaultDocumentClient() override = default; |
| 97 | + virtual void document_did_append_line() override {}; |
| 98 | + virtual void document_did_insert_line(size_t) override {}; |
| 99 | + virtual void document_did_remove_line(size_t) override {}; |
| 100 | + virtual void document_did_remove_all_lines() override {}; |
| 101 | + virtual void document_did_change() override {}; |
| 102 | + virtual void document_did_set_text() override {}; |
| 103 | + virtual void document_did_set_cursor(const GUI::TextPosition&) override {}; |
| 104 | + |
| 105 | + virtual bool is_automatic_indentation_enabled() const override { return false; } |
| 106 | + virtual int soft_tab_width() const override { return 4; } |
| 107 | +}; |
| 108 | +static DefaultDocumentClient s_default_document_client; |
| 109 | + |
| 110 | +RefPtr<GUI::TextDocument> FileDB::create_from_file(Core::File& file) const |
| 111 | +{ |
| 112 | + auto content = file.read_all(); |
| 113 | + StringView content_view(content); |
| 114 | + auto document = GUI::TextDocument::create(&s_default_document_client); |
| 115 | + document->set_text(content_view); |
| 116 | + return document; |
| 117 | +} |
| 118 | + |
| 119 | +void FileDB::on_file_edit_insert_text(const String& file_name, const String& inserted_text, size_t start_line, size_t start_column) |
| 120 | +{ |
| 121 | + auto document = get(file_name); |
| 122 | + if (!document) { |
| 123 | + dbgln("file {} has not been opened", file_name); |
| 124 | + return; |
| 125 | + } |
| 126 | + GUI::TextPosition start_position { start_line, start_column }; |
| 127 | + document->insert_at(start_position, inserted_text, &s_default_document_client); |
| 128 | + |
| 129 | +#if FILE_CONTENT_DEBUG |
| 130 | + dbgln("{}", document->text()); |
| 131 | +#endif |
| 132 | +} |
| 133 | + |
| 134 | +void FileDB::on_file_edit_remove_text(const String& file_name, size_t start_line, size_t start_column, size_t end_line, size_t end_column) |
| 135 | +{ |
| 136 | + auto document = get(file_name); |
| 137 | + if (!document) { |
| 138 | + dbgln("file {} has not been opened", file_name); |
| 139 | + return; |
| 140 | + } |
| 141 | + GUI::TextPosition start_position { start_line, start_column }; |
| 142 | + GUI::TextRange range { |
| 143 | + GUI::TextPosition { start_line, start_column }, |
| 144 | + GUI::TextPosition { end_line, end_column } |
| 145 | + }; |
| 146 | + |
| 147 | + document->remove(range); |
| 148 | +#if FILE_CONTENT_DEBUG |
| 149 | + dbgln("{}", document->text()); |
| 150 | +#endif |
| 151 | +} |
0 commit comments