ClientConnection.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. void ClientConnection::greet(String const& project_root)
  27. {
  28. m_filedb.set_project_root(project_root);
  29. if (unveil(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. }
  38. void ClientConnection::file_opened(String const& filename, IPC::File const& file)
  39. {
  40. if (m_filedb.is_open(filename)) {
  41. return;
  42. }
  43. m_filedb.add(filename, file.take_fd());
  44. m_autocomplete_engine->file_opened(filename);
  45. }
  46. void ClientConnection::file_edit_insert_text(String const& filename, String const& text, i32 start_line, i32 start_column)
  47. {
  48. dbgln_if(LANGUAGE_SERVER_DEBUG, "InsertText for file: {}", filename);
  49. dbgln_if(LANGUAGE_SERVER_DEBUG, "Text: {}", text);
  50. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{}]", start_line, start_column);
  51. m_filedb.on_file_edit_insert_text(filename, text, start_line, start_column);
  52. m_autocomplete_engine->on_edit(filename);
  53. }
  54. void ClientConnection::file_edit_remove_text(String const& filename, i32 start_line, i32 start_column, i32 end_line, i32 end_column)
  55. {
  56. dbgln_if(LANGUAGE_SERVER_DEBUG, "RemoveText for file: {}", filename);
  57. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{} - {}:{}]", start_line, start_column, end_line, end_column);
  58. m_filedb.on_file_edit_remove_text(filename, start_line, start_column, end_line, end_column);
  59. m_autocomplete_engine->on_edit(filename);
  60. }
  61. void ClientConnection::auto_complete_suggestions(GUI::AutocompleteProvider::ProjectLocation const& location)
  62. {
  63. dbgln_if(LANGUAGE_SERVER_DEBUG, "AutoCompleteSuggestions for: {} {}:{}", location.file, location.line, location.column);
  64. auto document = m_filedb.get(location.file);
  65. if (!document) {
  66. dbgln("file {} has not been opened", location.file);
  67. return;
  68. }
  69. GUI::TextPosition autocomplete_position = { (size_t)location.line, (size_t)max(location.column, location.column - 1) };
  70. Vector<GUI::AutocompleteProvider::Entry> suggestions = m_autocomplete_engine->get_suggestions(location.file, autocomplete_position);
  71. async_auto_complete_suggestions(move(suggestions));
  72. }
  73. void ClientConnection::set_file_content(String const& filename, String const& content)
  74. {
  75. dbgln_if(LANGUAGE_SERVER_DEBUG, "SetFileContent: {}", filename);
  76. auto document = m_filedb.get(filename);
  77. if (!document) {
  78. m_filedb.add(filename, content);
  79. VERIFY(m_filedb.is_open(filename));
  80. } else {
  81. document->set_text(content.view());
  82. }
  83. VERIFY(m_filedb.is_open(filename));
  84. m_autocomplete_engine->on_edit(filename);
  85. }
  86. void ClientConnection::find_declaration(GUI::AutocompleteProvider::ProjectLocation const& location)
  87. {
  88. dbgln_if(LANGUAGE_SERVER_DEBUG, "FindDeclaration: {} {}:{}", location.file, location.line, location.column);
  89. auto document = m_filedb.get(location.file);
  90. if (!document) {
  91. dbgln("file {} has not been opened", location.file);
  92. return;
  93. }
  94. GUI::TextPosition identifier_position = { (size_t)location.line, (size_t)location.column };
  95. auto decl_location = m_autocomplete_engine->find_declaration_of(location.file, identifier_position);
  96. if (!decl_location.has_value()) {
  97. dbgln("could not find declaration");
  98. return;
  99. }
  100. dbgln_if(LANGUAGE_SERVER_DEBUG, "declaration location: {} {}:{}", decl_location.value().file, decl_location.value().line, decl_location.value().column);
  101. async_declaration_location(GUI::AutocompleteProvider::ProjectLocation { decl_location.value().file, decl_location.value().line, decl_location.value().column });
  102. }
  103. void ClientConnection::get_parameters_hint(GUI::AutocompleteProvider::ProjectLocation const& location)
  104. {
  105. dbgln_if(LANGUAGE_SERVER_DEBUG, "GetFunctionParams: {} {}:{}", location.file, location.line, location.column);
  106. auto document = m_filedb.get(location.file);
  107. if (!document) {
  108. dbgln("file {} has not been opened", location.file);
  109. return;
  110. }
  111. GUI::TextPosition identifier_position = { (size_t)location.line, (size_t)location.column };
  112. auto params = m_autocomplete_engine->get_function_params_hint(location.file, identifier_position);
  113. if (!params.has_value()) {
  114. dbgln("could not get parameters hint");
  115. return;
  116. }
  117. dbgln_if(LANGUAGE_SERVER_DEBUG, "parameters hint:");
  118. for (auto& param : params->params) {
  119. dbgln_if(LANGUAGE_SERVER_DEBUG, "{}", param);
  120. }
  121. dbgln_if(LANGUAGE_SERVER_DEBUG, "Parameter index: {}", params->current_index);
  122. async_parameters_hint_result(params->params, params->current_index);
  123. }
  124. }