Files
carbon-lang/toolchain/language_server/handle_initialize.cpp
T
Richard Smith 03939a9223 Add language server support for SemIR in testdata. (#7803)
Add hover cards and jump to declaration / definition / reference for the
formatted SemIR that appears in check tests. This is done by adding a
heuristic "parser" for SemIR to the language server. The
cross-references are strictly best-effort, since this is just a tool for
Carbon developers, not a user-facing facility.

A couple of other changes made along the way:

* file_test tests with an AUTOUPDATE-SPLIT no longer look for CHECK:
lines outside that split. This was motivated by the tests for this new
facility including CHECK: lines as part of the test input.
* An agent skill for working on the language server, tracking some
things that cost Claude time when working on this.

Assisted-by: Claude via Antigravity
2026-09-23 23:44:32 +00:00

58 lines
2.5 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 <utility>
#include "llvm/ADT/STLExtras.h"
#include "toolchain/language_server/handle.h"
namespace Carbon::LanguageServer {
// Picks the encoding used to measure `Position::character` on the wire.
// Use UTF-8 if offered, since Carbon source is natively UTF-8. Otherwise fall
// back to UTF-16, to support VS Code and old LSP clients.
static auto NegotiatePositionEncoding(
const clang::clangd::ClientCapabilities& capabilities)
-> clang::clangd::OffsetEncoding {
if (capabilities.PositionEncodings &&
llvm::is_contained(*capabilities.PositionEncodings,
clang::clangd::OffsetEncoding::UTF8)) {
return clang::clangd::OffsetEncoding::UTF8;
}
return clang::clangd::OffsetEncoding::UTF16;
}
auto HandleInitialize(
Context& context, const clang::clangd::InitializeParams& params,
llvm::function_ref<auto(llvm::Expected<llvm::json::Object>)->void> on_done)
-> void {
auto encoding = NegotiatePositionEncoding(params.capabilities);
context.SetPositionEncoding(encoding);
llvm::json::Object capabilities{{"declarationProvider", true},
{"definitionProvider", true},
{"documentFormattingProvider", true},
{"documentSymbolProvider", true},
{"hoverProvider", true},
{"implementationProvider", true},
{"positionEncoding", encoding},
{"referencesProvider", true},
{"textDocumentSync", /*Incremental=*/2},
{"typeDefinitionProvider", true}};
llvm::json::Object reply{{"capabilities", std::move(capabilities)}};
on_done(reply);
}
// Implements `initialized`:
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialized
auto HandleInitialized(Context& /*context*/,
const clang::clangd::NoParams& /*params*/) -> void {
// Nothing to do, but every client sends this, so we handle it rather than
// warning about an unsupported notification.
// TODO: This is when we would use `client/registerCapability` for any
// capabilities we want to register dynamically.
}
} // namespace Carbon::LanguageServer