ClientConnection.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ClientConnection.h"
  7. #include <AK/Debug.h>
  8. #include <AK/HashMap.h>
  9. #include <LibCore/File.h>
  10. #include <LibGUI/TextDocument.h>
  11. namespace LanguageServers {
  12. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  13. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  14. : IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
  15. {
  16. s_connections.set(client_id, *this);
  17. }
  18. ClientConnection::~ClientConnection()
  19. {
  20. }
  21. void ClientConnection::die()
  22. {
  23. s_connections.remove(client_id());
  24. exit(0);
  25. }
  26. Messages::LanguageServer::GreetResponse ClientConnection::handle(const Messages::LanguageServer::Greet& message)
  27. {
  28. m_filedb.set_project_root(message.project_root());
  29. if (unveil(message.project_root().characters(), "r") < 0) {
  30. perror("unveil");
  31. exit(1);
  32. }
  33. if (unveil(nullptr, nullptr) < 0) {
  34. perror("unveil");
  35. exit(1);
  36. }
  37. return {};
  38. }
  39. void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
  40. {
  41. if (m_filedb.is_open(message.filename())) {
  42. return;
  43. }
  44. m_filedb.add(message.filename(), message.file().take_fd());
  45. m_autocomplete_engine->file_opened(message.filename());
  46. }
  47. void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
  48. {
  49. dbgln_if(LANGUAGE_SERVER_DEBUG, "InsertText for file: {}", message.filename());
  50. dbgln_if(LANGUAGE_SERVER_DEBUG, "Text: {}", message.text());
  51. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{}]", message.start_line(), message.start_column());
  52. m_filedb.on_file_edit_insert_text(message.filename(), message.text(), message.start_line(), message.start_column());
  53. m_autocomplete_engine->on_edit(message.filename());
  54. }
  55. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  56. {
  57. dbgln_if(LANGUAGE_SERVER_DEBUG, "RemoveText for file: {}", message.filename());
  58. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
  59. m_filedb.on_file_edit_remove_text(message.filename(), message.start_line(), message.start_column(), message.end_line(), message.end_column());
  60. m_autocomplete_engine->on_edit(message.filename());
  61. }
  62. void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
  63. {
  64. dbgln_if(LANGUAGE_SERVER_DEBUG, "AutoCompleteSuggestions for: {} {}:{}", message.location().file, message.location().line, message.location().column);
  65. auto document = m_filedb.get(message.location().file);
  66. if (!document) {
  67. dbgln("file {} has not been opened", message.location().file);
  68. return;
  69. }
  70. GUI::TextPosition autocomplete_position = { (size_t)message.location().line, (size_t)max(message.location().column, message.location().column - 1) };
  71. Vector<GUI::AutocompleteProvider::Entry> suggestions = m_autocomplete_engine->get_suggestions(message.location().file, autocomplete_position);
  72. post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
  73. }
  74. void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
  75. {
  76. dbgln_if(LANGUAGE_SERVER_DEBUG, "SetFileContent: {}", message.filename());
  77. auto document = m_filedb.get(message.filename());
  78. if (!document) {
  79. m_filedb.add(message.filename(), message.content());
  80. VERIFY(m_filedb.is_open(message.filename()));
  81. } else {
  82. const auto& content = message.content();
  83. document->set_text(content.view());
  84. }
  85. VERIFY(m_filedb.is_open(message.filename()));
  86. m_autocomplete_engine->on_edit(message.filename());
  87. }
  88. void ClientConnection::handle(const Messages::LanguageServer::FindDeclaration& message)
  89. {
  90. dbgln_if(LANGUAGE_SERVER_DEBUG, "FindDeclaration: {} {}:{}", message.location().file, message.location().line, message.location().column);
  91. auto document = m_filedb.get(message.location().file);
  92. if (!document) {
  93. dbgln("file {} has not been opened", message.location().file);
  94. return;
  95. }
  96. GUI::TextPosition identifier_position = { (size_t)message.location().line, (size_t)message.location().column };
  97. auto location = m_autocomplete_engine->find_declaration_of(message.location().file, identifier_position);
  98. if (!location.has_value()) {
  99. dbgln("could not find declaration");
  100. return;
  101. }
  102. dbgln_if(LANGUAGE_SERVER_DEBUG, "declaration location: {} {}:{}", location.value().file, location.value().line, location.value().column);
  103. post_message(Messages::LanguageClient::DeclarationLocation(GUI::AutocompleteProvider::ProjectLocation { location.value().file, location.value().line, location.value().column }));
  104. }
  105. void ClientConnection::set_declarations_of_document_callback(ClientConnection& instance, const String& filename, Vector<GUI::AutocompleteProvider::Declaration>&& declarations)
  106. {
  107. instance.post_message(Messages::LanguageClient::DeclarationsInDocument(filename, move(declarations)));
  108. }
  109. }