Files
carbon-lang/toolchain/language_server/language_server.cpp
T
Jon Ross-PerkinsandChandler Carruth 7befe2ce9f Switch custom error stream output to diagnostic (#4846)
This switches most error printing to use diagnostics instead of direct
stream writes, even when not a specific file diagnostic. I'm allowing
empty filenames for this use-case.

This allows a little more specific testing to validate coverage of
output using the diagnostic coverage test. I'm adding a few tests to
cover things that weren't previously tested.

Separately, this also forces a little more standardization in format...
considering how changes like #4568 show effort being spent to _mirror_
diagnostic style, my thought is now to just use diagnostic code where
possible.

Note this also allows incrementally better testing of the language
server; I'm changing the crash fix from #4847 in favor of diagnostic
testing.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2025-01-30 01:58:07 +00:00

50 lines
1.9 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/language_server/language_server.h"
#include "clang-tools-extra/clangd/LSPBinder.h"
#include "clang-tools-extra/clangd/Transport.h"
#include "clang-tools-extra/clangd/support/Logger.h"
#include "common/raw_string_ostream.h"
#include "toolchain/diagnostics/diagnostic_emitter.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(FILE* input_stream, llvm::raw_ostream& output_stream,
llvm::raw_ostream& error_stream, DiagnosticConsumer& consumer)
-> bool {
// TODO: Consider implementing a custom logger that splits vlog to
// vlog_stream when provided. For now, this disables verbose logging.
clang::clangd::StreamLogger logger(error_stream, clang::clangd::Logger::Info);
clang::clangd::LoggingSession logging_session(logger);
// Set up the connection.
std::unique_ptr<clang::clangd::Transport> transport(
clang::clangd::newJSONTransport(input_stream, output_stream,
/*InMirror=*/nullptr,
/*Pretty=*/true));
Context context;
// TODO: Use error_stream in IncomingMessages to report dropped errors.
IncomingMessages incoming(transport.get(), &context);
OutgoingMessages outgoing(transport.get());
// Run the server loop.
llvm::Error err = transport->loop(incoming);
if (err) {
RawStringOstream out;
out << err;
CARBON_DIAGNOSTIC(LanguageServerTransportError, Error, "{0}", std::string);
NoLocDiagnosticEmitter emitter(&consumer);
emitter.Emit(LanguageServerTransportError, out.TakeStr());
return false;
}
return true;
}
} // namespace Carbon::LanguageServer