1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

HackStudio: Abstract away language-server details

This commit moves all the logic that deals with the language server
(from HackStudio) into a LanguageClient class, provides some functions
to make constructing them easier, and makes all language servers use a
singular IPC definition.
Also fixes the FIXME about making the autocompletion async.
This makes adding language servers in the future significantly less
duplicate-y, and significantly easier :^)
This commit is contained in:
AnotherTest 2020-10-02 03:01:33 +03:30 committed by Andreas Kling
parent 44f9637e20
commit ac5e08a541
17 changed files with 291 additions and 118 deletions

View file

@ -1,16 +1,15 @@
compile_ipc(CppLanguageServer.ipc CppLanguageServerEndpoint.h)
compile_ipc(CppLanguageClient.ipc CppLanguageClientEndpoint.h)
set(SOURCES
ClientConnection.cpp
main.cpp
CppLanguageServerEndpoint.h
CppLanguageClientEndpoint.h
AutoComplete.cpp
)
set(GENERATED_SOURCES
../LanguageServerEndpoint.h
../LanguageClientEndpoint.h)
serenity_bin(CppLanguageServer)
# We link with LibGUI because we use GUI::TextDocument to update
# We link with LibGUI because we use GUI::TextDocument to update
# the content of files according to the edit actions we receive over IPC.
target_link_libraries(CppLanguageServer LibIPC LibCpp LibGUI)

View file

