ClientConnection.cpp 6.5 KB

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