LanguageClient.h 4.9 KB

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