From 8ed96eb27c332bd86dafd3da78b362668e00e0b9 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 23 Jan 2021 19:48:42 +0200 Subject: HackStudio: Attach previous Language Client when detaching Previously, if a new LanguageClient was created & destroyed, the ServerConnection to the language server would be left without an attached LanguageClient. As a result, auto-completion results would not be updated in the UI. Starting with this commit, the LanguageClient holds a WeakPtr to the previous LanguageClient that was attached to the ServerConnection, and re-attaches it after detaching itself. --- Userland/DevTools/HackStudio/LanguageClient.cpp | 7 +++++-- Userland/DevTools/HackStudio/LanguageClient.h | 12 ++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'Userland/DevTools') diff --git a/Userland/DevTools/HackStudio/LanguageClient.cpp b/Userland/DevTools/HackStudio/LanguageClient.cpp index a10df6d4b6..e02b2c98cf 100644 --- a/Userland/DevTools/HackStudio/LanguageClient.cpp +++ b/Userland/DevTools/HackStudio/LanguageClient.cpp @@ -32,8 +32,11 @@ namespace HackStudio { void ServerConnection::handle(const Messages::LanguageClient::AutoCompleteSuggestions& message) { - if (m_language_client) - m_language_client->provide_autocomplete_suggestions(message.suggestions()); + if (!m_language_client) { + dbgln("Language Server connection has no attached language client"); + return; + } + m_language_client->provide_autocomplete_suggestions(message.suggestions()); } void LanguageClient::open_file(const String& path, int fd) diff --git a/Userland/DevTools/HackStudio/LanguageClient.h b/Userland/DevTools/HackStudio/LanguageClient.h index bf294a7ea6..03c362f6a7 100644 --- a/Userland/DevTools/HackStudio/LanguageClient.h +++ b/Userland/DevTools/HackStudio/LanguageClient.h @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include @@ -64,6 +66,8 @@ public: set_my_client_id(response->client_id()); } + WeakPtr language_client() { return m_language_client; } + template static NonnullRefPtr get_or_create(const String& project_path) { @@ -81,21 +85,24 @@ public: protected: virtual void handle(const Messages::LanguageClient::AutoCompleteSuggestions&) override; - LanguageClient* m_language_client { nullptr }; + WeakPtr m_language_client; }; -class LanguageClient { +class LanguageClient : public Weakable { public: explicit LanguageClient(NonnullRefPtr&& connection) : m_connection(*connection) , m_server_connection(move(connection)) { + m_previous_client = m_connection.language_client(); m_connection.attach(*this); } virtual ~LanguageClient() { m_connection.detach(); + if (m_previous_client) + m_connection.attach(*m_previous_client); } virtual void open_file(const String& path, int fd); @@ -111,6 +118,7 @@ public: private: ServerConnection& m_connection; NonnullRefPtr m_server_connection; + WeakPtr m_previous_client; }; template -- cgit v1.2.3