FileDB.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. namespace LanguageServers {
  30. RefPtr<const GUI::TextDocument> FileDB::get(const String& file_name) const
  31. {
  32. auto absolute_path = to_absolute_path(file_name);
  33. auto document_optional = m_open_files.get(absolute_path);
  34. if (!document_optional.has_value())
  35. return nullptr;
  36. return document_optional.value();
  37. }
  38. RefPtr<GUI::TextDocument> FileDB::get(const String& file_name)
  39. {
  40. auto document = reinterpret_cast<const FileDB*>(this)->get(file_name);
  41. if (document.is_null())
  42. return nullptr;
  43. return adopt(*const_cast<GUI::TextDocument*>(document.leak_ref()));
  44. }
  45. RefPtr<const GUI::TextDocument> FileDB::get_or_create_from_filesystem(const String& file_name) const
  46. {
  47. auto absolute_path = to_absolute_path(file_name);
  48. auto document = get(absolute_path);
  49. if (document)
  50. return document;
  51. return create_from_filesystem(absolute_path);
  52. }
  53. RefPtr<GUI::TextDocument> FileDB::get_or_create_from_filesystem(const String& file_name)
  54. {
  55. auto document = reinterpret_cast<const FileDB*>(this)->get_or_create_from_filesystem(file_name);
  56. if (document.is_null())
  57. return nullptr;
  58. return adopt(*const_cast<GUI::TextDocument*>(document.leak_ref()));
  59. }
  60. bool FileDB::is_open(const String& file_name) const
  61. {
  62. return m_open_files.contains(to_absolute_path(file_name));
  63. }
  64. bool FileDB::add(const String& file_name, int fd)
  65. {
  66. auto document = create_from_fd(fd);
  67. if (!document)
  68. return false;
  69. m_open_files.set(to_absolute_path(file_name), document.release_nonnull());
  70. return true;
  71. }
  72. String FileDB::to_absolute_path(const String& file_name) const
  73. {
  74. if (LexicalPath { file_name }.is_absolute()) {
  75. return file_name;
  76. }
  77. VERIFY(!m_project_root.is_null());
  78. return LexicalPath { String::formatted("{}/{}", m_project_root, file_name) }.string();
  79. }
  80. RefPtr<GUI::TextDocument> FileDB::create_from_filesystem(const String& file_name) const
  81. {
  82. auto file = Core::File::open(to_absolute_path(file_name), Core::IODevice::ReadOnly);
  83. if (file.is_error()) {
  84. dbgln("failed to create document for {} from filesystem", file_name);
  85. return nullptr;
  86. }
  87. return create_from_file(*file.value());
  88. }
  89. RefPtr<GUI::TextDocument> FileDB::create_from_fd(int fd) const
  90. {
  91. auto file = Core::File::construct();
  92. if (!file->open(fd, Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) {
  93. errno = file->error();
  94. perror("open");
  95. dbgln("Failed to open project file");
  96. return nullptr;
  97. }
  98. return create_from_file(*file);
  99. }
  100. class DefaultDocumentClient final : public GUI::TextDocument::Client {
  101. public:
  102. virtual ~DefaultDocumentClient() override = default;
  103. virtual void document_did_append_line() override {};
  104. virtual void document_did_insert_line(size_t) override {};
  105. virtual void document_did_remove_line(size_t) override {};
  106. virtual void document_did_remove_all_lines() override {};
  107. virtual void document_did_change() override {};
  108. virtual void document_did_set_text() override {};
  109. virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
  110. virtual bool is_automatic_indentation_enabled() const override { return false; }
  111. virtual int soft_tab_width() const override { return 4; }
  112. };
  113. static DefaultDocumentClient s_default_document_client;
  114. RefPtr<GUI::TextDocument> FileDB::create_from_file(Core::File& file) const
  115. {
  116. auto content = file.read_all();
  117. StringView content_view(content);
  118. auto document = GUI::TextDocument::create(&s_default_document_client);
  119. document->set_text(content_view);
  120. return document;
  121. }
  122. void FileDB::on_file_edit_insert_text(const String& file_name, const String& inserted_text, size_t start_line, size_t start_column)
  123. {
  124. VERIFY(is_open(file_name));
  125. auto document = get(file_name);
  126. VERIFY(document);
  127. GUI::TextPosition start_position { start_line, start_column };
  128. document->insert_at(start_position, inserted_text, &s_default_document_client);
  129. dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
  130. }
  131. 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)
  132. {
  133. // TODO: If file is not open - need to get its contents
  134. // Otherwise- somehow verify that respawned language server is synced with all file contents
  135. VERIFY(is_open(file_name));
  136. auto document = get(file_name);
  137. VERIFY(document);
  138. GUI::TextPosition start_position { start_line, start_column };
  139. GUI::TextRange range {
  140. GUI::TextPosition { start_line, start_column },
  141. GUI::TextPosition { end_line, end_column }
  142. };
  143. document->remove(range);
  144. dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
  145. }
  146. RefPtr<GUI::TextDocument> FileDB::create_with_content(const String& content)
  147. {
  148. StringView content_view(content);
  149. auto document = GUI::TextDocument::create(&s_default_document_client);
  150. document->set_text(content_view);
  151. return document;
  152. }
  153. bool FileDB::add(const String& file_name, const String& content)
  154. {
  155. auto document = create_with_content(content);
  156. if (!document) {
  157. VERIFY_NOT_REACHED();
  158. return false;
  159. }
  160. m_open_files.set(to_absolute_path(file_name), document.release_nonnull());
  161. return true;
  162. }
  163. }