LanguageClient.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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 "LanguageClient.h"
  27. #include <AK/String.h>
  28. #include <AK/Vector.h>
  29. #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
  30. #include <LibGUI/Notification.h>
  31. namespace HackStudio {
  32. void ServerConnection::handle(const Messages::LanguageClient::AutoCompleteSuggestions& message)
  33. {
  34. if (!m_language_client) {
  35. dbgln("Language Server connection has no attached language client");
  36. return;
  37. }
  38. m_language_client->provide_autocomplete_suggestions(message.suggestions());
  39. }
  40. void ServerConnection::die()
  41. {
  42. dbgln("ServerConnection::die()");
  43. if (!m_language_client)
  44. return;
  45. m_language_client->on_server_crash();
  46. }
  47. void LanguageClient::open_file(const String& path, int fd)
  48. {
  49. if (!m_server_connection)
  50. return;
  51. m_server_connection->post_message(Messages::LanguageServer::FileOpened(path, fd));
  52. }
  53. void LanguageClient::set_file_content(const String& path, const String& content)
  54. {
  55. if (!m_server_connection)
  56. return;
  57. m_server_connection->post_message(Messages::LanguageServer::SetFileContent(path, content));
  58. }
  59. void LanguageClient::insert_text(const String& path, const String& text, size_t line, size_t column)
  60. {
  61. if (!m_server_connection)
  62. return;
  63. m_server_connection->post_message(Messages::LanguageServer::FileEditInsertText(path, text, line, column));
  64. }
  65. void LanguageClient::remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column)
  66. {
  67. if (!m_server_connection)
  68. return;
  69. m_server_connection->post_message(Messages::LanguageServer::FileEditRemoveText(path, from_line, from_column, to_line, to_column));
  70. }
  71. void LanguageClient::request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column)
  72. {
  73. if (!m_server_connection)
  74. return;
  75. set_active_client();
  76. m_server_connection->post_message(Messages::LanguageServer::AutoCompleteSuggestions(path, cursor_line, cursor_column));
  77. }
  78. void LanguageClient::provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>& suggestions)
  79. {
  80. if (!m_server_connection)
  81. return;
  82. if (on_autocomplete_suggestions)
  83. on_autocomplete_suggestions(suggestions);
  84. // Otherwise, drop it on the floor :shrug:
  85. }
  86. void LanguageClient::set_autocomplete_mode(const String& mode)
  87. {
  88. if (!m_server_connection)
  89. return;
  90. m_server_connection->post_message(Messages::LanguageServer::SetAutoCompleteMode(mode));
  91. }
  92. void LanguageClient::set_active_client()
  93. {
  94. if (!m_server_connection)
  95. return;
  96. m_server_connection->attach(*this);
  97. }
  98. void LanguageClient::on_server_crash()
  99. {
  100. ASSERT(m_server_connection);
  101. auto project_path = m_server_connection->projcet_path();
  102. ServerConnection::remove_instance_for_project(project_path);
  103. m_server_connection = nullptr;
  104. auto notification = GUI::Notification::construct();
  105. notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"));
  106. notification->set_title("Oops!");
  107. notification->set_text(String::formatted("LanguageServer for {} crashed", project_path));
  108. notification->show();
  109. }
  110. HashMap<String, NonnullRefPtr<ServerConnection>> ServerConnection::s_instances_for_projects;
  111. RefPtr<ServerConnection> ServerConnection::instance_for_project(const String& project_path)
  112. {
  113. auto key = LexicalPath { project_path }.string();
  114. auto value = s_instances_for_projects.get(key);
  115. if (!value.has_value())
  116. return nullptr;
  117. return *value.value();
  118. }
  119. void ServerConnection::set_instance_for_project(const String& project_path, NonnullRefPtr<ServerConnection>&& instance)
  120. {
  121. auto key = LexicalPath { project_path }.string();
  122. s_instances_for_projects.set(key, move(instance));
  123. }
  124. void ServerConnection::remove_instance_for_project(const String& project_path)
  125. {
  126. auto key = LexicalPath { project_path }.string();
  127. s_instances_for_projects.remove(key);
  128. }
  129. }