ClientConnection.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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_SH_LANGUAGE_SERVER
  32. // #define DEBUG_FILE_CONTENT
  33. namespace LanguageServers::Shell {
  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_SH_LANGUAGE_SERVER
  52. dbgln("project_root: {}", m_project_root);
  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::formatted("{}/{}", m_project_root, message.file_name()));
  73. #ifdef DEBUG_SH_LANGUAGE_SERVER
  74. dbgln("FileOpened: {}", file_path);
  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. dbgln("Failed to open project file: {}", file_path);
  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_SH_LANGUAGE_SERVER
  95. dbgln("InsertText for file: {}", message.file_name());
  96. dbgln("Text: {}", message.text());
  97. dbgln("[{}:{}]", message.start_line(), message.start_column());
  98. #endif
  99. auto document = document_for(message.file_name());
  100. if (!document) {
  101. dbgln("file {} has not been opened", message.file_name());
  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. dbgln("{}", document->text());
  108. #endif
  109. }
  110. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  111. {
  112. #ifdef DEBUG_SH_LANGUAGE_SERVER
  113. dbgln("RemoveText for file: {}", message.file_name());
  114. dbgln("[{}:{} - {}:{}]", 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. dbgln("file {} has not been opened", message.file_name());
  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_SH_LANGUAGE_SERVER
  136. dbgln("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. dbgln("file {} has not been opened", message.file_name());
  141. return;
  142. }
  143. auto& lines = document->lines();
  144. size_t offset = 0;
  145. if (message.cursor_line() > 0) {
  146. for (auto i = 0; i < message.cursor_line(); ++i)
  147. offset += lines[i].length() + 1;
  148. }
  149. offset += message.cursor_column();
  150. auto suggestions = m_autocomplete.get_suggestions(document->text(), offset);
  151. post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
  152. }
  153. RefPtr<GUI::TextDocument> ClientConnection::document_for(const String& file_name)
  154. {
  155. auto document_optional = m_open_files.get(file_name);
  156. if (!document_optional.has_value())
  157. return nullptr;
  158. return document_optional.value();
  159. }
  160. void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
  161. {
  162. auto document = document_for(message.file_name());
  163. if (!document) {
  164. dbgln("file {} has not been opened", message.file_name());
  165. return;
  166. }
  167. auto content = message.content();
  168. document->set_text(content.view());
  169. }
  170. }