Files
carbon-lang/toolchain/language_server/handle_text_document.cpp
T
Jon Ross-Perkins 9e466b9335 Cache calculated file state in LSP (#4897)
Add caching of parsed documents, and testing of the textDocument
handlers. This is based on #4896, which splits out some of the
boilerplate to calls.

Note, this caches the entire parse state because we'll want to try to
emit diagnostics when we see the update, without waiting. It may be
helpful to do that asynchronously, but we don't want to wait for another
call (such as documentSymbol). Really, we'll probably want to also add
check for diagnostics, at least.
2025-02-07 01:04:42 +00:00

67 lines
2.3 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(filename.str()); });
insert_result.value().SetText(context, 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.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