#include #include #include #include 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_at(const FactSet &set, std::string_view path, Position position) { std::set 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 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> seen; std::set 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