LanguageClient.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "AutoCompleteResponse.h"
  8. #include "Language.h"
  9. #include <AK/Forward.h>
  10. #include <AK/LexicalPath.h>
  11. #include <AK/Types.h>
  12. #include <AK/WeakPtr.h>
  13. #include <AK/Weakable.h>
  14. #include <LibCore/ElapsedTimer.h>
  15. #include <LibCpp/Preprocessor.h>
  16. #include <LibIPC/ServerConnection.h>
  17. #include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
  18. #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
  19. namespace HackStudio {
  20. class LanguageClient;
  21. class ServerConnectionWrapper;
  22. class ServerConnection
  23. : public IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>
  24. , public LanguageClientEndpoint {
  25. friend class ServerConnectionWrapper;
  26. public:
  27. ServerConnection(const StringView& socket, const String& project_path)
  28. : IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, socket)
  29. {
  30. m_project_path = project_path;
  31. async_greet(m_project_path);
  32. }
  33. WeakPtr<LanguageClient> language_client() { return m_current_language_client; }
  34. const String& project_path() const { return m_project_path; }
  35. virtual void die() override;
  36. protected:
  37. virtual void auto_complete_suggestions(Vector<GUI::AutocompleteProvider::Entry> const&) override;
  38. virtual void declaration_location(GUI::AutocompleteProvider::ProjectLocation const&) override;
  39. virtual void declarations_in_document(String const&, Vector<GUI::AutocompleteProvider::Declaration> const&) override;
  40. virtual void todo_entries_in_document(String const&, Vector<Cpp::Parser::TodoEntry> const&) override;
  41. virtual void parameters_hint_result(Vector<String> const&, int index) override;
  42. void set_wrapper(ServerConnectionWrapper& wrapper) { m_wrapper = &wrapper; }
  43. String m_project_path;
  44. WeakPtr<LanguageClient> m_current_language_client;
  45. ServerConnectionWrapper* m_wrapper { nullptr };
  46. };
  47. class ServerConnectionWrapper {
  48. AK_MAKE_NONCOPYABLE(ServerConnectionWrapper);
  49. public:
  50. explicit ServerConnectionWrapper(const String& language_name, Function<NonnullRefPtr<ServerConnection>()> connection_creator);
  51. ~ServerConnectionWrapper() = default;
  52. template<typename LanguageServerType>
  53. static ServerConnectionWrapper& get_or_create(const String& project_path);
  54. Language language() const { return m_language; }
  55. ServerConnection* connection();
  56. void on_crash();
  57. void try_respawn_connection();
  58. void attach(LanguageClient& client);
  59. void detach();
  60. void set_active_client(LanguageClient& client);
  61. private:
  62. void create_connection();
  63. void show_crash_notification() const;
  64. void show_frequenct_crashes_notification() const;
  65. Language m_language;
  66. Function<NonnullRefPtr<ServerConnection>()> m_connection_creator;
  67. RefPtr<ServerConnection> m_connection;
  68. Core::ElapsedTimer m_last_crash_timer;
  69. bool m_respawn_allowed { true };
  70. };
  71. class ServerConnectionInstances {
  72. public:
  73. static void set_instance_for_language(const String& language_name, NonnullOwnPtr<ServerConnectionWrapper>&& connection_wrapper);
  74. static void remove_instance_for_language(const String& language_name);
  75. static ServerConnectionWrapper* get_instance_wrapper(const String& language_name);
  76. private:
  77. static HashMap<String, NonnullOwnPtr<ServerConnectionWrapper>> s_instance_for_language;
  78. };
  79. class LanguageClient : public Weakable<LanguageClient> {
  80. public:
  81. explicit LanguageClient(ServerConnectionWrapper& connection_wrapper)
  82. : m_connection_wrapper(connection_wrapper)
  83. {
  84. if (m_connection_wrapper.connection()) {
  85. m_previous_client = m_connection_wrapper.connection()->language_client();
  86. VERIFY(m_previous_client.ptr() != this);
  87. m_connection_wrapper.attach(*this);
  88. }
  89. }
  90. virtual ~LanguageClient()
  91. {
  92. // m_connection_wrapper is nullified if the server crashes
  93. if (m_connection_wrapper.connection())
  94. m_connection_wrapper.detach();
  95. VERIFY(m_previous_client.ptr() != this);
  96. if (m_previous_client && m_connection_wrapper.connection())
  97. m_connection_wrapper.set_active_client(*m_previous_client);
  98. }
  99. Language language() const { return m_connection_wrapper.language(); }
  100. void set_active_client();
  101. virtual void open_file(const String& path, int fd);
  102. virtual void set_file_content(const String& path, const String& content);
  103. virtual void insert_text(const String& path, const String& text, size_t line, size_t column);
  104. virtual void remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column);
  105. virtual void request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column);
  106. virtual void search_declaration(const String& path, size_t line, size_t column);
  107. virtual void get_parameters_hint(const String& path, size_t line, size_t column);
  108. void provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>&) const;
  109. void declaration_found(const String& file, size_t line, size_t column) const;
  110. void parameters_hint_result(Vector<String> const& params, size_t argument_index) const;
  111. // Callbacks that get called when the result of a language server query is ready
  112. Function<void(Vector<GUI::AutocompleteProvider::Entry>)> on_autocomplete_suggestions;
  113. Function<void(const String&, size_t, size_t)> on_declaration_found;
  114. Function<void(Vector<String> const&, size_t)> on_function_parameters_hint_result;
  115. private:
  116. ServerConnectionWrapper& m_connection_wrapper;
  117. WeakPtr<LanguageClient> m_previous_client;
  118. };
  119. template<typename ServerConnectionT>
  120. static inline NonnullOwnPtr<LanguageClient> get_language_client(const String& project_path)
  121. {
  122. return make<LanguageClient>(ServerConnectionWrapper::get_or_create<ServerConnectionT>(project_path));
  123. }
  124. template<typename LanguageServerType>
  125. ServerConnectionWrapper& ServerConnectionWrapper::get_or_create(const String& project_path)
  126. {
  127. auto* wrapper = ServerConnectionInstances::get_instance_wrapper(LanguageServerType::language_name());
  128. if (wrapper)
  129. return *wrapper;
  130. auto connection_wrapper_ptr = make<ServerConnectionWrapper>(LanguageServerType::language_name(), [project_path]() { return LanguageServerType::construct(project_path); });
  131. auto& connection_wrapper = *connection_wrapper_ptr;
  132. ServerConnectionInstances::set_instance_for_language(LanguageServerType::language_name(), move(connection_wrapper_ptr));
  133. return connection_wrapper;
  134. }
  135. }