ClientConnection.cpp 6.6 KB

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