LanguageClient.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. String m_project_path;
  79. WeakPtr<LanguageClient> m_language_client;
  80. private:
  81. static HashMap<String, NonnullRefPtr<ServerConnection>> s_instances_for_projects;
  82. };
  83. class LanguageClient : public Weakable<LanguageClient> {
  84. public:
  85. explicit LanguageClient(NonnullRefPtr<ServerConnection>&& connection)
  86. : m_server_connection(move(connection))
  87. {
  88. m_previous_client = m_server_connection->language_client();
  89. ASSERT(m_previous_client.ptr() != this);
  90. m_server_connection->attach(*this);
  91. }
  92. virtual ~LanguageClient()
  93. {
  94. m_server_connection->detach();
  95. ASSERT(m_previous_client.ptr() != this);
  96. if (m_previous_client)
  97. m_server_connection->attach(*m_previous_client);
  98. }
  99. void set_active_client();
  100. virtual void open_file(const String& path, int fd);
  101. virtual void set_file_content(const String& path, const String& content);
  102. virtual void insert_text(const String& path, const String& text, size_t line, size_t column);
  103. virtual void remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column);
  104. virtual void request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column);
  105. virtual void set_autocomplete_mode(const String& mode);
  106. void provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>&);
  107. void on_server_crash();
  108. Function<void(Vector<GUI::AutocompleteProvider::Entry>)> on_autocomplete_suggestions;
  109. private:
  110. WeakPtr<ServerConnection> m_server_connection;
  111. WeakPtr<LanguageClient> m_previous_client;
  112. };
  113. template<typename ServerConnectionT>
  114. static inline NonnullOwnPtr<LanguageClient> get_language_client(const String& project_path)
  115. {
  116. return make<LanguageClient>(ServerConnection::get_or_create<ServerConnectionT>(project_path));
  117. }
  118. }