FileDB.cpp 5.4 KB

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