Refactor the language server structure. (#4721)

I'm trying to make the LSP look more like the rest of the toolchain. I'm
trying to separate the handlers from the transport layer, and remove the
multiple inheritance aspect. Also fixing some style issues, switching to
`Map`, and removing an unnecessary copt.

I'm using `Context` for the central object for consistency with other
portions of the toolchain. In order to get the `handle_*` files working,
I'm using LLVM's registry class. It has a quirk that I can't register
two registries in the same cpp file, so there are two one-line cpp
files.

In order to help show the delta (or lack thereof) for actual
implementation, I've copied server.cpp over handle_* and undone that in
two commits. See the third and fourth commits on the PR history for
that.
This commit is contained in:
Jon Ross-Perkins
2025-01-08 06:01:51 +00:00
committed by GitHub
parent aaef516600
commit 96d836f965
12 changed files with 489 additions and 276 deletions
+24 -3
View File
@@ -4,14 +4,35 @@
#include "toolchain/language_server/language_server.h"
#include "toolchain/language_server/server.h"
#include "clang-tools-extra/clangd/LSPBinder.h"
#include "clang-tools-extra/clangd/Transport.h"
#include "toolchain/language_server/context.h"
#include "toolchain/language_server/incoming_messages.h"
#include "toolchain/language_server/outgoing_messages.h"
namespace Carbon::LanguageServer {
auto Run(std::FILE* input_stream, llvm::raw_ostream& output_stream)
-> ErrorOr<Success> {
Server server(input_stream, output_stream);
return server.Run();
// Set up the connection.
std::unique_ptr<clang::clangd::Transport> transport(
clang::clangd::newJSONTransport(input_stream, output_stream,
/*InMirror=*/nullptr,
/*Pretty=*/true));
Context context;
IncomingMessages incoming(transport.get(), &context);
OutgoingMessages outgoing(transport.get());
// Run the server loop.
llvm::Error err = transport->loop(incoming);
if (err) {
std::string str;
llvm::raw_string_ostream out(str);
out << err;
return Error(str);
} else {
return Success();
}
}
} // namespace Carbon::LanguageServer