@ -38,7 +38,7 @@ namespace LanguageServers::Cpp {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: IPC::ClientConnection<CppLanguageClientEndpoint, CppLanguageServerEndpoint>(*this, move(socket), client_id)
: IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
{
s_connections.set(client_id, *this);
}
@ -53,13 +53,13 @@ void ClientConnection::die()
exit(0);
}
OwnPtr<Messages::CppLanguageServer::GreetResponse> ClientConnection::handle(const Messages::CppLanguageServer::Greet& message)
OwnPtr<Messages::LanguageServer::GreetResponse> ClientConnection::handle(const Messages::LanguageServer::Greet& message)
{
m_project_root = LexicalPath(message.project_root());
#ifdef DEBUG_CPP_LANGUAGE_SERVER
dbg() << "project_root: " << m_project_root.string();
#endif
return make<Messages::CppLanguageServer::GreetResponse>(client_id());
return make<Messages::LanguageServer::GreetResponse>(client_id());
}
class DefaultDocumentClient final : public GUI::TextDocument::Client {
@ -74,12 +74,12 @@ public:
virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
virtual bool is_automatic_indentation_enabled() const override { return true; }
virtual int soft_tab_width() const { return 4; }
virtual int soft_tab_width() const override { return 4; }
};
static DefaultDocumentClient s_default_document_client;
void ClientConnection::handle(const Messages::CppLanguageServer::FileOpened& message)
void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
{
LexicalPath file_path(String::format("%s/%s", m_project_root.string().characters(), message.file_name().characters()));
#ifdef DEBUG_CPP_LANGUAGE_SERVER
@ -103,7 +103,7 @@ void ClientConnection::handle(const Messages::CppLanguageServer::FileOpened& mes
#endif
}
void ClientConnection::handle(const Messages::CppLanguageServer::FileEditInsertText& message)
void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
{
#ifdef DEBUG_CPP_LANGUAGE_SERVER
dbg() << "InsertText for file: " << message.file_name();
@ -122,7 +122,7 @@ void ClientConnection::handle(const Messages::CppLanguageServer::FileEditInsertT
#endif
}
void ClientConnection::handle(const Messages::CppLanguageServer::FileEditRemoveText& message)
void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
{
#ifdef DEBUG_CPP_LANGUAGE_SERVER
dbg() << "RemoveText for file: " << message.file_name();
@ -147,9 +147,7 @@ void ClientConnection::handle(const Messages::CppLanguageServer::FileEditRemoveT
#endif
}
// FIXME: The work we do here could be taxing and block the client for a significant time.
// Would should turn this to an async IPC endpoint and report the reuslts back in a separate Server->Client message.
OwnPtr<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse> ClientConnection::handle(const Messages::CppLanguageServer::AutoCompleteSuggestions& message)
void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
{
#ifdef DEBUG_CPP_LANGUAGE_SERVER
dbg() << "AutoCompleteSuggestions for: " << message.file_name() << " " << message.cursor_line() << ":" << message.cursor_column();
@ -158,11 +156,11 @@ OwnPtr<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse> ClientConne
auto document = document_for(message.file_name());
if (!document) {
dbg() << "file " << message.file_name() << " has not been opened";
return nullptr;
return;
}
Vector<String> suggestions = AutoComplete::get_suggestions(document->text(), { (size_t)message.cursor_line(), (size_t)message.cursor_column() });
return make<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse>(suggestions);
post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
}
RefPtr<GUI::TextDocument> ClientConnection::document_for(const String& file_name)
@ -174,7 +172,7 @@ RefPtr<GUI::TextDocument> ClientConnection::document_for(const String& file_name
return document_optional.value();
}
void ClientConnection::handle(const Messages::CppLanguageServer::SetFileContent& message)
void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
{
auto document = document_for(message.file_name());
if (!document) {

View file

@ -28,16 +28,16 @@
#include <AK/HashMap.h>
#include <AK/LexicalPath.h>
#include <DevTools/HackStudio/LanguageServers/Cpp/CppLanguageClientEndpoint.h>
#include <DevTools/HackStudio/LanguageServers/Cpp/CppLanguageServerEndpoint.h>
#include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
#include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
#include <LibGUI/TextDocument.h>
#include <LibIPC/ClientConnection.h>
namespace LanguageServers::Cpp {
class ClientConnection final
: public IPC::ClientConnection<CppLanguageClientEndpoint, CppLanguageServerEndpoint>
, public CppLanguageServerEndpoint {
: public IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>
, public LanguageServerEndpoint {
C_OBJECT(ClientConnection);
public:
@ -47,12 +47,12 @@ public:
virtual void die() override;
private:
virtual OwnPtr<Messages::CppLanguageServer::GreetResponse> handle(const Messages::CppLanguageServer::Greet&) override;
virtual void handle(const Messages::CppLanguageServer::FileOpened&) override;
virtual void handle(const Messages::CppLanguageServer::FileEditInsertText&) override;
virtual void handle(const Messages::CppLanguageServer::FileEditRemoveText&) override;
virtual void handle(const Messages::CppLanguageServer::SetFileContent&) override;
virtual OwnPtr<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse> handle(const Messages::CppLanguageServer::AutoCompleteSuggestions&) override;
virtual OwnPtr<Messages::LanguageServer::GreetResponse> handle(const Messages::LanguageServer::Greet&) override;
virtual void handle(const Messages::LanguageServer::FileOpened&) override;
virtual void handle(const Messages::LanguageServer::FileEditInsertText&) override;
virtual void handle(const Messages::LanguageServer::FileEditRemoveText&) override;
virtual void handle(const Messages::LanguageServer::SetFileContent&) override;
virtual void handle(const Messages::LanguageServer::AutoCompleteSuggestions&) override;
RefPtr<GUI::TextDocument> document_for(const String& file_name);

View file

@ -1,4 +0,0 @@
endpoint CppLanguageClient = 8002
{
Dummy() =|
}

View file

@ -1,11 +0,0 @@
endpoint CppLanguageServer = 8001
{
Greet(String project_root) => (i32 client_id)
FileOpened(String file_name) =|
FileEditInsertText(String file_name, String text, i32 start_line, i32 start_column) =|
FileEditRemoveText(String file_name, i32 start_line, i32 start_column, i32 end_line, i32 end_column) =|
SetFileContent(String file_name, String content) =|
AutoCompleteSuggestions(String file_name, i32 cursor_line, i32 cursor_column) => (Vector<String> suggestions)
}