Files
Language-Server/src/navigation/Navigation.cpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

133 lines
4.8 KiB
C++

#include <vls/navigation/Navigation.hpp>
#include <algorithm>
#include <set>
#include <tuple>
namespace vls::navigation {
namespace {
bool contains(const Range &range, Position position) {
return range.start <= position && position <= range.end;
}
bool identifier(std::string_view name) {
if (name.empty() || name.size() > 256)
return false;
for (std::size_t index = 0; index != name.size(); ++index) {
const char c = name[index];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' ||
(index && c >= '0' && c <= '9')))
return false;
}
return true;
}
bool generated(std::string_view name) {
return name.contains('$') || name.starts_with("_GLOBAL__") || name.contains('<');
}
} // namespace
std::expected<Binding, Refusal> binding_at(const FactSet &set, std::string_view path,
Position position) {
std::set<Binding> candidates;
for (const auto &fact : set.facts)
if (fact.source == path && contains(fact.use, position))
candidates.insert(fact.binding);
if (candidates.empty())
return std::unexpected{Refusal::unknown_binding};
if (candidates.size() != 1)
return std::unexpected{Refusal::ambiguous_binding};
return *candidates.begin();
}
References references(const FactSet &set, const Binding &binding, std::size_t limit) {
References result;
std::set<Location> unique;
for (const auto &fact : set.facts)
if (fact.binding == binding)
unique.insert(Location{fact.source, fact.use});
for (const auto &location : unique) {
if (result.locations.size() == limit) {
result.truncated = true;
break;
}
result.locations.push_back(location);
}
for (const auto &context : set.contexts)
if (!context.complete)
result.incomplete_contexts.push_back(context.context);
std::ranges::sort(result.incomplete_contexts);
result.coverage_complete = result.incomplete_contexts.empty();
return result;
}
RenamePlan prepare_rename(const FactSet &set, std::string_view path, Position position,
std::string_view replacement, bool transactional_client) {
RenamePlan plan;
const auto binding = binding_at(set, path, position);
if (!binding) {
plan.refusal = binding.error();
plan.detail = "no unique binding at the requested position";
return plan;
}
if (!identifier(replacement)) {
plan.refusal = Refusal::invalid_name;
plan.detail = "the replacement is not a plain identifier";
return plan;
}
if (generated(replacement)) {
plan.refusal = Refusal::generated_name;
plan.detail = "the replacement looks like a generated name";
return plan;
}
if (replacement == binding->name) {
plan.refusal = Refusal::unchanged_name;
plan.detail = "the replacement equals the current spelling";
return plan;
}
for (const auto &context : set.contexts)
if (!context.complete) {
plan.refusal = Refusal::incomplete_coverage;
plan.detail = "incomplete analysis coverage in " + context.context +
(context.reason.empty() ? "" : ": " + context.reason);
return plan;
}
std::set<std::pair<std::string, Range>> seen;
std::set<std::string> files;
for (const auto &fact : set.facts) {
if (fact.binding != *binding)
continue;
if (const auto current = set.current_sources.find(fact.source);
current != set.current_sources.end() && current->second != fact.content) {
plan.refusal = Refusal::stale_source;
plan.detail = "the source changed after analysis: " + fact.source;
return plan;
}
if (!fact.editable) {
plan.refusal = Refusal::non_editable_origin;
plan.detail = "the binding has a non-editable origin: " + fact.source;
return plan;
}
if (seen.emplace(fact.source, fact.use).second) {
files.insert(fact.source);
plan.edits.push_back({fact.source, fact.use, std::string{replacement}, fact.content});
}
}
if (plan.edits.empty()) {
plan.refusal = Refusal::unknown_binding;
plan.detail = "the binding has no proven occurrence";
return plan;
}
if (files.size() > 1 && !transactional_client) {
plan.refusal = Refusal::multi_file_without_transaction;
plan.detail = "multi-file rename requires a transactional client";
return plan;
}
std::ranges::sort(plan.edits, [](const auto &left, const auto &right) {
return std::tie(left.path, left.range) < std::tie(right.path, right.range);
});
return plan;
}
} // namespace vls::navigation