Files
carbon-lang/toolchain/language_server/handle_text_document.cpp
T
Jon Ross-Perkins f89985d0f4 Add support for publishDiagnostics (#4912)
<img width="536" alt="Screenshot 2025-02-06 at 3 02 18 PM"
src="https://github.com/user-attachments/assets/27b7673d-f8d4-4f9f-9e5d-20c3a88b8c78"
/>

Requires the caching work in #4897
2025-02-07 23:04:35 +00:00

69 lines
2.4 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/handle.h"
namespace Carbon::LanguageServer {
auto HandleDidOpenTextDocument(
Context& context, const clang::clangd::DidOpenTextDocumentParams& params)
-> void {
llvm::StringRef filename = params.textDocument.uri.file();
if (!filename.ends_with(".carbon")) {
// Ignore non-Carbon files.
return;
}
auto insert_result = context.files().Insert(
filename, [&] { return Context::File(params.textDocument.uri); });
insert_result.value().SetText(context, params.textDocument.version,
params.textDocument.text);
if (!insert_result.is_inserted()) {
CARBON_DIAGNOSTIC(LanguageServerOpenDuplicateFile, Warning,
"duplicate open file request; updating content");
context.file_emitter().Emit(filename, LanguageServerOpenDuplicateFile);
}
}
auto HandleDidChangeTextDocument(
Context& context, const clang::clangd::DidChangeTextDocumentParams& params)
-> void {
llvm::StringRef filename = params.textDocument.uri.file();
if (!filename.ends_with(".carbon")) {
// Ignore non-Carbon files.
return;
}
// Full text is sent if full sync is specified in capabilities.
if (params.contentChanges.size() != 1) {
CARBON_DIAGNOSTIC(LanguageServerUnsupportedChanges, Warning,
"received unsupported contentChanges count: {0}", int);
context.file_emitter().Emit(filename, LanguageServerUnsupportedChanges,
params.contentChanges.size());
return;
}
if (auto* file = context.LookupFile(filename)) {
file->SetText(context, params.textDocument.version,
params.contentChanges[0].text);
}
}
auto HandleDidCloseTextDocument(
Context& context, const clang::clangd::DidCloseTextDocumentParams& params)
-> void {
llvm::StringRef filename = params.textDocument.uri.file();
if (!filename.ends_with(".carbon")) {
// Ignore non-Carbon files.
return;
}
if (!context.files().Erase(filename)) {
CARBON_DIAGNOSTIC(LanguageServerCloseUnknownFile, Warning,
"tried closing unknown file; ignoring request");
context.file_emitter().Emit(filename, LanguageServerCloseUnknownFile);
}
}
} // namespace Carbon::LanguageServer