Serve selection and folding ranges over LSP

A bounded snapshot-only core (vls/features/TextStructure.hpp) builds
nested selection chains (identifier, line, enclosing blocks, document)
and foldable regions (brace blocks, comments, preprocessor pairs,
include groups) while ignoring literals. The server advertises and serves
textDocument/selectionRange with bounded positions and
textDocument/foldingRange through EditorDiagnostics; registered
vls_feature_tests cases cover both.
This commit is contained in:
faumaray committed 2026-12-05 14:41:03 +03:00
1 parent 8ca3dcbf74
commit 624c24d028
12 files changed
+601 -8

No files matched your search

+36
View File
@@ -16,6 +16,7 @@ namespace vls {
namespace {
namespace Protocol = Void::LanguageServer::Protocol;
using Protocol::as_array;
using Protocol::as_object;
using Protocol::as_string;
using Protocol::find_member;
@@ -262,6 +263,8 @@ DispatchResult Server::handle(std::string_view body) {
{"changeNotifications", Json{true}}}}}}}}};
if (diagnostics_) {
capabilities.emplace("documentSymbolProvider", Json{true});
capabilities.emplace("selectionRangeProvider", Json{true});
capabilities.emplace("foldingRangeProvider", Json{true});
}
if (diagnostics_ && diagnostics_->supports_occurrences()) {
capabilities.emplace("hoverProvider", Json{true});
@@ -337,6 +340,39 @@ DispatchResult Server::handle(std::string_view body) {
return error(id, -32602, "Expected a completion item");
return reply(id, "result", diagnostics_->completion_resolve(*params));
}
if (*method == "textDocument/foldingRange") {
if (!diagnostics_)
return error(id, -32601, "Method not found");
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri))
return error(id, -32602, "Expected textDocument.uri");
return reply(id, "result", diagnostics_->folding_ranges(*uri));
}
if (*method == "textDocument/selectionRange") {
if (!diagnostics_)
return error(id, -32601, "Method not found");
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
const auto *positions = as_array(member(params, "positions"));
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri) || !positions ||
positions->size() > 256)
return error(id, -32602, "Expected textDocument.uri and bounded positions");
std::vector<Void::LanguageServer::Documents::Position> selected;
for (const auto &entry : *positions) {
const auto *fields = Protocol::as_object(&entry);
const auto *line = fields ? Protocol::find_member(*fields, "line") : nullptr;
const auto *character = fields ? Protocol::find_member(*fields, "character") : nullptr;
const auto *line_value = line ? std::get_if<std::int64_t>(&line->value) : nullptr;
const auto *character_value =
character ? std::get_if<std::int64_t>(&character->value) : nullptr;
if (!line_value || !character_value || *line_value < 0 || *character_value < 0 ||
*line_value > Void::LanguageServer::Documents::kMaxPositionValue ||
*character_value > Void::LanguageServer::Documents::kMaxPositionValue)
return error(id, -32602, "Expected bounded positions");
selected.push_back({static_cast<std::uint32_t>(*line_value),
static_cast<std::uint32_t>(*character_value)});
}
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
}
if (*method == "textDocument/documentSymbol") {
if (!diagnostics_)
return error(id, -32601, "Method not found");