ClientConnection.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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&)
  49. {
  50. return make<Messages::LanguageServer::GreetResponse>();
  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 false; }
  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().take_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. dbgln<FILE_CONTENT_DEBUG>("{}", document->text());
  81. }
  82. void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
  83. {
  84. #if CPP_LANGUAGE_SERVER_DEBUG
  85. dbgln("InsertText for file: {}", message.file_name());
  86. dbgln("Text: {}", message.text());
  87. dbgln("[{}:{}]", message.start_line(), message.start_column());
  88. #endif
  89. auto document = document_for(message.file_name());
  90. if (!document) {
  91. dbgln("file {} has not been opened", message.file_name());
  92. return;
  93. }
  94. GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
  95. document->insert_at(start_position, message.text(), &s_default_document_client);
  96. #if FILE_CONTENT_DEBUG
  97. dbgln("{}", document->text());
  98. #endif
  99. }
  100. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  101. {
  102. #if CPP_LANGUAGE_SERVER_DEBUG
  103. dbgln("RemoveText for file: {}", message.file_name());
  104. dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
  105. #endif
  106. auto document = document_for(message.file_name());
  107. if (!document) {
  108. dbgln("file {} has not been opened", message.file_name());
  109. return;
  110. }
  111. GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
  112. GUI::TextRange range {
  113. GUI::TextPosition { (size_t)message.start_line(),
  114. (size_t)message.start_column() },
  115. GUI::TextPosition { (size_t)message.end_line(),
  116. (size_t)message.end_column() }
  117. };
  118. document->remove(range);
  119. #if FILE_CONTENT_DEBUG
  120. dbgln("{}", document->text());
  121. #endif
  122. }
  123. void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
  124. {
  125. #if CPP_LANGUAGE_SERVER_DEBUG
  126. dbgln("AutoCompleteSuggestions for: {} {}:{}", message.file_name(), message.cursor_line(), message.cursor_column());
  127. #endif
  128. auto document = document_for(message.file_name());
  129. if (!document) {
  130. dbgln("file {} has not been opened", message.file_name());
  131. return;
  132. }
  133. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  134. switch (m_auto_complete_mode) {
  135. case AutoCompleteMode::Lexer:
  136. suggestions = LexerAutoComplete::get_suggestions(document->text(), { (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) });
  137. break;
  138. case AutoCompleteMode::Parser: {
  139. auto engine = ParserAutoComplete(document->text());
  140. suggestions = engine.get_suggestions({ (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) });
  141. }
  142. }
  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. void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message)
  163. {
  164. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  165. dbgln("SetAutoCompleteMode: {}", message.mode());
  166. #endif
  167. if (message.mode() == "Parser")
  168. m_auto_complete_mode = AutoCompleteMode::Parser;
  169. else
  170. m_auto_complete_mode = AutoCompleteMode::Lexer;
  171. }
  172. }