Serve document symbols and highlights over LSP

SymbolIndex::find_by_path exposes one document's records with the same
overlay shadowing and coverage as find. EditorDiagnostics maps them to
textDocument/documentSymbol, and a bounded same-file highlight core
(vls/features/DocumentSymbols.hpp) backs textDocument/documentHighlight
from verified occurrence facts with the ambiguous-binding rule. Both are
advertised and exercised over a real stdio session with the stock
observer. Record the LSP parity matrix.
This commit is contained in:
faumaray committed 2026-12-05 14:21:26 +03:00
1 parent da8003f6d4
commit 8ca3dcbf74
16 files changed
+366 -8

No files matched your search

+16 -2
View File
@@ -260,9 +260,13 @@ DispatchResult Server::handle(std::string_view body) {
Json{Json::Object{
{"workspaceFolders", Json{Json::Object{{"supported", Json{true}},
{"changeNotifications", Json{true}}}}}}}}};
if (diagnostics_) {
capabilities.emplace("documentSymbolProvider", Json{true});
}
if (diagnostics_ && diagnostics_->supports_occurrences()) {
capabilities.emplace("hoverProvider", Json{true});
capabilities.emplace("definitionProvider", Json{true});
capabilities.emplace("documentHighlightProvider", Json{true});
}
if (diagnostics_)
capabilities.emplace("completionProvider",
@@ -301,7 +305,7 @@ DispatchResult Server::handle(std::string_view body) {
: Json{Json::Object{{"state", Json{std::string{"disabled"}}}}});
}
if (*method == "textDocument/hover" || *method == "textDocument/definition" ||
*method == "textDocument/signatureHelp") {
*method == "textDocument/signatureHelp" || *method == "textDocument/documentHighlight") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())
return error(id, -32601, "Method not found");
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
@@ -321,7 +325,9 @@ DispatchResult Server::handle(std::string_view body) {
auto result = *method == "textDocument/hover" ? diagnostics_->hover(*uri, selected)
: *method == "textDocument/definition"
? diagnostics_->definition(*uri, selected)
: diagnostics_->signature_help(*uri, selected);
: *method == "textDocument/signatureHelp"
? diagnostics_->signature_help(*uri, selected)
: diagnostics_->document_highlights(*uri, selected);
return reply(id, "result", std::move(result));
}
if (*method == "completionItem/resolve") {
@@ -331,6 +337,14 @@ 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/documentSymbol") {
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_->document_symbols(*uri));
}
if (*method == "textDocument/completion") {
if (!diagnostics_)
return error(id, -32601, "Method not found");
+29
View File
@@ -0,0 +1,29 @@
#include <vls/features/DocumentSymbols.hpp>
#include <set>
#include <tuple>
namespace vls::features::documents {
std::vector<Range> highlights(std::span<const Use> uses, std::string_view path, Position position,
std::size_t limit) {
std::set<Binding> candidates;
for (const auto &use : uses)
if (use.path == path && use.range.start <= position && position <= use.range.end)
candidates.insert(use.binding);
std::vector<Range> result;
if (candidates.size() != 1)
return result;
std::set<Range> unique;
for (const auto &use : uses)
if (use.path == path && use.binding == *candidates.begin())
unique.insert(use.range);
for (const auto &range : unique) {
if (result.size() == limit)
break;
result.push_back(range);
}
return result;
}
} // namespace vls::features::documents
+34
View File
@@ -462,6 +462,40 @@ Result SymbolIndex::find(const Query &query) const {
return result;
}
Result SymbolIndex::find_by_path(std::string_view path, std::size_t limit) const {
auto &state = *state_;
std::lock_guard lock{state.mutex};
Result result;
result.coverage = state.coverage_locked(std::nullopt);
std::set<std::tuple<std::string, SymbolKind, Location>> seen;
std::set<std::string> shadowed;
for (const auto &[overlay_path, overlay] : state.overlays)
shadowed.insert(overlay_path);
const auto consider = [&](const SymbolRecord &record) {
if (record.location.path != path)
return;
if (!seen.emplace(record.name, record.kind, record.location).second)
return;
result.symbols.push_back(record);
};
for (const auto &[name, entry] : state.contexts)
for (const auto &record : entry.records)
if (!shadowed.contains(record.location.path))
consider(record);
for (const auto &[overlay_path, overlay] : state.overlays)
for (const auto &record : overlay.records)
consider(record);
std::ranges::sort(result.symbols, [](const auto &left, const auto &right) {
return std::tie(left.location, left.name, left.kind) <
std::tie(right.location, right.name, right.kind);
});
if (result.symbols.size() > limit) {
result.symbols.resize(limit);
result.truncated = true;
}
return result;
}
Coverage SymbolIndex::coverage() const {
auto &state = *state_;
std::lock_guard lock{state.mutex};
+80
View File
@@ -1,6 +1,7 @@
#include <vls/semantic/EditorDiagnostics.hpp>
#include <vls/features/Completion.hpp>
#include <vls/features/DocumentSymbols.hpp>
#include <vls/features/IndexNavigation.hpp>
#include <vls/features/SignatureHelp.hpp>
#include <vls/index/SymbolIndex.hpp>
@@ -973,6 +974,85 @@ Json EditorDiagnostics::completion_resolve(const Json &item) const {
return Json{std::move(enriched)};
}
Json EditorDiagnostics::document_symbols(std::string_view uri) const {
auto &s = *state_;
const auto identity = Documents::normalize_uri(uri);
if (!identity || !identity->is_file_uri)
return Json{nullptr};
index::Result records;
{
std::lock_guard lock{s.mutex};
records = s.symbols.find_by_path(identity->canonical_path.native());
}
Json::Array symbols;
std::set<std::tuple<std::string, int, std::uint32_t, std::uint32_t>> seen;
for (const auto &record : records.symbols) {
const auto &location = record.location;
if (!seen.emplace(record.name, static_cast<int>(record.kind), location.line,
location.character)
.second)
continue;
const auto range = range_json(Documents::Range{{location.line, location.character},
{location.endLine, location.endCharacter}});
symbols.push_back(Json{Json::Object{{"name", Json{record.name}},
{"kind", Json{symbol_kind(record.kind)}},
{"range", range},
{"selectionRange", range}}});
}
return Json{std::move(symbols)};
}
Json EditorDiagnostics::document_highlights(std::string_view uri,
Documents::Position position) const {
auto &s = *state_;
auto key = Documents::client_uri_key(uri);
std::shared_ptr<const Receipt> receipt;
std::shared_ptr<const std::vector<Occurrence>> facts;
{
std::lock_guard lock{s.mutex};
const auto i = key ? s.entries.find(*key) : s.entries.end();
if (i == s.entries.end())
return Json{nullptr};
receipt = i->second.receipt;
facts = i->second.occurrences;
}
if (!receipt || !facts || !s.valid(*receipt))
return Json{nullptr};
const Documents::DocumentSnapshot *snapshot = nullptr;
for (const auto &entry : receipt->capture->entries())
if (entry.snapshot && entry.snapshot->identity.canonical_uri == *key) {
snapshot = entry.snapshot.get();
break;
}
if (!snapshot)
return Json{nullptr};
std::vector<features::documents::Use> uses;
for (const auto &occurrence : *facts) {
if (occurrence.use_file.get() != snapshot)
continue;
features::documents::Use use;
use.binding.name = occurrence.name;
if (occurrence.has_definition())
use.binding.definition = features::documents::Location{
occurrence.definition_file->identity.canonical_path.native(),
{{occurrence.definition.start.line, occurrence.definition.start.character},
{occurrence.definition.end.line, occurrence.definition.end.character}}};
use.path = snapshot->identity.canonical_path.native();
use.range = {{occurrence.use.start.line, occurrence.use.start.character},
{occurrence.use.end.line, occurrence.use.end.character}};
uses.push_back(std::move(use));
}
const auto ranges = features::documents::highlights(
uses, snapshot->identity.canonical_path.native(), {position.line, position.character});
Json::Array result;
for (const auto &range : ranges)
result.push_back(Json{Json::Object{
{"range", range_json(Documents::Range{{range.start.line, range.start.character},
{range.end.line, range.end.character}})},
{"kind", Json{std::int64_t{1}}}}});
return Json{std::move(result)};
}
void EditorDiagnostics::set_transactional_rename(bool supported) {
std::lock_guard lock{state_->mutex};
state_->transactional_rename = supported;