ClientConnection.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2020, 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 "ClientConnection.h"
  27. #include "LexerAutoComplete.h"
  28. #include "ParserAutoComplete.h"
  29. #include <AK/Debug.h>
  30. #include <AK/HashMap.h>
  31. #include <LibCore/File.h>
  32. #include <LibGUI/TextDocument.h>
  33. namespace LanguageServers::Cpp {
  34. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  35. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  36. : IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
  37. {
  38. s_connections.set(client_id, *this);
  39. }
  40. ClientConnection::~ClientConnection()
  41. {
  42. }
  43. void ClientConnection::die()
  44. {
  45. s_connections.remove(client_id());
  46. exit(0);
  47. }
  48. OwnPtr<Messages::LanguageServer::GreetResponse> ClientConnection::handle(const Messages::LanguageServer::Greet& message)
  49. {
  50. m_filedb.set_project_root(message.project_root());
  51. if (unveil(message.project_root().characters(), "r") < 0) {
  52. perror("unveil");
  53. exit(1);
  54. }
  55. if (unveil(nullptr, nullptr) < 0) {
  56. perror("unveil");
  57. exit(1);
  58. }
  59. return make<Messages::LanguageServer::GreetResponse>();
  60. }
  61. class DefaultDocumentClient final : public GUI::TextDocument::Client {
  62. public:
  63. virtual ~DefaultDocumentClient() override = default;
  64. virtual void document_did_append_line() override {};
  65. virtual void document_did_insert_line(size_t) override {};
  66. virtual void document_did_remove_line(size_t) override {};
  67. virtual void document_did_remove_all_lines() override {};
  68. virtual void document_did_change() override {};
  69. virtual void document_did_set_text() override {};
  70. virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
  71. virtual bool is_automatic_indentation_enabled() const override { return false; }
  72. virtual int soft_tab_width() const override { return 4; }
  73. };
  74. static DefaultDocumentClient s_default_document_client;
  75. void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
  76. {
  77. auto file = Core::File::construct(this);
  78. if (!file->open(message.file().take_fd(), Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) {
  79. errno = file->error();
  80. perror("open");
  81. dbgln("Failed to open project file");
  82. return;
  83. }
  84. auto content = file->read_all();
  85. StringView content_view(content);
  86. auto document = GUI::TextDocument::create(&s_default_document_client);
  87. document->set_text(content_view);
  88. m_open_files.set(message.file_name(), document);
  89. dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
  90. }
  91. void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
  92. {
  93. #if CPP_LANGUAGE_SERVER_DEBUG
  94. dbgln("InsertText for file: {}", message.file_name());
  95. dbgln("Text: {}", message.text());
  96. dbgln("[{}:{}]", message.start_line(), message.start_column());
  97. #endif
  98. auto document = document_for(message.file_name());
  99. if (!document) {
  100. dbgln("file {} has not been opened", message.file_name());
  101. return;
  102. }
  103. GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
  104. document->insert_at(start_position, message.text(), &s_default_document_client);
  105. #if FILE_CONTENT_DEBUG
  106. dbgln("{}", document->text());
  107. #endif
  108. }
  109. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  110. {
  111. #if CPP_LANGUAGE_SERVER_DEBUG
  112. dbgln("RemoveText for file: {}", message.file_name());
  113. dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
  114. #endif
  115. auto document = document_for(message.file_name());
  116. if (!document) {
  117. dbgln("file {} has not been opened", message.file_name());
  118. return;
  119. }
  120. GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
  121. GUI::TextRange range {
  122. GUI::TextPosition { (size_t)message.start_line(),
  123. (size_t)message.start_column() },
  124. GUI::TextPosition { (size_t)message.end_line(),
  125. (size_t)message.end_column() }
  126. };
  127. document->remove(range);
  128. #if FILE_CONTENT_DEBUG
  129. dbgln("{}", document->text());
  130. #endif
  131. }
  132. void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
  133. {
  134. #if CPP_LANGUAGE_SERVER_DEBUG
  135. dbgln("AutoCompleteSuggestions for: {} {}:{}", message.file_name(), message.cursor_line(), message.cursor_column());
  136. #endif
  137. auto document = document_for(message.file_name());
  138. if (!document) {
  139. dbgln("file {} has not been opened", message.file_name());
  140. return;
  141. }
  142. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  143. switch (m_auto_complete_mode) {
  144. case AutoCompleteMode::Lexer:
  145. suggestions = LexerAutoComplete::get_suggestions(document->text(), { (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) });
  146. break;
  147. case AutoCompleteMode::Parser: {
  148. auto engine = ParserAutoComplete(document->text());
  149. suggestions = engine.get_suggestions({ (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) });
  150. }
  151. }
  152. post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
  153. }
  154. RefPtr<GUI::TextDocument> ClientConnection::document_for(const String& file_name)
  155. {
  156. auto document_optional = m_open_files.get(file_name);
  157. if (!document_optional.has_value())
  158. return nullptr;
  159. return document_optional.value();
  160. }
  161. void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
  162. {
  163. auto document = document_for(message.file_name());
  164. if (!document) {
  165. dbgln("file {} has not been opened", message.file_name());
  166. return;
  167. }
  168. auto content = message.content();
  169. document->set_text(content.view());
  170. }
  171. void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message)
  172. {
  173. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  174. dbgln("SetAutoCompleteMode: {}", message.mode());
  175. #endif
  176. if (message.mode() == "Parser")
  177. m_auto_complete_mode = AutoCompleteMode::Parser;
  178. else
  179. m_auto_complete_mode = AutoCompleteMode::Lexer;
  180. }
  181. }