ClientConnection.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "AutoComplete.h"
  28. #include <AK/HashMap.h>
  29. #include <LibCore/File.h>
  30. #include <LibGUI/TextDocument.h>
  31. // #define DEBUG_CPP_LANGUAGE_SERVER
  32. // #define DEBUG_FILE_CONTENT
  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_project_root = LexicalPath(message.project_root());
  51. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  52. dbg() << "project_root: " << m_project_root.string();
  53. #endif
  54. return make<Messages::LanguageServer::GreetResponse>(client_id());
  55. }
  56. class DefaultDocumentClient final : public GUI::TextDocument::Client {
  57. public:
  58. virtual ~DefaultDocumentClient() override = default;
  59. virtual void document_did_append_line() override {};
  60. virtual void document_did_insert_line(size_t) override {};
  61. virtual void document_did_remove_line(size_t) override {};
  62. virtual void document_did_remove_all_lines() override {};
  63. virtual void document_did_change() override {};
  64. virtual void document_did_set_text() override {};
  65. virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
  66. virtual bool is_automatic_indentation_enabled() const override { return true; }
  67. virtual int soft_tab_width() const override { return 4; }
  68. };
  69. static DefaultDocumentClient s_default_document_client;
  70. void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
  71. {
  72. LexicalPath file_path(String::format("%s/%s", m_project_root.string().characters(), message.file_name().characters()));
  73. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  74. dbg() << "FileOpened: " << file_path.string();
  75. #endif
  76. auto file = Core::File::construct(file_path.string());
  77. if (!file->open(Core::IODevice::ReadOnly)) {
  78. errno = file->error();
  79. perror("open");
  80. dbg() << "Failed to open project file: " << file_path.string();
  81. return;
  82. }
  83. auto content = file->read_all();
  84. StringView content_view(content);
  85. auto document = GUI::TextDocument::create(&s_default_document_client);
  86. document->set_text(content_view);
  87. m_open_files.set(message.file_name(), document);
  88. #ifdef DEBUG_FILE_CONTENT
  89. dbg() << document->text();
  90. #endif
  91. }
  92. void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
  93. {
  94. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  95. dbg() << "InsertText for file: " << message.file_name();
  96. dbg() << "Text: " << message.text();
  97. dbg() << "[" << message.start_line() << ":" << message.start_column() << "]";
  98. #endif
  99. auto document = document_for(message.file_name());
  100. if (!document) {
  101. dbg() << "file " << message.file_name() << " has not been opened";
  102. return;
  103. }
  104. GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
  105. document->insert_at(start_position, message.text(), &s_default_document_client);
  106. #ifdef DEBUG_FILE_CONTENT
  107. dbg() << document->text();
  108. #endif
  109. }
  110. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  111. {
  112. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  113. dbg() << "RemoveText for file: " << message.file_name();
  114. dbg() << "[" << message.start_line() << ":" << message.start_column() << " - " << message.end_line() << ":" << message.end_column() << "]";
  115. #endif
  116. auto document = document_for(message.file_name());
  117. if (!document) {
  118. dbg() << "file " << message.file_name() << " has not been opened";
  119. return;
  120. }
  121. GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
  122. GUI::TextRange range {
  123. GUI::TextPosition { (size_t)message.start_line(),
  124. (size_t)message.start_column() },
  125. GUI::TextPosition { (size_t)message.end_line(),
  126. (size_t)message.end_column() }
  127. };
  128. document->remove(range);
  129. #ifdef DEBUG_FILE_CONTENT
  130. dbg() << document->text();
  131. #endif
  132. }
  133. void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
  134. {
  135. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  136. dbg() << "AutoCompleteSuggestions for: " << message.file_name() << " " << message.cursor_line() << ":" << message.cursor_column();
  137. #endif
  138. auto document = document_for(message.file_name());
  139. if (!document) {
  140. dbg() << "file " << message.file_name() << " has not been opened";
  141. return;
  142. }
  143. Vector<String> suggestions = AutoComplete::get_suggestions(document->text(), { (size_t)message.cursor_line(), (size_t)message.cursor_column() });
  144. post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
  145. }
  146. RefPtr<GUI::TextDocument> ClientConnection::document_for(const String& file_name)
  147. {
  148. auto document_optional = m_open_files.get(file_name);
  149. if (!document_optional.has_value())
  150. return nullptr;
  151. return document_optional.value();
  152. }
  153. void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
  154. {
  155. auto document = document_for(message.file_name());
  156. if (!document) {
  157. dbg() << "file " << message.file_name() << " has not been opened";
  158. return;
  159. }
  160. auto content = message.content();
  161. document->set_text(content.view());
  162. }
  163. }