LanguageClient.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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)
  43. : IPC::ServerConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, socket)
  44. {
  45. }
  46. void attach(LanguageClient& client)
  47. {
  48. m_language_client = &client;
  49. }
  50. void detach()
  51. {
  52. m_language_client = nullptr;
  53. }
  54. virtual void handshake() override
  55. {
  56. send_sync<Messages::LanguageServer::Greet>();
  57. }
  58. WeakPtr<LanguageClient> language_client() { return m_language_client; }
  59. template<typename ConcreteType>
  60. static NonnullRefPtr<ServerConnection> get_or_create(const String& project_path)
  61. {
  62. static HashMap<String, NonnullRefPtr<ConcreteType>> s_instances_for_projects;
  63. auto key = LexicalPath { project_path }.string();
  64. if (auto instance = s_instances_for_projects.get(key); instance.has_value())
  65. return *instance.value();
  66. auto connection = ConcreteType::construct();
  67. connection->handshake();
  68. s_instances_for_projects.set(key, *connection);
  69. return *connection;
  70. }
  71. protected:
  72. virtual void handle(const Messages::LanguageClient::AutoCompleteSuggestions&) override;
  73. WeakPtr<LanguageClient> m_language_client;
  74. };
  75. class LanguageClient : public Weakable<LanguageClient> {
  76. public:
  77. explicit LanguageClient(NonnullRefPtr<ServerConnection>&& connection)
  78. : m_connection(*connection)
  79. , m_server_connection(move(connection))
  80. {
  81. m_previous_client = m_connection.language_client();
  82. m_connection.attach(*this);
  83. }
  84. virtual ~LanguageClient()
  85. {
  86. m_connection.detach();
  87. if (m_previous_client)
  88. m_connection.attach(*m_previous_client);
  89. }
  90. virtual void open_file(const String& path, int fd);
  91. virtual void set_file_content(const String& path, const String& content);
  92. virtual void insert_text(const String& path, const String& text, size_t line, size_t column);
  93. virtual void remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column);
  94. virtual void request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column);
  95. virtual void set_autocomplete_mode(const String& mode);
  96. void provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>&);
  97. Function<void(Vector<GUI::AutocompleteProvider::Entry>)> on_autocomplete_suggestions;
  98. private:
  99. ServerConnection& m_connection;
  100. NonnullRefPtr<ServerConnection> m_server_connection;
  101. WeakPtr<LanguageClient> m_previous_client;
  102. };
  103. template<typename ServerConnectionT>
  104. static inline NonnullOwnPtr<LanguageClient> get_language_client(const String& project_path)
  105. {
  106. return make<LanguageClient>(ServerConnection::get_or_create<ServerConnectionT>(project_path));
  107. }
  108. }