ClientConnection.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <AK/Debug.h>
  28. #include <AK/HashMap.h>
  29. #include <LibCore/File.h>
  30. #include <LibGUI/TextDocument.h>
  31. namespace LanguageServers {
  32. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  33. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  34. : IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
  35. {
  36. s_connections.set(client_id, *this);
  37. }
  38. ClientConnection::~ClientConnection()
  39. {
  40. }
  41. void ClientConnection::die()
  42. {
  43. s_connections.remove(client_id());
  44. exit(0);
  45. }
  46. OwnPtr<Messages::LanguageServer::GreetResponse> ClientConnection::handle(const Messages::LanguageServer::Greet& message)
  47. {
  48. m_filedb.set_project_root(message.project_root());
  49. if (unveil(message.project_root().characters(), "r") < 0) {
  50. perror("unveil");
  51. exit(1);
  52. }
  53. if (unveil(nullptr, nullptr) < 0) {
  54. perror("unveil");
  55. exit(1);
  56. }
  57. return make<Messages::LanguageServer::GreetResponse>();
  58. }
  59. void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
  60. {
  61. if (m_filedb.is_open(message.file_name())) {
  62. return;
  63. }
  64. m_filedb.add(message.file_name(), message.file().take_fd());
  65. m_autocomplete_engine->file_opened(message.file_name());
  66. }
  67. void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
  68. {
  69. dbgln_if(LANGUAGE_SERVER_DEBUG, "InsertText for file: {}", message.file_name());
  70. dbgln_if(LANGUAGE_SERVER_DEBUG, "Text: {}", message.text());
  71. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{}]", message.start_line(), message.start_column());
  72. m_filedb.on_file_edit_insert_text(message.file_name(), message.text(), message.start_line(), message.start_column());
  73. m_autocomplete_engine->on_edit(message.file_name());
  74. }
  75. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  76. {
  77. dbgln_if(LANGUAGE_SERVER_DEBUG, "RemoveText for file: {}", message.file_name());
  78. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
  79. m_filedb.on_file_edit_remove_text(message.file_name(), message.start_line(), message.start_column(), message.end_line(), message.end_column());
  80. m_autocomplete_engine->on_edit(message.file_name());
  81. }
  82. void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
  83. {
  84. dbgln_if(LANGUAGE_SERVER_DEBUG, "AutoCompleteSuggestions for: {} {}:{}", message.location().file, message.location().line, message.location().column);
  85. auto document = m_filedb.get(message.location().file);
  86. if (!document) {
  87. dbgln("file {} has not been opened", message.location().file);
  88. return;
  89. }
  90. GUI::TextPosition autocomplete_position = { (size_t)message.location().line, (size_t)max(message.location().column, message.location().column - 1) };
  91. Vector<GUI::AutocompleteProvider::Entry> suggestions = m_autocomplete_engine->get_suggestions(message.location().file, autocomplete_position);
  92. post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
  93. }
  94. void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
  95. {
  96. dbgln_if(LANGUAGE_SERVER_DEBUG, "SetFileContent: {}", message.file_name());
  97. auto document = m_filedb.get(message.file_name());
  98. if (!document) {
  99. m_filedb.add(message.file_name(), message.content());
  100. VERIFY(m_filedb.is_open(message.file_name()));
  101. } else {
  102. const auto& content = message.content();
  103. document->set_text(content.view());
  104. }
  105. VERIFY(m_filedb.is_open(message.file_name()));
  106. m_autocomplete_engine->on_edit(message.file_name());
  107. }
  108. void ClientConnection::handle(const Messages::LanguageServer::FindDeclaration& message)
  109. {
  110. dbgln_if(LANGUAGE_SERVER_DEBUG, "FindDeclaration: {} {}:{}", message.location().file, message.location().line, message.location().column);
  111. auto document = m_filedb.get(message.location().file);
  112. if (!document) {
  113. dbgln("file {} has not been opened", message.location().file);
  114. return;
  115. }
  116. GUI::TextPosition identifier_position = { (size_t)message.location().line, (size_t)message.location().column };
  117. auto location = m_autocomplete_engine->find_declaration_of(message.location().file, identifier_position);
  118. if (!location.has_value()) {
  119. dbgln("could not find declaration");
  120. return;
  121. }
  122. dbgln_if(LANGUAGE_SERVER_DEBUG, "declaration location: {} {}:{}", location.value().file, location.value().line, location.value().column);
  123. post_message(Messages::LanguageClient::DeclarationLocation(GUI::AutocompleteProvider::ProjectLocation { location.value().file, location.value().line, location.value().column }));
  124. }
  125. void ClientConnection::set_declarations_of_document_callback(ClientConnection& instance, const String& filename, Vector<GUI::AutocompleteProvider::Declaration>&& declarations)
  126. {
  127. instance.post_message(Messages::LanguageClient::DeclarationsInDocument(filename, move(declarations)));
  128. }
  129. }