LanguageClient.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  27. #include "AutoCompleteResponse.h"
  28. #include <AK/Forward.h>
  29. #include <AK/LexicalPath.h>
  30. #include <AK/Types.h>
  31. #include <AK/WeakPtr.h>
  32. #include <AK/Weakable.h>
  33. #include <LibIPC/ServerConnection.h>
  34. #include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
  35. #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
  36. namespace HackStudio {
  37. class LanguageClient;
  38. class ServerConnection
  39. : public IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>
  40. , public LanguageClientEndpoint {
  41. public:
  42. ServerConnection(const StringView& socket, const String& project_path)
  43. : IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, socket)
  44. {
  45. m_project_path = project_path;
  46. }
  47. void attach(LanguageClient& client)
  48. {
  49. m_language_client = &client;
  50. }
  51. void detach()
  52. {
  53. m_language_client = nullptr;
  54. }
  55. virtual void handshake() override
  56. {
  57. send_sync<Messages::LanguageServer::Greet>(m_project_path);
  58. }
  59. WeakPtr<LanguageClient> language_client() { return m_language_client; }
  60. const String& projcet_path() const { return m_project_path; }
  61. template<typename ConcreteType>
  62. static NonnullRefPtr<ServerConnection> get_or_create(const String& project_path)
  63. {
  64. auto key = LexicalPath { project_path }.string();
  65. if (auto instance = s_instances_for_projects.get(key); instance.has_value())
  66. return *instance.value();
  67. auto connection = ConcreteType::construct(project_path);
  68. connection->handshake();
  69. set_instance_for_project(project_path, *connection);
  70. return *connection;
  71. }
  72. static RefPtr<ServerConnection> instance_for_project(const String& project_path);
  73. static void set_instance_for_project(const String& project_path, NonnullRefPtr<ServerConnection>&&);
  74. static void remove_instance_for_project(const String& project_path);
  75. virtual void die();
  76. protected:
  77. virtual void handle(const Messages::LanguageClient::AutoCompleteSuggestions&) override;
  78. virtual void handle(const Messages::LanguageClient::DeclarationLocation&) override;
  79. virtual void handle(const Messages::LanguageClient::DeclarationsInDocument&) override;
  80. String m_project_path;
  81. WeakPtr<LanguageClient> m_language_client;
  82. private:
  83. static HashMap<String, NonnullRefPtr<ServerConnection>> s_instances_for_projects;
  84. };
  85. class LanguageClient : public Weakable<LanguageClient> {
  86. public:
  87. explicit LanguageClient(NonnullRefPtr<ServerConnection>&& connection)
  88. : m_server_connection(move(connection))
  89. {
  90. m_previous_client = m_server_connection->language_client();
  91. VERIFY(m_previous_client.ptr() != this);
  92. m_server_connection->attach(*this);
  93. }
  94. virtual ~LanguageClient()
  95. {
  96. m_server_connection->detach();
  97. VERIFY(m_previous_client.ptr() != this);
  98. if (m_previous_client)
  99. m_server_connection->attach(*m_previous_client);
  100. }
  101. void set_active_client();
  102. virtual void open_file(const String& path, int fd);
  103. virtual void set_file_content(const String& path, const String& content);
  104. virtual void insert_text(const String& path, const String& text, size_t line, size_t column);
  105. virtual void remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column);
  106. virtual void request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column);
  107. virtual void set_autocomplete_mode(const String& mode);
  108. virtual void search_declaration(const String& path, size_t line, size_t column);
  109. void provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>&);
  110. void declaration_found(const String& file, size_t line, size_t column);
  111. void on_server_crash();
  112. Function<void(Vector<GUI::AutocompleteProvider::Entry>)> on_autocomplete_suggestions;
  113. Function<void(const String&, size_t, size_t)> on_declaration_found;
  114. private:
  115. WeakPtr<ServerConnection> m_server_connection;
  116. WeakPtr<LanguageClient> m_previous_client;
  117. };
  118. template<typename ServerConnectionT>
  119. static inline NonnullOwnPtr<LanguageClient> get_language_client(const String& project_path)
  120. {
  121. return make<LanguageClient>(ServerConnection::get_or_create<ServerConnectionT>(project_path));
  122. }
  123. }