LanguageClient.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "Language.h"
  29. #include <AK/Forward.h>
  30. #include <AK/LexicalPath.h>
  31. #include <AK/Types.h>
  32. #include <AK/WeakPtr.h>
  33. #include <AK/Weakable.h>
  34. #include <LibCore/ElapsedTimer.h>
  35. #include <LibIPC/ServerConnection.h>
  36. #include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
  37. #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
  38. namespace HackStudio {
  39. class LanguageClient;
  40. class ServerConnectionWrapper;
  41. class ServerConnection
  42. : public IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>
  43. , public LanguageClientEndpoint {
  44. friend class ServerConnectionWrapper;
  45. public:
  46. ServerConnection(const StringView& socket, const String& project_path)
  47. : IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, socket)
  48. {
  49. m_project_path = project_path;
  50. }
  51. virtual void handshake() override
  52. {
  53. send_sync<Messages::LanguageServer::Greet>(m_project_path);
  54. }
  55. WeakPtr<LanguageClient> language_client() { return m_current_language_client; }
  56. const String& project_path() const { return m_project_path; }
  57. virtual void die() override;
  58. protected:
  59. virtual void handle(const Messages::LanguageClient::AutoCompleteSuggestions&) override;
  60. virtual void handle(const Messages::LanguageClient::DeclarationLocation&) override;
  61. virtual void handle(const Messages::LanguageClient::DeclarationsInDocument&) override;
  62. void set_wrapper(ServerConnectionWrapper& wrapper) { m_wrapper = &wrapper; }
  63. String m_project_path;
  64. WeakPtr<LanguageClient> m_current_language_client;
  65. ServerConnectionWrapper* m_wrapper { nullptr };
  66. };
  67. class ServerConnectionWrapper {
  68. AK_MAKE_NONCOPYABLE(ServerConnectionWrapper);
  69. public:
  70. explicit ServerConnectionWrapper(const String& language_name, Function<NonnullRefPtr<ServerConnection>()> connection_creator);
  71. ~ServerConnectionWrapper() = default;
  72. template<typename LanguageServerType>
  73. static ServerConnectionWrapper& get_or_create(const String& project_path);
  74. ServerConnection* connection();
  75. void on_crash();
  76. void try_respawn_connection();
  77. void attach(LanguageClient& client);
  78. void detach();
  79. void set_active_client(LanguageClient& client);
  80. private:
  81. void create_connection();
  82. void show_crash_notification() const;
  83. void show_frequenct_crashes_notification() const;
  84. Language m_language;
  85. Function<NonnullRefPtr<ServerConnection>()> m_connection_creator;
  86. RefPtr<ServerConnection> m_connection;
  87. Core::ElapsedTimer m_last_crash_timer;
  88. bool m_respawn_allowed { true };
  89. };
  90. class ServerConnectionInstances {
  91. public:
  92. static void set_instance_for_language(const String& language_name, NonnullOwnPtr<ServerConnectionWrapper>&& connection_wrapper);
  93. static void remove_instance_for_language(const String& language_name);
  94. static ServerConnectionWrapper* get_instance_wrapper(const String& language_name);
  95. private:
  96. static HashMap<String, NonnullOwnPtr<ServerConnectionWrapper>> s_instance_for_language;
  97. };
  98. class LanguageClient : public Weakable<LanguageClient> {
  99. public:
  100. explicit LanguageClient(ServerConnectionWrapper& connection_wrapper)
  101. : m_connection_wrapper(connection_wrapper)
  102. {
  103. if (m_connection_wrapper.connection()) {
  104. m_previous_client = m_connection_wrapper.connection()->language_client();
  105. VERIFY(m_previous_client.ptr() != this);
  106. m_connection_wrapper.attach(*this);
  107. }
  108. }
  109. virtual ~LanguageClient()
  110. {
  111. // m_connection_wrapper is nullified if the server crashes
  112. if (m_connection_wrapper.connection())
  113. m_connection_wrapper.detach();
  114. VERIFY(m_previous_client.ptr() != this);
  115. if (m_previous_client && m_connection_wrapper.connection())
  116. m_connection_wrapper.set_active_client(*m_previous_client);
  117. }
  118. void set_active_client();
  119. virtual void open_file(const String& path, int fd);
  120. virtual void set_file_content(const String& path, const String& content);
  121. virtual void insert_text(const String& path, const String& text, size_t line, size_t column);
  122. virtual void remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column);
  123. virtual void request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column);
  124. virtual void set_autocomplete_mode(const String& mode);
  125. virtual void search_declaration(const String& path, size_t line, size_t column);
  126. void provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>&) const;
  127. void declaration_found(const String& file, size_t line, size_t column) const;
  128. Function<void(Vector<GUI::AutocompleteProvider::Entry>)> on_autocomplete_suggestions;
  129. Function<void(const String&, size_t, size_t)> on_declaration_found;
  130. private:
  131. ServerConnectionWrapper& m_connection_wrapper;
  132. WeakPtr<LanguageClient> m_previous_client;
  133. };
  134. template<typename ServerConnectionT>
  135. static inline NonnullOwnPtr<LanguageClient> get_language_client(const String& project_path)
  136. {
  137. return make<LanguageClient>(ServerConnectionWrapper::get_or_create<ServerConnectionT>(project_path));
  138. }
  139. template<typename LanguageServerType>
  140. ServerConnectionWrapper& ServerConnectionWrapper::get_or_create(const String& project_path)
  141. {
  142. auto* wrapper = ServerConnectionInstances::get_instance_wrapper(LanguageServerType::language_name());
  143. if (wrapper)
  144. return *wrapper;
  145. auto connection_wrapper_ptr = make<ServerConnectionWrapper>(LanguageServerType::language_name(), [project_path]() { return LanguageServerType::construct(project_path); });
  146. auto& connection_wrapper = *connection_wrapper_ptr;
  147. ServerConnectionInstances::set_instance_for_language(LanguageServerType::language_name(), move(connection_wrapper_ptr));
  148. return connection_wrapper;
  149. }
  150. }