ConnectionFromClient.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ConnectionFromClient.h"
  8. #include <AK/Debug.h>
  9. #include <AK/HashMap.h>
  10. #include <LibCore/File.h>
  11. #include <LibGUI/TextDocument.h>
  12. namespace LanguageServers {
  13. static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
  14. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  15. : IPC::ConnectionFromClient<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), 1)
  16. {
  17. s_connections.set(1, *this);
  18. }
  19. void ConnectionFromClient::die()
  20. {
  21. s_connections.remove(client_id());
  22. exit(0);
  23. }
  24. void ConnectionFromClient::greet(String const& project_root)
  25. {
  26. m_filedb.set_project_root(project_root);
  27. if (unveil(project_root.characters(), "r") < 0) {
  28. perror("unveil");
  29. exit(1);
  30. }
  31. if (unveil(nullptr, nullptr) < 0) {
  32. perror("unveil");
  33. exit(1);
  34. }
  35. }
  36. void ConnectionFromClient::file_opened(String const& filename, IPC::File const& file)
  37. {
  38. if (m_filedb.is_open(filename)) {
  39. return;
  40. }
  41. m_filedb.add(filename, file.take_fd());
  42. m_autocomplete_engine->file_opened(filename);
  43. }
  44. void ConnectionFromClient::file_edit_insert_text(String const& filename, String const& text, i32 start_line, i32 start_column)
  45. {
  46. dbgln_if(LANGUAGE_SERVER_DEBUG, "InsertText for file: {}", filename);
  47. dbgln_if(LANGUAGE_SERVER_DEBUG, "Text: {}", text);
  48. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{}]", start_line, start_column);
  49. m_filedb.on_file_edit_insert_text(filename, text, start_line, start_column);
  50. m_autocomplete_engine->on_edit(filename);
  51. }
  52. void ConnectionFromClient::file_edit_remove_text(String const& filename, i32 start_line, i32 start_column, i32 end_line, i32 end_column)
  53. {
  54. dbgln_if(LANGUAGE_SERVER_DEBUG, "RemoveText for file: {}", filename);
  55. dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{} - {}:{}]", start_line, start_column, end_line, end_column);
  56. m_filedb.on_file_edit_remove_text(filename, start_line, start_column, end_line, end_column);
  57. m_autocomplete_engine->on_edit(filename);
  58. }
  59. void ConnectionFromClient::auto_complete_suggestions(CodeComprehension::ProjectLocation const& location)
  60. {
  61. dbgln_if(LANGUAGE_SERVER_DEBUG, "AutoCompleteSuggestions for: {} {}:{}", location.file, location.line, location.column);
  62. auto document = m_filedb.get_document(location.file);
  63. if (!document) {
  64. dbgln("file {} has not been opened", location.file);
  65. return;
  66. }
  67. GUI::TextPosition autocomplete_position = { (size_t)location.line, (size_t)max(location.column, location.column - 1) };
  68. Vector<CodeComprehension::AutocompleteResultEntry> suggestions = m_autocomplete_engine->get_suggestions(location.file, autocomplete_position);
  69. async_auto_complete_suggestions(move(suggestions));
  70. }
  71. void ConnectionFromClient::set_file_content(String const& filename, String const& content)
  72. {
  73. dbgln_if(LANGUAGE_SERVER_DEBUG, "SetFileContent: {}", filename);
  74. auto document = m_filedb.get_document(filename);
  75. if (!document) {
  76. m_filedb.add(filename, content);
  77. VERIFY(m_filedb.is_open(filename));
  78. } else {
  79. document->set_text(content.view());
  80. }
  81. VERIFY(m_filedb.is_open(filename));
  82. m_autocomplete_engine->on_edit(filename);
  83. }
  84. void ConnectionFromClient::find_declaration(CodeComprehension::ProjectLocation const& location)
  85. {
  86. dbgln_if(LANGUAGE_SERVER_DEBUG, "FindDeclaration: {} {}:{}", location.file, location.line, location.column);
  87. auto document = m_filedb.get_document(location.file);
  88. if (!document) {
  89. dbgln("file {} has not been opened", location.file);
  90. return;
  91. }
  92. GUI::TextPosition identifier_position = { (size_t)location.line, (size_t)location.column };
  93. auto decl_location = m_autocomplete_engine->find_declaration_of(location.file, identifier_position);
  94. if (!decl_location.has_value()) {
  95. dbgln("could not find declaration");
  96. return;
  97. }
  98. dbgln_if(LANGUAGE_SERVER_DEBUG, "declaration location: {} {}:{}", decl_location.value().file, decl_location.value().line, decl_location.value().column);
  99. async_declaration_location(CodeComprehension::ProjectLocation { decl_location.value().file, decl_location.value().line, decl_location.value().column });
  100. }
  101. void ConnectionFromClient::get_parameters_hint(CodeComprehension::ProjectLocation const& location)
  102. {
  103. dbgln_if(LANGUAGE_SERVER_DEBUG, "GetParametersHint: {} {}:{}", location.file, location.line, location.column);
  104. auto document = m_filedb.get_document(location.file);
  105. if (!document) {
  106. dbgln("file {} has not been opened", location.file);
  107. return;
  108. }
  109. GUI::TextPosition identifier_position = { (size_t)location.line, (size_t)location.column };
  110. auto params = m_autocomplete_engine->get_function_params_hint(location.file, identifier_position);
  111. if (!params.has_value()) {
  112. dbgln("could not get parameters hint");
  113. return;
  114. }
  115. dbgln_if(LANGUAGE_SERVER_DEBUG, "parameters hint:");
  116. for (auto& param : params->params) {
  117. dbgln_if(LANGUAGE_SERVER_DEBUG, "{}", param);
  118. }
  119. dbgln_if(LANGUAGE_SERVER_DEBUG, "Parameter index: {}", params->current_index);
  120. async_parameters_hint_result(params->params, params->current_index);
  121. }
  122. void ConnectionFromClient::get_tokens_info(String const& filename)
  123. {
  124. dbgln_if(LANGUAGE_SERVER_DEBUG, "GetTokenInfo: {}", filename);
  125. auto document = m_filedb.get_document(filename);
  126. if (!document) {
  127. dbgln("file {} has not been opened", filename);
  128. return;
  129. }
  130. auto tokens_info = m_autocomplete_engine->get_tokens_info(filename);
  131. async_tokens_info_result(move(tokens_info));
  132. }
  133. }