The observer emits an optional extent inside each proven definition span from the body's last statement finish (the C++ frontend does not set function_end_locus). The occurrence decoder binds it to the same snapshot through the negotiated encoding and rejects malformed extents; call hierarchy attributes nested calls to the enclosing extent. A real stdio session reported both inner calls as outgoing from outer, and the observer and occurrence corpora still pass.
1696 lines
79 KiB
C++
1696 lines
79 KiB
C++
#include <vls/semantic/EditorDiagnostics.hpp>
|
|
|
|
#include <vls/features/CallHierarchy.hpp>
|
|
#include <vls/features/CodeActions.hpp>
|
|
#include <vls/features/Completion.hpp>
|
|
#include <vls/features/DocumentSymbols.hpp>
|
|
#include <vls/features/IndexNavigation.hpp>
|
|
#include <vls/features/InlayHints.hpp>
|
|
#include <vls/features/SemanticTokens.hpp>
|
|
#include <vls/features/SignatureHelp.hpp>
|
|
#include <vls/features/TextStructure.hpp>
|
|
#include <vls/index/SymbolIndex.hpp>
|
|
#include <vls/modules/ArtifactCache.hpp>
|
|
#include <vls/modules/GccArtifacts.hpp>
|
|
#include <vls/modules/ModuleWorkQueue.hpp>
|
|
#include <vls/navigation/Navigation.hpp>
|
|
#include <vls/scheduler/Scheduler.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <deque>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <set>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
|
|
namespace vls::semantic {
|
|
namespace P = Void::LanguageServer::Protocol;
|
|
namespace B = Void::LanguageServer::Build;
|
|
namespace W = Void::LanguageServer::Workspace;
|
|
namespace fs = std::filesystem;
|
|
using namespace std::chrono_literals;
|
|
namespace {
|
|
using Json = P::Json;
|
|
using Clock = std::chrono::steady_clock;
|
|
constexpr std::size_t max_documents = 12;
|
|
|
|
Json position(const Documents::Position &p) {
|
|
return Json{Json::Object{{"line", Json{std::int64_t{p.line}}},
|
|
{"character", Json{std::int64_t{p.character}}}}};
|
|
}
|
|
|
|
Json range_json(const Documents::Range &range) {
|
|
return Json{Json::Object{{"start", position(range.start)}, {"end", position(range.end)}}};
|
|
}
|
|
|
|
index::Location location_of(const Documents::Range &range,
|
|
const Documents::DocumentSnapshot &snapshot) {
|
|
return {snapshot.identity.canonical_path.native(), range.start.line, range.start.character,
|
|
range.end.line, range.end.character};
|
|
}
|
|
|
|
bool under(const fs::path &root, const fs::path &path) {
|
|
return std::mismatch(root.begin(), root.end(), path.begin(), path.end()).first == root.end();
|
|
}
|
|
|
|
std::string refusal_text(navigation::Refusal refusal) {
|
|
switch (refusal) {
|
|
case navigation::Refusal::unknown_binding:
|
|
return "no symbol at the requested position";
|
|
case navigation::Refusal::ambiguous_binding:
|
|
return "the position resolves to more than one binding";
|
|
case navigation::Refusal::incomplete_coverage:
|
|
return "analysis coverage is incomplete";
|
|
case navigation::Refusal::stale_source:
|
|
return "the source changed after analysis";
|
|
case navigation::Refusal::non_editable_origin:
|
|
return "the binding has a non-editable origin";
|
|
case navigation::Refusal::generated_name:
|
|
return "the name is compiler-generated";
|
|
case navigation::Refusal::invalid_name:
|
|
return "the replacement is not a plain identifier";
|
|
case navigation::Refusal::unchanged_name:
|
|
return "the replacement equals the current spelling";
|
|
case navigation::Refusal::multi_file_without_transaction:
|
|
return "multi-file rename requires client transaction support";
|
|
case navigation::Refusal::none:
|
|
break;
|
|
}
|
|
return "navigation refused";
|
|
}
|
|
|
|
int symbol_kind(index::SymbolKind kind) {
|
|
switch (kind) {
|
|
case index::SymbolKind::function:
|
|
return 12; // LSP Function
|
|
case index::SymbolKind::type:
|
|
return 5; // LSP Class
|
|
case index::SymbolKind::variable:
|
|
return 13; // LSP Variable
|
|
case index::SymbolKind::field:
|
|
return 8; // LSP Field
|
|
case index::SymbolKind::namespace_:
|
|
return 3; // LSP Namespace
|
|
case index::SymbolKind::module:
|
|
return 2; // LSP Module
|
|
case index::SymbolKind::unknown:
|
|
break;
|
|
}
|
|
return 1; // LSP File fallback for an unclassified symbol
|
|
}
|
|
|
|
struct PublishedDiagnostic {
|
|
std::string path;
|
|
Documents::Range range;
|
|
DiagnosticSeverity severity;
|
|
std::string message, rule;
|
|
std::vector<DiagnosticFix> fixes;
|
|
};
|
|
|
|
struct EditorReport {
|
|
Json::Array diagnostics, other_messages;
|
|
std::vector<PublishedDiagnostic> published;
|
|
std::size_t unlocated{};
|
|
};
|
|
|
|
EditorReport for_owner(const DiagnosticBatch &batch) {
|
|
EditorReport report;
|
|
for (const auto &d : batch.diagnostics) {
|
|
if (!d.location.range || !d.location.snapshot ||
|
|
!d.location.snapshot->same_input_as(*batch.owner)) {
|
|
++report.unlocated;
|
|
if (report.other_messages.size() < 8)
|
|
report.other_messages.emplace_back(d.message.substr(0, 1024));
|
|
continue;
|
|
}
|
|
report.published.push_back({d.location.snapshot->identity.canonical_path.native(),
|
|
*d.location.range, d.severity, d.message, d.rule, d.fixes});
|
|
auto &r = *d.location.range;
|
|
Json::Object item{
|
|
{"range", Json{Json::Object{{"start", position(r.start)}, {"end", position(r.end)}}}},
|
|
{"severity", Json{static_cast<std::int64_t>(d.severity) + 1}},
|
|
{"source", Json{std::string{"gcc"}}},
|
|
{"message", Json{d.message}}};
|
|
if (!d.rule.empty())
|
|
item.emplace("code", Json{d.rule});
|
|
report.diagnostics.emplace_back(std::move(item));
|
|
}
|
|
return report;
|
|
}
|
|
|
|
std::expected<std::string, std::string>
|
|
notification(std::string_view uri, std::optional<std::int32_t> version, Json::Array diagnostics) {
|
|
Json::Object params{{"uri", Json{std::string{uri}}},
|
|
{"diagnostics", Json{std::move(diagnostics)}}};
|
|
if (version)
|
|
params.emplace("version", Json{std::int64_t{*version}});
|
|
return P::serialize_json(
|
|
Json{Json::Object{{"jsonrpc", Json{std::string{"2.0"}}},
|
|
{"method", Json{std::string{"textDocument/publishDiagnostics"}}},
|
|
{"params", Json{std::move(params)}}}});
|
|
}
|
|
|
|
struct Receipt {
|
|
SemanticContextLease lease;
|
|
SourceScope scope;
|
|
std::shared_ptr<const SourceCapture> capture;
|
|
};
|
|
|
|
struct Entry {
|
|
std::string uri, state{"queued"}, detail, provider, alternative;
|
|
std::optional<std::int32_t> version, published_version;
|
|
bool sent{}, had_findings{};
|
|
std::shared_ptr<const Receipt> receipt;
|
|
std::vector<PublishedDiagnostic> published;
|
|
std::shared_ptr<const std::vector<Occurrence>> occurrences;
|
|
std::string occurrence_state{"disabled"}, occurrence_reason;
|
|
std::string module_state{"disabled"}, module_detail;
|
|
std::string module_artifact_state{"disabled"}, module_artifact_detail,
|
|
module_artifact_generation;
|
|
std::vector<std::string> module_provides, module_imports;
|
|
std::vector<fs::path> hints;
|
|
B::Selection hint_selection;
|
|
std::size_t runs{}, unlocated{};
|
|
Json::Array other_messages;
|
|
std::optional<Clock::time_point> retry_at;
|
|
std::chrono::seconds retry_delay{1};
|
|
};
|
|
} // namespace
|
|
|
|
struct EditorDiagnostics::State {
|
|
fs::path root;
|
|
std::shared_ptr<const GccProfile> profile;
|
|
WorkspaceSession &workspace;
|
|
const Documents::DocumentStore &documents;
|
|
SourceCatalog catalog;
|
|
Send send;
|
|
std::mutex mutex;
|
|
std::condition_variable wake;
|
|
std::map<std::string, Entry> entries;
|
|
std::deque<std::string> pending;
|
|
// One foreground slot: the analysis worker is still the single owner of compiler
|
|
// process state, grants and the module cache. The scheduler supplies bounded
|
|
// admission, revision coalescing, cancellation, a running watchdog and metrics.
|
|
scheduler::Scheduler scheduler;
|
|
modules::ModuleWorkQueue module_work{scheduler};
|
|
index::SymbolIndex symbols;
|
|
bool updating{}, stopped{}, versions{}, transactional_rename{};
|
|
std::uint64_t epoch{1}, issued{};
|
|
Clock::time_point earliest{}, next_poll{Clock::now() + 1s};
|
|
// The single worker installs source grants and owns the process for each job.
|
|
std::optional<SourceScope> scope;
|
|
InstalledSourceGrant grant;
|
|
fs::path artifact_root;
|
|
std::shared_ptr<modules::ArtifactCache> artifacts;
|
|
std::jthread worker;
|
|
|
|
State(fs::path r, std::shared_ptr<const GccProfile> p, WorkspaceSession &w,
|
|
const Documents::DocumentStore &d, Send output)
|
|
: root(std::move(r)), profile(std::move(p)), workspace(w), documents(d), catalog(d),
|
|
send(std::move(output)), scheduler(scheduler::Budget{.foreground = 1,
|
|
.background = 1,
|
|
.artifact = 1,
|
|
.queued = 32,
|
|
.memory_mib = 4096,
|
|
.watchdog = 90s}),
|
|
worker([this] { loop(); }) {}
|
|
|
|
fs::path artifact_root_of() {
|
|
if (artifact_root.empty())
|
|
artifact_root =
|
|
fs::temp_directory_path() / ("vls-module-artifacts-" + std::to_string(::getpid()));
|
|
return artifact_root;
|
|
}
|
|
|
|
bool current(const std::string &key, std::uint64_t generation) const {
|
|
return !stopped && !updating && generation == epoch && entries.contains(key);
|
|
}
|
|
void retract(Entry &entry, bool closed = false) {
|
|
if (entry.sent && (entry.had_findings || closed)) {
|
|
auto body =
|
|
notification(entry.uri, versions ? entry.published_version : std::nullopt, {});
|
|
if (body)
|
|
send(std::move(*body));
|
|
}
|
|
entry.sent = false;
|
|
entry.had_findings = false;
|
|
}
|
|
void queue(const std::string &key, Entry &entry) {
|
|
entry.state = "queued";
|
|
entry.detail.clear();
|
|
entry.other_messages.clear();
|
|
entry.unlocated = entry.runs = 0;
|
|
entry.receipt.reset();
|
|
entry.occurrences.reset();
|
|
entry.occurrence_state = "disabled";
|
|
entry.occurrence_reason.clear();
|
|
entry.module_state = "disabled";
|
|
entry.module_detail.clear();
|
|
entry.module_provides.clear();
|
|
entry.module_imports.clear();
|
|
entry.module_artifact_state = "disabled";
|
|
entry.module_artifact_detail.clear();
|
|
entry.module_artifact_generation.clear();
|
|
entry.retry_at.reset();
|
|
symbols.remove(key);
|
|
pending.push_back(key);
|
|
}
|
|
void failed(const std::string &key, std::uint64_t generation, std::string reason,
|
|
bool refresh_context = false) {
|
|
std::lock_guard lock{mutex};
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &entry = entries.at(key);
|
|
retract(entry);
|
|
entry.receipt.reset();
|
|
entry.occurrences.reset();
|
|
entry.state = "unavailable";
|
|
entry.detail = std::move(reason);
|
|
if (refresh_context) {
|
|
entry.retry_at = Clock::now() + entry.retry_delay;
|
|
entry.retry_delay = std::min(entry.retry_delay * 2, 30s);
|
|
}
|
|
}
|
|
bool valid(const Receipt &record) const {
|
|
return workspace.validate_semantic_context(record.lease) &&
|
|
catalog.validate(record.scope, *record.capture);
|
|
}
|
|
void analyze(const std::string &key, std::uint64_t generation, std::uint64_t sequence,
|
|
std::stop_token cancel) {
|
|
auto lease = workspace.acquire_semantic_context(key, std::nullopt,
|
|
{profile->driver, W::CompilerDriver::gcc});
|
|
if (!lease)
|
|
return failed(key, generation, lease.error().message, true);
|
|
const auto &selection = lease->selection();
|
|
SourceScope job{selection->generation->identity().workspace,
|
|
lease->configuration_generation(), lease->driver().tool.trust_generation};
|
|
auto owner = documents.snapshot(key);
|
|
if (job.workspace.root != root || !owner || !selection->selected ||
|
|
owner->identity.canonical_path != selection->selected->key.owning_tu)
|
|
return failed(key, generation,
|
|
"The selected command belongs to another translation unit or workspace");
|
|
if (!scope || *scope != job) {
|
|
auto installed = catalog.install(job, std::array{root});
|
|
if (!installed)
|
|
return failed(key, generation, "Could not bind the workspace source directory");
|
|
scope = job;
|
|
grant = std::move(*installed);
|
|
}
|
|
std::vector<fs::path> warm;
|
|
{
|
|
std::lock_guard lock{mutex};
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &entry = entries.at(key);
|
|
if (entry.hint_selection == selection)
|
|
warm = entry.hints;
|
|
}
|
|
if (cancel.stop_requested() || !workspace.validate_semantic_context(*lease))
|
|
return failed(key, generation, "The compiler command was superseded", true);
|
|
auto result =
|
|
analyze_with_gcc(*profile, *lease, documents, catalog, grant, sequence, warm, cancel);
|
|
// Old lookup hints may include a file removed by the edit. Discover the current
|
|
// program from its TU when the old namespace cannot be materialized.
|
|
if (!result && !warm.empty() && !cancel.stop_requested() &&
|
|
workspace.validate_semantic_context(*lease))
|
|
result =
|
|
analyze_with_gcc(*profile, *lease, documents, catalog, grant, sequence, {}, cancel);
|
|
if (!result)
|
|
return failed(key, generation, result.error());
|
|
auto receipt = std::make_shared<Receipt>(Receipt{*lease, job, result->sources});
|
|
auto report = for_owner(result->batch);
|
|
const bool has_findings = !report.diagnostics.empty();
|
|
const bool publish =
|
|
has_findings || (result->batch.complete_for_owner && !report.unlocated);
|
|
// Bind the reply to this exact selection and diagnostics operation before delivery.
|
|
B::PublicationSlot slot{lease->selection_state(), B::Operation::diagnostics};
|
|
auto accepted = slot.replace_inputs(sequence, result->revision);
|
|
auto ticket = slot.issue();
|
|
if (!accepted || !ticket || !valid(*receipt) || !owner->same_input_as(*result->batch.owner))
|
|
return failed(key, generation, "The compiler inputs changed before publication", true);
|
|
std::unique_lock lock{mutex};
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &entry = entries.at(key);
|
|
if (publish) {
|
|
auto body = notification(entry.uri, versions ? owner->version : std::nullopt,
|
|
std::move(report.diagnostics));
|
|
if (!body || !slot.publish(*ticket, *body) || !send(std::move(*body))) {
|
|
entry.state = "unavailable";
|
|
entry.detail = "Could not publish the compiler diagnostics";
|
|
return;
|
|
}
|
|
entry.sent = true;
|
|
entry.had_findings = has_findings;
|
|
entry.published_version = owner->version;
|
|
entry.published = std::move(report.published);
|
|
} else {
|
|
retract(entry);
|
|
entry.published.clear();
|
|
}
|
|
entry.provider = selection->generation->identity().provider;
|
|
entry.alternative = selection->selected->key.alternative;
|
|
entry.hint_selection = selection;
|
|
entry.hints = std::move(result->lookups);
|
|
entry.runs = result->compiler_runs;
|
|
entry.unlocated = report.unlocated;
|
|
entry.other_messages = std::move(report.other_messages);
|
|
entry.receipt = std::move(receipt);
|
|
if (result->occurrences) {
|
|
entry.occurrences = std::make_shared<const std::vector<Occurrence>>(
|
|
std::move(result->occurrences->occurrences));
|
|
entry.occurrence_state = !result->occurrences->complete
|
|
? "incomplete"
|
|
: (result->occurrences->partial ? "partial" : "ready");
|
|
entry.occurrence_reason = result->occurrences->reason;
|
|
} else {
|
|
entry.occurrences.reset();
|
|
entry.occurrence_state = profile->observes() ? "unavailable" : "disabled";
|
|
entry.occurrence_reason = result->occurrence_detail;
|
|
}
|
|
entry.retry_delay = 1s;
|
|
index::ContextInput facts;
|
|
facts.context = key;
|
|
facts.complete =
|
|
result->occurrences && result->occurrences->complete && !result->occurrences->partial;
|
|
facts.reason =
|
|
result->occurrences ? result->occurrences->reason : result->occurrence_detail;
|
|
for (const auto &source : result->sources->entries())
|
|
if (source.snapshot) {
|
|
facts.sources.emplace_back(source.logical_path.native(),
|
|
source.snapshot->content_hash.sha256_hex);
|
|
facts.dependencies.push_back(source.logical_path.native());
|
|
}
|
|
if (entry.occurrences)
|
|
for (const auto &occurrence : *entry.occurrences) {
|
|
index::SymbolRecord record;
|
|
record.name = occurrence.name;
|
|
record.kind = index::SymbolKind::function;
|
|
record.location = location_of(occurrence.use, *occurrence.use_file);
|
|
if (occurrence.has_definition())
|
|
record.definition =
|
|
location_of(occurrence.definition, *occurrence.definition_file);
|
|
record.provenance = "stock-observer";
|
|
facts.records.push_back(std::move(record));
|
|
}
|
|
symbols.publish(facts);
|
|
// The index is current before the status advertises readiness.
|
|
entry.state = result->batch.complete_for_owner && !report.unlocated ? "ready" : "partial";
|
|
entry.module_state = "running";
|
|
entry.module_detail.clear();
|
|
entry.module_provides.clear();
|
|
entry.module_imports.clear();
|
|
entry.module_artifact_state = "disabled";
|
|
entry.module_artifact_detail.clear();
|
|
entry.module_artifact_generation.clear();
|
|
std::vector<fs::path> scan_hints = entry.hints;
|
|
std::vector<std::string> artifact_arguments;
|
|
const auto working_directory = *selection->selected->working_directory;
|
|
const auto &translation_unit = selection->selected->key.owning_tu;
|
|
if (selection->selected->semantic_arguments &&
|
|
!selection->selected->semantic_arguments->empty()) {
|
|
const auto &original = *selection->selected->semantic_arguments;
|
|
artifact_arguments.push_back(original.front());
|
|
for (std::size_t i = 1; i < original.size(); ++i) {
|
|
const auto &argument = original[i];
|
|
if (argument == "-c" || argument == translation_unit.native())
|
|
continue;
|
|
if (argument == "-o") {
|
|
++i;
|
|
continue;
|
|
}
|
|
artifact_arguments.push_back(argument);
|
|
}
|
|
artifact_arguments.insert(
|
|
artifact_arguments.end(),
|
|
{"-fmodules-ts", "-c", translation_unit.native(), "-o", "module.o"});
|
|
}
|
|
lock.unlock();
|
|
auto product = std::make_shared<StockScan>();
|
|
const auto scan_lease = *lease;
|
|
const auto scan_grant = grant;
|
|
const auto scan_profile = profile;
|
|
const bool admitted = module_work.submit(
|
|
key, modules::ModuleWorkKind::scan, sequence,
|
|
[this, scan_lease, scan_grant, scan_profile, product,
|
|
scan_hints = std::move(scan_hints)](std::stop_token token) {
|
|
auto scanned = scan_modules_with_gcc(*scan_profile, scan_lease, documents, catalog,
|
|
scan_grant, scan_hints, token);
|
|
if (!scanned)
|
|
throw std::runtime_error(scanned.error());
|
|
*product = std::move(*scanned);
|
|
return std::string{};
|
|
},
|
|
[this, key, generation, product, artifact_arguments,
|
|
working_directory](modules::ModuleTaskResult task) {
|
|
{
|
|
std::lock_guard guard{mutex};
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &finished = entries.at(key);
|
|
finished.module_state =
|
|
task.outcome == scheduler::Outcome::completed
|
|
? (product->complete ? "ready" : "partial")
|
|
: (task.outcome == scheduler::Outcome::cancelled ? "cancelled"
|
|
: "failed");
|
|
finished.module_detail = task.detail;
|
|
finished.module_provides = product->provides;
|
|
finished.module_imports = product->imports;
|
|
}
|
|
if (task.outcome != scheduler::Outcome::completed || product->provides.empty() ||
|
|
artifact_arguments.empty())
|
|
return;
|
|
std::shared_ptr<modules::ArtifactCache> cache;
|
|
fs::path artifact_root;
|
|
{
|
|
std::lock_guard guard{mutex};
|
|
if (!artifacts)
|
|
artifacts = std::make_shared<modules::ArtifactCache>(artifact_root_of());
|
|
cache = artifacts;
|
|
artifact_root = this->artifact_root;
|
|
}
|
|
const auto generation_id = std::make_shared<std::string>();
|
|
module_work.submit(
|
|
key, modules::ModuleWorkKind::artifact, task.revision,
|
|
[cache, artifact_arguments, working_directory, generation_id, product,
|
|
artifact_root](std::stop_token) {
|
|
modules::ModulePlan plan;
|
|
plan.domain.compatibility = "stock-gcc-16.2.0";
|
|
plan.claims_complete = true;
|
|
for (const auto &name : product->provides) {
|
|
B::ContextKey context{{},
|
|
{},
|
|
B::ContextRole::target,
|
|
name,
|
|
working_directory / (name + ".cpp")};
|
|
modules::UnitInputs inputs{"artifact-" + name, name, name, {}};
|
|
plan.units.push_back({{context,
|
|
{working_directory, name + ".o"},
|
|
inputs,
|
|
"editor-scan",
|
|
{}},
|
|
modules::UnitKind::primary_interface,
|
|
{}});
|
|
plan.order.push_back(context);
|
|
}
|
|
modules::ModuleCommand command{working_directory, artifact_arguments};
|
|
auto produced = modules::produce_module_artifacts(
|
|
plan, *cache, artifact_root / "staging",
|
|
modules::gcc_module_artifact_work(
|
|
[command](const B::ContextKey &)
|
|
-> std::optional<modules::ModuleCommand> { return command; }));
|
|
if (!produced)
|
|
throw std::runtime_error(produced.error());
|
|
*generation_id = (*produced)->id();
|
|
return *generation_id;
|
|
},
|
|
[this, key, generation, generation_id](modules::ModuleTaskResult task) {
|
|
std::lock_guard guard{mutex};
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &finished = entries.at(key);
|
|
finished.module_artifact_state =
|
|
task.outcome == scheduler::Outcome::completed
|
|
? "ready"
|
|
: (task.outcome == scheduler::Outcome::cancelled ? "cancelled"
|
|
: "failed");
|
|
finished.module_artifact_detail = task.detail;
|
|
finished.module_artifact_generation = *generation_id;
|
|
});
|
|
std::lock_guard guard{mutex};
|
|
if (current(key, generation))
|
|
entries.at(key).module_artifact_state = "running";
|
|
});
|
|
if (!admitted) {
|
|
std::lock_guard guard{mutex};
|
|
if (current(key, generation)) {
|
|
auto &refused = entries.at(key);
|
|
refused.module_state = "unavailable";
|
|
refused.module_detail = "module scan admission was refused";
|
|
}
|
|
}
|
|
}
|
|
|
|
void poll(std::unique_lock<std::mutex> &lock) {
|
|
const auto generation = epoch;
|
|
std::vector<std::pair<std::string, std::shared_ptr<const Receipt>>> saved;
|
|
for (auto &[key, entry] : entries)
|
|
if (entry.receipt)
|
|
saved.emplace_back(key, entry.receipt);
|
|
else if (entry.retry_at && Clock::now() >= *entry.retry_at)
|
|
queue(key, entry);
|
|
for (auto &[key, receipt] : saved) {
|
|
lock.unlock();
|
|
const bool still_valid = valid(*receipt);
|
|
lock.lock();
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &entry = entries.at(key);
|
|
if (!still_valid && entry.receipt == receipt) {
|
|
retract(entry);
|
|
queue(key, entry);
|
|
}
|
|
}
|
|
}
|
|
navigation::FactSet fact_set_locked() const {
|
|
navigation::FactSet set;
|
|
set.backend = "stock GCC 16.2.0";
|
|
set.generation = symbols.generation();
|
|
for (const auto &[key, entry] : entries) {
|
|
set.contexts.push_back(
|
|
{key, entry.occurrence_state == "ready", entry.occurrence_reason});
|
|
if (entry.occurrences)
|
|
for (const auto &occurrence : *entry.occurrences) {
|
|
navigation::Fact fact;
|
|
fact.binding.name = occurrence.name;
|
|
if (occurrence.has_definition())
|
|
fact.binding.definition = navigation::Location{
|
|
occurrence.definition_file->identity.canonical_path.native(),
|
|
{occurrence.definition.start.line,
|
|
occurrence.definition.start.character, occurrence.definition.end.line,
|
|
occurrence.definition.end.character}};
|
|
fact.context = key;
|
|
fact.source = occurrence.use_file->identity.canonical_path.native();
|
|
fact.content = occurrence.use_file->content_hash.sha256_hex;
|
|
fact.use = {occurrence.use.start.line, occurrence.use.start.character,
|
|
occurrence.use.end.line, occurrence.use.end.character};
|
|
fact.editable = under(root, fact.source);
|
|
fact.provenance = "stock-observer";
|
|
set.facts.push_back(std::move(fact));
|
|
}
|
|
if (entry.receipt)
|
|
for (const auto &source : entry.receipt->capture->entries())
|
|
if (source.snapshot)
|
|
set.current_sources[source.logical_path.native()] =
|
|
source.snapshot->content_hash.sha256_hex;
|
|
}
|
|
// Index-backed coverage: persisted contexts and overlays join the set.
|
|
// Facts whose source content is not currently known stay non-editable,
|
|
// so a rename can never edit an unverified file.
|
|
if (const auto generation = symbols.current()) {
|
|
std::vector<features::IndexedSource> sources;
|
|
sources.reserve(set.current_sources.size());
|
|
for (const auto &[path, content] : set.current_sources)
|
|
sources.push_back({path, content, under(root, path)});
|
|
features::ingest_index_facts(set, *generation, sources);
|
|
}
|
|
return set;
|
|
}
|
|
|
|
void loop() {
|
|
std::unique_lock lock{mutex};
|
|
while (!stopped) {
|
|
if (updating) {
|
|
wake.wait(lock, [&] { return stopped || !updating; });
|
|
continue;
|
|
}
|
|
if (pending.empty()) {
|
|
if (Clock::now() < next_poll) {
|
|
wake.wait_until(lock, next_poll);
|
|
continue;
|
|
}
|
|
next_poll = Clock::now() + 1s;
|
|
poll(lock);
|
|
continue;
|
|
}
|
|
if (Clock::now() < earliest) {
|
|
wake.wait_until(lock, earliest);
|
|
continue;
|
|
}
|
|
auto key = std::move(pending.front());
|
|
pending.pop_front();
|
|
const auto generation = epoch, sequence = ++issued;
|
|
entries.at(key).state = "running";
|
|
lock.unlock();
|
|
const bool accepted = scheduler.submit(
|
|
{.priority = scheduler::Priority::foreground,
|
|
.key = key,
|
|
.revision = sequence,
|
|
.memory_mib = 1},
|
|
[this, key, generation, sequence](std::stop_token cancel) {
|
|
try {
|
|
analyze(key, generation, sequence, cancel);
|
|
} catch (const std::exception &) {
|
|
failed(key, generation, "The compiler job could not be completed");
|
|
}
|
|
return std::string{};
|
|
},
|
|
[this, key, generation](scheduler::Result result) {
|
|
if (result.outcome != scheduler::Outcome::timed_out)
|
|
return;
|
|
std::lock_guard guard{mutex};
|
|
if (!current(key, generation))
|
|
return;
|
|
auto &entry = entries.at(key);
|
|
retract(entry);
|
|
entry.receipt.reset();
|
|
entry.occurrences.reset();
|
|
entry.state = "unavailable";
|
|
entry.detail = "The compiler job exceeded its deadline";
|
|
entry.retry_at = Clock::now() + entry.retry_delay;
|
|
entry.retry_delay = std::min(entry.retry_delay * 2, 30s);
|
|
});
|
|
lock.lock();
|
|
if (!accepted && current(key, generation)) {
|
|
auto &entry = entries.at(key);
|
|
entry.state = "unavailable";
|
|
entry.detail = "The analysis queue is full";
|
|
entry.retry_at = Clock::now() + entry.retry_delay;
|
|
entry.retry_delay = std::min(entry.retry_delay * 2, 30s);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
EditorDiagnostics::EditorDiagnostics(const fs::path &root,
|
|
std::shared_ptr<const GccProfile> profile,
|
|
WorkspaceSession &workspace,
|
|
const Documents::DocumentStore &documents, Send send)
|
|
: state_(std::make_unique<State>(root, std::move(profile), workspace, documents,
|
|
std::move(send))) {}
|
|
EditorDiagnostics::~EditorDiagnostics() { stop(); }
|
|
void EditorDiagnostics::set_version_support(bool enabled) {
|
|
std::lock_guard lock{state_->mutex};
|
|
state_->versions = enabled;
|
|
}
|
|
void EditorDiagnostics::begin_update() {
|
|
auto &s = *state_;
|
|
std::vector<std::string> active;
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
++s.epoch;
|
|
s.updating = true;
|
|
s.pending.clear();
|
|
active.reserve(s.entries.size());
|
|
for (const auto &[key, entry] : s.entries)
|
|
active.push_back(key);
|
|
}
|
|
// Cancellation may invoke a terminal completion synchronously, so it runs
|
|
// without the state lock. A queued job is cancelled immediately; a running
|
|
// job receives a cooperative stop request.
|
|
for (const auto &key : active) {
|
|
s.scheduler.cancel(key);
|
|
s.module_work.cancel(key);
|
|
}
|
|
}
|
|
void EditorDiagnostics::files_changed(std::span<const features::file_operations::Change> changes) {
|
|
auto &s = *state_;
|
|
std::lock_guard lock{s.mutex};
|
|
for (const auto &change : changes) {
|
|
if (change.gone)
|
|
s.symbols.clear_overlay(change.path);
|
|
// The new content is unknown, so the path is treated as changed: every
|
|
// context that consumed it is marked incomplete, never silently kept.
|
|
s.symbols.invalidate(change.path, std::nullopt);
|
|
}
|
|
}
|
|
|
|
Json EditorDiagnostics::renamed_files(std::span<const RenamedFile> renames) const {
|
|
auto &s = *state_;
|
|
Json::Object changes;
|
|
for (const auto &overlay : s.documents.list_overlays()) {
|
|
if (!overlay || !overlay->identity.is_file_uri)
|
|
continue;
|
|
for (const auto &rename : renames) {
|
|
const auto edits = features::include_update::include_edits(
|
|
overlay->text, overlay->identity.canonical_path.native(), rename.old_path,
|
|
rename.new_path);
|
|
if (edits.empty())
|
|
continue;
|
|
auto &list = changes[overlay->identity.display_uri];
|
|
if (!std::holds_alternative<Json::Array>(list.value))
|
|
list = Json{Json::Array{}};
|
|
auto &array = std::get<Json::Array>(list.value);
|
|
for (const auto &edit : edits) {
|
|
const auto start = overlay->offset_to_position(edit.start);
|
|
const auto end = overlay->offset_to_position(edit.end);
|
|
if (!start || !end)
|
|
continue;
|
|
array.push_back(
|
|
Json{Json::Object{{"range", range_json(Documents::Range{*start, *end})},
|
|
{"newText", Json{edit.replacement}}}});
|
|
}
|
|
}
|
|
}
|
|
if (changes.empty())
|
|
return Json{nullptr};
|
|
return Json{Json::Object{{"changes", Json{std::move(changes)}}}};
|
|
}
|
|
|
|
void EditorDiagnostics::finish_update(bool workspace_changed) {
|
|
auto &s = *state_;
|
|
auto overlays = s.documents.list_overlays();
|
|
std::lock_guard lock{s.mutex};
|
|
if (s.stopped)
|
|
return;
|
|
std::set<std::string> present;
|
|
for (const auto &overlay : overlays) {
|
|
const auto &id = overlay->identity;
|
|
if (!id.is_file_uri || !overlay->version ||
|
|
std::mismatch(s.root.begin(), s.root.end(), id.canonical_path.begin(),
|
|
id.canonical_path.end())
|
|
.first != s.root.end())
|
|
continue;
|
|
present.insert(id.canonical_uri);
|
|
}
|
|
for (auto i = s.entries.begin(); i != s.entries.end();) {
|
|
if (present.contains(i->first))
|
|
++i;
|
|
else {
|
|
s.retract(i->second, true);
|
|
s.symbols.remove(i->first);
|
|
s.module_work.cancel(i->first);
|
|
i = s.entries.erase(i);
|
|
}
|
|
}
|
|
for (const auto &overlay : overlays) {
|
|
const auto &id = overlay->identity;
|
|
if (!present.contains(id.canonical_uri) ||
|
|
(s.entries.size() >= max_documents && !s.entries.contains(id.canonical_uri)))
|
|
continue;
|
|
auto &entry = s.entries[id.canonical_uri];
|
|
entry.uri = id.display_uri;
|
|
entry.version = overlay->version;
|
|
entry.retry_delay = 1s;
|
|
if (workspace_changed) {
|
|
s.retract(entry);
|
|
entry.hints.clear();
|
|
entry.hint_selection.reset();
|
|
}
|
|
s.queue(id.canonical_uri, entry);
|
|
}
|
|
s.earliest = Clock::now() + 75ms;
|
|
s.updating = false;
|
|
s.wake.notify_one();
|
|
}
|
|
void EditorDiagnostics::stop() {
|
|
auto &s = *state_;
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
s.stopped = true;
|
|
s.wake.notify_one();
|
|
}
|
|
s.scheduler.cancel_all();
|
|
s.module_work.cancel_all();
|
|
if (s.worker.joinable())
|
|
s.worker.join();
|
|
}
|
|
Json EditorDiagnostics::status(std::string_view uri) const {
|
|
auto &s = *state_;
|
|
auto key = Documents::client_uri_key(uri);
|
|
std::lock_guard lock{s.mutex};
|
|
const auto i = key ? s.entries.find(*key) : s.entries.end();
|
|
if (i == s.entries.end())
|
|
return Json{Json::Object{
|
|
{"state", Json{std::string{"unavailable"}}},
|
|
{"detail",
|
|
Json{std::string{"No synchronized document is scheduled for this workspace"}}}}};
|
|
const auto &entry = i->second;
|
|
Json::Object result{{"uri", Json{entry.uri}},
|
|
{"state", Json{entry.state}},
|
|
{"backend", Json{std::string{"GCC 16.2.0"}}},
|
|
{"detail", Json{entry.detail}},
|
|
{"compilerRuns", Json{static_cast<std::int64_t>(entry.runs)}},
|
|
{"unlocatedCount", Json{static_cast<std::int64_t>(entry.unlocated)}},
|
|
{"messages", Json{entry.other_messages}}};
|
|
if (entry.version)
|
|
result.emplace("version", Json{std::int64_t{*entry.version}});
|
|
if (entry.receipt) {
|
|
result.emplace("provider", Json{entry.provider});
|
|
result.emplace("alternative", Json{entry.alternative});
|
|
result.emplace("workspaceUri", Json{Documents::path_to_file_uri(s.root)});
|
|
result.emplace("projectCoverage", Json{std::string{"selectedTranslationUnit"}});
|
|
Json::Array provides, imports;
|
|
for (const auto &name : entry.module_provides)
|
|
provides.push_back(Json{name});
|
|
for (const auto &name : entry.module_imports)
|
|
imports.push_back(Json{name});
|
|
result.emplace("moduleArtifact",
|
|
Json{Json::Object{{"state", Json{entry.module_artifact_state}},
|
|
{"detail", Json{entry.module_artifact_detail}},
|
|
{"generation", Json{entry.module_artifact_generation}}}});
|
|
result.emplace("moduleScan", Json{Json::Object{{"state", Json{entry.module_state}},
|
|
{"detail", Json{entry.module_detail}},
|
|
{"provides", Json{std::move(provides)}},
|
|
{"imports", Json{std::move(imports)}}}});
|
|
result.emplace(
|
|
"occurrences",
|
|
Json{Json::Object{{"state", Json{entry.occurrence_state}},
|
|
{"count", Json{static_cast<std::int64_t>(
|
|
entry.occurrences ? entry.occurrences->size() : 0)}},
|
|
{"reason", Json{entry.occurrence_reason}}}});
|
|
}
|
|
const auto coverage = s.symbols.coverage();
|
|
result.emplace(
|
|
"index",
|
|
Json{Json::Object{{"generation", Json{static_cast<std::int64_t>(s.symbols.generation())}},
|
|
{"records", Json{static_cast<std::int64_t>(s.symbols.records())}},
|
|
{"indexed", Json{coverage.indexed}},
|
|
{"complete", Json{coverage.complete}}}});
|
|
const auto metrics = s.scheduler.metrics();
|
|
result.emplace(
|
|
"scheduler",
|
|
Json{Json::Object{
|
|
{"accepted", Json{static_cast<std::int64_t>(metrics.accepted)}},
|
|
{"completed", Json{static_cast<std::int64_t>(metrics.completed)}},
|
|
{"failed", Json{static_cast<std::int64_t>(metrics.failed)}},
|
|
{"cancelled", Json{static_cast<std::int64_t>(metrics.cancelled)}},
|
|
{"timedOut", Json{static_cast<std::int64_t>(metrics.timed_out)}},
|
|
{"queuedPeak", Json{static_cast<std::int64_t>(metrics.queued_peak)}},
|
|
{"runningPeak", Json{static_cast<std::int64_t>(metrics.running_peak)}},
|
|
{"queueMs", Json{static_cast<std::int64_t>(metrics.queue_nanoseconds / 1'000'000)}},
|
|
{"runMs", Json{static_cast<std::int64_t>(metrics.run_nanoseconds / 1'000'000)}}}});
|
|
return Json{std::move(result)};
|
|
}
|
|
|
|
namespace {
|
|
const Occurrence *covering(const Receipt &receipt, const std::vector<Occurrence> &facts,
|
|
const std::string &key, Documents::Position position) {
|
|
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 nullptr;
|
|
const auto offset = snapshot->position_to_offset(position);
|
|
if (!offset)
|
|
return nullptr;
|
|
for (const auto &fact : facts) {
|
|
if (fact.use_file.get() != snapshot)
|
|
continue;
|
|
const auto range = snapshot->range_to_offsets(fact.use);
|
|
if (range && *offset >= range->first && *offset <= range->second)
|
|
return &fact;
|
|
}
|
|
return nullptr;
|
|
}
|
|
} // namespace
|
|
|
|
bool EditorDiagnostics::supports_occurrences() const noexcept {
|
|
return state_->profile->observes();
|
|
}
|
|
|
|
Json EditorDiagnostics::workspace_symbols(std::string_view query) const {
|
|
index::Query selected;
|
|
selected.name = std::string{query.substr(0, 256)};
|
|
selected.substring = true;
|
|
selected.limit = 100;
|
|
const auto result = state_->symbols.find(selected);
|
|
Json::Array symbols;
|
|
for (const auto &record : result.symbols) {
|
|
const auto &location = record.definition ? *record.definition : record.location;
|
|
symbols.push_back(Json{Json::Object{
|
|
{"name", Json{record.name}},
|
|
{"kind", Json{static_cast<std::int64_t>(symbol_kind(record.kind))}},
|
|
{"location", Json{Json::Object{
|
|
{"uri", Json{Documents::path_to_file_uri(location.path)}},
|
|
{"range", Json{Json::Object{
|
|
{"start", position({location.line, location.character})},
|
|
{"end", position({location.endLine,
|
|
location.endCharacter})}}}}}}}}});
|
|
}
|
|
return Json{std::move(symbols)};
|
|
}
|
|
|
|
Json EditorDiagnostics::completion(std::string_view uri, Documents::Position position) const {
|
|
auto &s = *state_;
|
|
const auto snapshot = s.documents.snapshot(uri);
|
|
if (!snapshot)
|
|
return Json{nullptr};
|
|
const auto offset = snapshot->position_to_offset(position);
|
|
if (!offset)
|
|
return Json{nullptr};
|
|
const auto text = std::string_view{snapshot->text};
|
|
const auto prefix = features::completion::prefix_at(text, *offset);
|
|
index::Query query;
|
|
query.name = std::string{prefix};
|
|
query.substring = true;
|
|
query.limit = 200;
|
|
std::vector<features::completion::Candidate> candidates;
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
const auto result = s.symbols.find(query);
|
|
for (const auto &record : result.symbols) {
|
|
const auto kind =
|
|
record.kind == index::SymbolKind::function ? features::completion::Kind::function
|
|
: record.kind == index::SymbolKind::type ? features::completion::Kind::type
|
|
: record.kind == index::SymbolKind::variable ? features::completion::Kind::variable
|
|
: record.kind == index::SymbolKind::field ? features::completion::Kind::field
|
|
: record.kind == index::SymbolKind::namespace_
|
|
? features::completion::Kind::namespace_
|
|
: record.kind == index::SymbolKind::module ? features::completion::Kind::module
|
|
: features::completion::Kind::unknown;
|
|
candidates.push_back({record.name,
|
|
kind,
|
|
record.location.path,
|
|
record.provenance,
|
|
{record.name, record.location.path, record.location.line,
|
|
record.location.character}});
|
|
}
|
|
}
|
|
const std::uint64_t generation = s.symbols.generation();
|
|
const auto completed = features::completion::complete(candidates, text, *offset);
|
|
Json::Array items;
|
|
for (const auto &item : completed.items) {
|
|
items.push_back(Json{Json::Object{
|
|
{"label", Json{item.label}},
|
|
{"kind", Json{static_cast<std::int64_t>(item.kind)}},
|
|
{"detail", Json{item.detail}},
|
|
{"sortText", Json{item.sort_text}},
|
|
{"data", Json{Json::Object{
|
|
{"generation", Json{static_cast<std::int64_t>(generation)}},
|
|
{"name", Json{item.identity.name}},
|
|
{"path", Json{item.identity.path}},
|
|
{"line", Json{static_cast<std::int64_t>(item.identity.line)}},
|
|
{"character", Json{static_cast<std::int64_t>(item.identity.character)}}}}},
|
|
{"textEdit",
|
|
Json{Json::Object{
|
|
{"range", range_json(Documents::Range{
|
|
{position.line, static_cast<std::uint32_t>(item.replacement_start)},
|
|
{position.line, static_cast<std::uint32_t>(item.replacement_end)}})},
|
|
{"newText", Json{item.label}}}}}}});
|
|
}
|
|
return Json{Json::Object{{"isIncomplete", Json{completed.incomplete}},
|
|
{"items", Json{std::move(items)}}}};
|
|
}
|
|
|
|
Json EditorDiagnostics::completion_resolve(const Json &item) const {
|
|
auto &s = *state_;
|
|
const auto *fields = P::as_object(&item);
|
|
if (!fields)
|
|
return item;
|
|
const auto *data = P::as_object(P::find_member(*fields, "data"));
|
|
if (!data)
|
|
return item;
|
|
const auto *name = P::as_string(P::find_member(*data, "name"));
|
|
const auto *path = P::as_string(P::find_member(*data, "path"));
|
|
const auto generation = P::as_int64(P::find_member(*data, "generation"));
|
|
const auto line = P::as_int64(P::find_member(*data, "line"));
|
|
const auto character = P::as_int64(P::find_member(*data, "character"));
|
|
if (!name || !path || !generation || !line || !character || *line < 0 || *character < 0)
|
|
return item;
|
|
features::completion::Identity identity{*name, *path, static_cast<std::uint32_t>(*line),
|
|
static_cast<std::uint32_t>(*character)};
|
|
std::vector<features::completion::Detail> current;
|
|
std::uint64_t now{};
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
now = s.symbols.generation();
|
|
index::Query query;
|
|
query.name = *name;
|
|
query.limit = 64;
|
|
const auto result = s.symbols.find(query);
|
|
for (const auto &record : result.symbols) {
|
|
const auto kind =
|
|
record.kind == index::SymbolKind::function ? features::completion::Kind::function
|
|
: record.kind == index::SymbolKind::type ? features::completion::Kind::type
|
|
: record.kind == index::SymbolKind::variable ? features::completion::Kind::variable
|
|
: record.kind == index::SymbolKind::field ? features::completion::Kind::field
|
|
: record.kind == index::SymbolKind::namespace_
|
|
? features::completion::Kind::namespace_
|
|
: record.kind == index::SymbolKind::module ? features::completion::Kind::module
|
|
: features::completion::Kind::unknown;
|
|
features::completion::Detail detail;
|
|
detail.identity = {record.name, record.location.path, record.location.line,
|
|
record.location.character};
|
|
detail.kind = kind;
|
|
detail.detail = record.location.path;
|
|
detail.provenance = record.provenance;
|
|
current.push_back(std::move(detail));
|
|
}
|
|
}
|
|
const auto resolved = features::completion::resolve(
|
|
identity, static_cast<std::uint64_t>(*generation), now, current);
|
|
if (resolved.status != features::completion::Resolved::Status::resolved)
|
|
return item;
|
|
Json::Object enriched = *fields;
|
|
enriched.insert_or_assign("detail", Json{resolved.detail});
|
|
Json::Object documentation;
|
|
documentation.emplace("kind", Json{std::string{"markdown"}});
|
|
documentation.emplace("value", Json{resolved.documentation});
|
|
enriched.insert_or_assign("documentation", Json{std::move(documentation)});
|
|
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)};
|
|
}
|
|
|
|
Json EditorDiagnostics::selection_ranges(std::string_view uri,
|
|
std::span<const Documents::Position> positions) const {
|
|
const auto snapshot = state_->documents.snapshot(uri);
|
|
if (!snapshot)
|
|
return Json{nullptr};
|
|
Json::Array result;
|
|
for (const auto &position : positions) {
|
|
const auto chain = features::structure::selection_chain(
|
|
snapshot->text, {position.line, position.character});
|
|
Json nested;
|
|
for (auto entry = chain.rbegin(); entry != chain.rend(); ++entry) {
|
|
Json::Object node{
|
|
{"range", range_json(Documents::Range{{entry->start.line, entry->start.character},
|
|
{entry->end.line, entry->end.character}})}};
|
|
if (!std::holds_alternative<std::nullptr_t>(nested.value))
|
|
node.emplace("parent", nested);
|
|
nested = Json{std::move(node)};
|
|
}
|
|
result.push_back(nested);
|
|
}
|
|
return Json{std::move(result)};
|
|
}
|
|
|
|
Json EditorDiagnostics::folding_ranges(std::string_view uri) const {
|
|
const auto snapshot = state_->documents.snapshot(uri);
|
|
if (!snapshot)
|
|
return Json{nullptr};
|
|
Json::Array result;
|
|
for (const auto &fold : features::structure::folding_ranges(snapshot->text)) {
|
|
Json::Object entry{{"startLine", Json{static_cast<std::int64_t>(fold.start_line)}},
|
|
{"endLine", Json{static_cast<std::int64_t>(fold.end_line)}}};
|
|
if (!fold.kind.empty())
|
|
entry.emplace("kind", Json{std::string{fold.kind}});
|
|
result.push_back(Json{std::move(entry)});
|
|
}
|
|
return Json{std::move(result)};
|
|
}
|
|
|
|
Json EditorDiagnostics::code_actions(std::string_view uri, Documents::Range range) const {
|
|
auto &s = *state_;
|
|
const auto identity = Documents::normalize_uri(uri);
|
|
if (!identity || !identity->is_file_uri)
|
|
return Json{nullptr};
|
|
std::vector<PublishedDiagnostic> published;
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
const auto i = s.entries.find(identity->canonical_uri);
|
|
if (i == s.entries.end())
|
|
return Json{Json::Array{}};
|
|
published = i->second.published;
|
|
}
|
|
std::vector<features::code_actions::Fix> fixes;
|
|
for (const auto &diagnostic : published) {
|
|
if (diagnostic.path != identity->canonical_path.native())
|
|
continue;
|
|
for (const auto &fix : diagnostic.fixes) {
|
|
features::code_actions::Fix entry;
|
|
entry.title =
|
|
fix.description.empty() ? diagnostic.message.substr(0, 256) : fix.description;
|
|
entry.path = diagnostic.path;
|
|
for (const auto &edit : fix.edits)
|
|
entry.edits.push_back({{{edit.range.start.line, edit.range.start.character},
|
|
{edit.range.end.line, edit.range.end.character}},
|
|
edit.replacement});
|
|
fixes.push_back(std::move(entry));
|
|
}
|
|
}
|
|
const auto selected = features::code_actions::actions(
|
|
fixes, {{range.start.line, range.start.character}, {range.end.line, range.end.character}});
|
|
Json::Array actions;
|
|
for (const auto &action : selected) {
|
|
Json::Array edits;
|
|
for (const auto &edit : action.edits)
|
|
edits.push_back(Json{Json::Object{
|
|
{"range",
|
|
range_json(Documents::Range{{edit.range.start.line, edit.range.start.character},
|
|
{edit.range.end.line, edit.range.end.character}})},
|
|
{"newText", Json{edit.replacement}}}});
|
|
Json::Object changes;
|
|
changes.emplace(Documents::path_to_file_uri(action.path), Json{std::move(edits)});
|
|
actions.push_back(Json{
|
|
Json::Object{{"title", Json{action.title}},
|
|
{"kind", Json{std::string{"quickfix"}}},
|
|
{"isPreferred", Json{true}},
|
|
{"edit", Json{Json::Object{{"changes", Json{std::move(changes)}}}}}}});
|
|
}
|
|
return Json{std::move(actions)};
|
|
}
|
|
|
|
Json EditorDiagnostics::semantic_tokens(std::string_view uri) 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{Json::Object{{"data", Json{Json::Array{}}}}};
|
|
receipt = i->second.receipt;
|
|
facts = i->second.occurrences;
|
|
}
|
|
if (!receipt || !facts || !s.valid(*receipt))
|
|
return Json{Json::Object{{"data", Json{Json::Array{}}}}};
|
|
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{Json::Object{{"data", Json{Json::Array{}}}}};
|
|
std::vector<features::semantic_tokens::Token> tokens;
|
|
for (const auto &occurrence : *facts) {
|
|
if (occurrence.use_file.get() != snapshot ||
|
|
occurrence.use.start.line != occurrence.use.end.line ||
|
|
occurrence.use.end.character <= occurrence.use.start.character)
|
|
continue;
|
|
tokens.push_back({occurrence.use.start.line, occurrence.use.start.character,
|
|
occurrence.use.end.character - occurrence.use.start.character});
|
|
}
|
|
const auto normalized = features::semantic_tokens::normalize(tokens);
|
|
Json::Array data;
|
|
for (const auto value : features::semantic_tokens::encode(normalized))
|
|
data.push_back(Json{static_cast<std::int64_t>(value)});
|
|
return Json{Json::Object{{"data", Json{std::move(data)}}}};
|
|
}
|
|
|
|
Json EditorDiagnostics::inlay_hints(std::string_view uri, Documents::Range range) 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{Json::Array{}};
|
|
receipt = i->second.receipt;
|
|
facts = i->second.occurrences;
|
|
}
|
|
if (!receipt || !facts || !s.valid(*receipt))
|
|
return Json{Json::Array{}};
|
|
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{Json::Array{}};
|
|
std::vector<features::inlay_hints::Hint> hints;
|
|
const features::inlay_hints::Range requested{{range.start.line, range.start.character},
|
|
{range.end.line, range.end.character}};
|
|
for (const auto &occurrence : *facts) {
|
|
if (occurrence.use_file.get() != snapshot || occurrence.signature.empty())
|
|
continue;
|
|
const auto use = snapshot->range_to_offsets(occurrence.use);
|
|
if (!use)
|
|
continue;
|
|
const std::vector<std::string> signatures{occurrence.signature};
|
|
const auto produced = features::inlay_hints::parameter_hints(signatures, snapshot->text,
|
|
use->second, requested);
|
|
hints.insert(hints.end(), produced.begin(), produced.end());
|
|
}
|
|
std::ranges::sort(hints);
|
|
const auto unique = std::ranges::unique(hints);
|
|
hints.erase(unique.begin(), unique.end());
|
|
Json::Array result;
|
|
for (const auto &hint : hints) {
|
|
if (result.size() == 256)
|
|
break;
|
|
result.push_back(
|
|
Json{Json::Object{{"position", position({hint.position.line, hint.position.character})},
|
|
{"label", Json{hint.label}},
|
|
{"kind", Json{static_cast<std::int64_t>(hint.kind)}},
|
|
{"paddingRight", Json{hint.padding_right}}}});
|
|
}
|
|
return Json{std::move(result)};
|
|
}
|
|
|
|
void EditorDiagnostics::set_transactional_rename(bool supported) {
|
|
std::lock_guard lock{state_->mutex};
|
|
state_->transactional_rename = supported;
|
|
}
|
|
|
|
EditorDiagnostics::NavigationReply
|
|
EditorDiagnostics::references(std::string_view uri, Documents::Position position) const {
|
|
auto &s = *state_;
|
|
std::lock_guard lock{s.mutex};
|
|
const auto identity = Documents::normalize_uri(uri);
|
|
if (!identity || !identity->is_file_uri)
|
|
return {Json{nullptr}, "invalid document URI"};
|
|
const auto path = identity->canonical_path.native();
|
|
const auto set = s.fact_set_locked();
|
|
const auto binding = navigation::binding_at(set, path, {position.line, position.character});
|
|
if (!binding)
|
|
return {Json{nullptr}, refusal_text(binding.error())};
|
|
const auto result = navigation::references(set, *binding);
|
|
Json::Array locations;
|
|
for (const auto &location : result.locations)
|
|
locations.push_back(Json{Json::Object{
|
|
{"uri", Json{Documents::path_to_file_uri(location.path)}},
|
|
{"range", range_json(Documents::Range{
|
|
{location.range.start.line, location.range.start.character},
|
|
{location.range.end.line, location.range.end.character}})}}});
|
|
auto value = Json{std::move(locations)};
|
|
if (!result.coverage_complete) {
|
|
std::string detail = "incomplete reference coverage";
|
|
for (const auto &context : result.incomplete_contexts)
|
|
detail += (detail.empty() ? "" : ", ") + context;
|
|
return {std::move(value), std::move(detail)};
|
|
}
|
|
return {std::move(value), std::nullopt};
|
|
}
|
|
|
|
EditorDiagnostics::NavigationReply
|
|
EditorDiagnostics::prepare_rename(std::string_view uri, Documents::Position position) const {
|
|
auto &s = *state_;
|
|
std::lock_guard lock{s.mutex};
|
|
const auto identity = Documents::normalize_uri(uri);
|
|
if (!identity || !identity->is_file_uri)
|
|
return {Json{nullptr}, "invalid document URI"};
|
|
const auto path = identity->canonical_path.native();
|
|
const auto set = s.fact_set_locked();
|
|
const auto binding = navigation::binding_at(set, path, {position.line, position.character});
|
|
if (!binding)
|
|
return {Json{nullptr}, refusal_text(binding.error())};
|
|
const auto plan = navigation::prepare_rename(set, path, {position.line, position.character},
|
|
binding->name + "_", s.transactional_rename);
|
|
if (plan.refusal == navigation::Refusal::incomplete_coverage ||
|
|
plan.refusal == navigation::Refusal::stale_source ||
|
|
plan.refusal == navigation::Refusal::non_editable_origin ||
|
|
plan.refusal == navigation::Refusal::ambiguous_binding)
|
|
return {Json{nullptr}, refusal_text(plan.refusal)};
|
|
const auto fact = std::ranges::find_if(set.facts, [&](const auto &candidate) {
|
|
return candidate.binding == *binding && candidate.source == path &&
|
|
candidate.use.start.line <= position.line && position.line <= candidate.use.end.line;
|
|
});
|
|
if (fact == set.facts.end())
|
|
return {Json{nullptr}, refusal_text(navigation::Refusal::unknown_binding)};
|
|
return {Json{Json::Object{{"range", range_json(Documents::Range{
|
|
{fact->use.start.line, fact->use.start.character},
|
|
{fact->use.end.line, fact->use.end.character}})},
|
|
{"placeholder", Json{fact->binding.name}}}},
|
|
std::nullopt};
|
|
}
|
|
|
|
EditorDiagnostics::NavigationReply EditorDiagnostics::rename(std::string_view uri,
|
|
Documents::Position position,
|
|
std::string_view replacement) const {
|
|
auto &s = *state_;
|
|
std::lock_guard lock{s.mutex};
|
|
const auto identity = Documents::normalize_uri(uri);
|
|
if (!identity || !identity->is_file_uri)
|
|
return {Json{nullptr}, "invalid document URI"};
|
|
const auto path = identity->canonical_path.native();
|
|
const auto set = s.fact_set_locked();
|
|
const auto plan = navigation::prepare_rename(set, path, {position.line, position.character},
|
|
replacement, s.transactional_rename);
|
|
if (!plan.ready())
|
|
return {Json{nullptr}, refusal_text(plan.refusal) + ": " + plan.detail};
|
|
Json::Object changes;
|
|
for (const auto &edit : plan.edits) {
|
|
auto uri_key = Documents::path_to_file_uri(edit.path);
|
|
auto &entry = changes[uri_key];
|
|
if (!std::holds_alternative<Json::Array>(entry.value))
|
|
entry = Json{Json::Array{}};
|
|
std::get<Json::Array>(entry.value)
|
|
.push_back(Json{Json::Object{
|
|
{"range",
|
|
range_json(Documents::Range{{edit.range.start.line, edit.range.start.character},
|
|
{edit.range.end.line, edit.range.end.character}})},
|
|
{"newText", Json{edit.replacement}}}});
|
|
}
|
|
return {Json{Json::Object{{"changes", Json{std::move(changes)}}}}, std::nullopt};
|
|
}
|
|
|
|
Json EditorDiagnostics::call_hierarchy_prepare(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{Json::Array{}};
|
|
receipt = i->second.receipt;
|
|
facts = i->second.occurrences;
|
|
}
|
|
if (!receipt || !facts || !s.valid(*receipt))
|
|
return Json{Json::Array{}};
|
|
const auto *match = covering(*receipt, *facts, *key, position);
|
|
if (!match || !match->has_definition())
|
|
return Json{Json::Array{}};
|
|
const auto &target = *match->definition_file;
|
|
const auto target_uri = target.identity.is_file_uri
|
|
? target.identity.canonical_uri
|
|
: Documents::path_to_file_uri(target.identity.canonical_path);
|
|
const auto range = range_json(match->definition);
|
|
Json::Array items;
|
|
items.push_back(Json{Json::Object{
|
|
{"name", Json{match->name}},
|
|
{"kind", Json{std::int64_t{12}}},
|
|
{"uri", Json{target_uri}},
|
|
{"range", range},
|
|
{"selectionRange", range},
|
|
{"data",
|
|
Json{Json::Object{
|
|
{"name", Json{match->name}},
|
|
{"path", Json{target.identity.canonical_path.native()}},
|
|
{"line", Json{static_cast<std::int64_t>(match->definition.start.line)}},
|
|
{"character", Json{static_cast<std::int64_t>(match->definition.start.character)}},
|
|
{"endLine", Json{static_cast<std::int64_t>(match->definition.end.line)}},
|
|
{"endCharacter",
|
|
Json{static_cast<std::int64_t>(match->definition.end.character)}}}}}}});
|
|
return Json{std::move(items)};
|
|
}
|
|
|
|
Json EditorDiagnostics::call_hierarchy_incoming(const Json &item) const {
|
|
auto &s = *state_;
|
|
const auto *fields = P::as_object(&item);
|
|
const auto *data = fields ? P::as_object(P::find_member(*fields, "data")) : nullptr;
|
|
const auto *name = data ? P::as_string(P::find_member(*data, "name")) : nullptr;
|
|
const auto *path = data ? P::as_string(P::find_member(*data, "path")) : nullptr;
|
|
const auto line = data ? P::as_int64(P::find_member(*data, "line")) : std::nullopt;
|
|
const auto character = data ? P::as_int64(P::find_member(*data, "character")) : std::nullopt;
|
|
const auto end_line = data ? P::as_int64(P::find_member(*data, "endLine")) : std::nullopt;
|
|
const auto end_character =
|
|
data ? P::as_int64(P::find_member(*data, "endCharacter")) : std::nullopt;
|
|
if (!name || !path || !line || !character || !end_line || !end_character)
|
|
return Json{Json::Array{}};
|
|
const features::call_hierarchy::Definition target{
|
|
*name,
|
|
*path,
|
|
{{static_cast<std::uint32_t>(*line), static_cast<std::uint32_t>(*character)},
|
|
{static_cast<std::uint32_t>(*end_line), static_cast<std::uint32_t>(*end_character)}}};
|
|
std::vector<features::call_hierarchy::Call> calls;
|
|
std::vector<features::call_hierarchy::Definition> known;
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
for (const auto &[key, entry] : s.entries) {
|
|
if (!entry.occurrences)
|
|
continue;
|
|
for (const auto &occurrence : *entry.occurrences) {
|
|
features::call_hierarchy::Call call;
|
|
call.callee = occurrence.name;
|
|
call.path = occurrence.use_file->identity.canonical_path.native();
|
|
call.range = {{occurrence.use.start.line, occurrence.use.start.character},
|
|
{occurrence.use.end.line, occurrence.use.end.character}};
|
|
if (occurrence.has_definition()) {
|
|
const auto &extent =
|
|
occurrence.extent ? *occurrence.extent : occurrence.definition;
|
|
features::call_hierarchy::Definition definition{
|
|
occurrence.name,
|
|
occurrence.definition_file->identity.canonical_path.native(),
|
|
{{extent.start.line, extent.start.character},
|
|
{extent.end.line, extent.end.character}}};
|
|
call.callee_definition = definition;
|
|
known.push_back(std::move(definition));
|
|
}
|
|
calls.push_back(std::move(call));
|
|
}
|
|
}
|
|
}
|
|
const auto grouped = features::call_hierarchy::incoming(calls, known, target);
|
|
Json::Array result;
|
|
for (const auto &caller : grouped) {
|
|
Json::Array ranges;
|
|
for (const auto &range : caller.from_ranges)
|
|
ranges.push_back(range_json(Documents::Range{{range.start.line, range.start.character},
|
|
{range.end.line, range.end.character}}));
|
|
const auto span =
|
|
range_json(Documents::Range{{caller.range.start.line, caller.range.start.character},
|
|
{caller.range.end.line, caller.range.end.character}});
|
|
Json::Object from{{"name", Json{caller.name}},
|
|
{"kind", Json{std::int64_t{caller.resolved_definition ? 12 : 1}}},
|
|
{"uri", Json{Documents::path_to_file_uri(caller.path)}},
|
|
{"range", span},
|
|
{"selectionRange", span}};
|
|
result.push_back(Json{Json::Object{{"from", Json{std::move(from)}},
|
|
{"fromRanges", Json{std::move(ranges)}}}});
|
|
}
|
|
return Json{std::move(result)};
|
|
}
|
|
|
|
Json EditorDiagnostics::call_hierarchy_outgoing(const Json &item) const {
|
|
auto &s = *state_;
|
|
const auto *fields = P::as_object(&item);
|
|
const auto *data = fields ? P::as_object(P::find_member(*fields, "data")) : nullptr;
|
|
const auto *name = data ? P::as_string(P::find_member(*data, "name")) : nullptr;
|
|
const auto *path = data ? P::as_string(P::find_member(*data, "path")) : nullptr;
|
|
const auto line = data ? P::as_int64(P::find_member(*data, "line")) : std::nullopt;
|
|
const auto character = data ? P::as_int64(P::find_member(*data, "character")) : std::nullopt;
|
|
const auto end_line = data ? P::as_int64(P::find_member(*data, "endLine")) : std::nullopt;
|
|
const auto end_character =
|
|
data ? P::as_int64(P::find_member(*data, "endCharacter")) : std::nullopt;
|
|
if (!name || !path || !line || !character || !end_line || !end_character)
|
|
return Json{Json::Array{}};
|
|
const features::call_hierarchy::Definition caller{
|
|
*name,
|
|
*path,
|
|
{{static_cast<std::uint32_t>(*line), static_cast<std::uint32_t>(*character)},
|
|
{static_cast<std::uint32_t>(*end_line), static_cast<std::uint32_t>(*end_character)}}};
|
|
std::vector<features::call_hierarchy::Call> calls;
|
|
std::vector<features::call_hierarchy::Definition> known;
|
|
{
|
|
std::lock_guard lock{s.mutex};
|
|
for (const auto &[key, entry] : s.entries) {
|
|
if (!entry.occurrences)
|
|
continue;
|
|
for (const auto &occurrence : *entry.occurrences) {
|
|
features::call_hierarchy::Call call;
|
|
call.callee = occurrence.name;
|
|
call.path = occurrence.use_file->identity.canonical_path.native();
|
|
call.range = {{occurrence.use.start.line, occurrence.use.start.character},
|
|
{occurrence.use.end.line, occurrence.use.end.character}};
|
|
if (occurrence.has_definition()) {
|
|
const auto &extent =
|
|
occurrence.extent ? *occurrence.extent : occurrence.definition;
|
|
features::call_hierarchy::Definition definition{
|
|
occurrence.name,
|
|
occurrence.definition_file->identity.canonical_path.native(),
|
|
{{extent.start.line, extent.start.character},
|
|
{extent.end.line, extent.end.character}}};
|
|
call.callee_definition = definition;
|
|
known.push_back(std::move(definition));
|
|
}
|
|
calls.push_back(std::move(call));
|
|
}
|
|
}
|
|
}
|
|
const auto grouped = features::call_hierarchy::outgoing(calls, known, caller);
|
|
Json::Array result;
|
|
for (const auto &callee : grouped) {
|
|
Json::Array ranges;
|
|
for (const auto &range : callee.from_ranges)
|
|
ranges.push_back(range_json(Documents::Range{{range.start.line, range.start.character},
|
|
{range.end.line, range.end.character}}));
|
|
const auto span = range_json(
|
|
Documents::Range{{callee.to.range.start.line, callee.to.range.start.character},
|
|
{callee.to.range.end.line, callee.to.range.end.character}});
|
|
Json::Object to{{"name", Json{callee.to.name}},
|
|
{"kind", Json{std::int64_t{12}}},
|
|
{"uri", Json{Documents::path_to_file_uri(callee.to.path)}},
|
|
{"range", span},
|
|
{"selectionRange", span}};
|
|
result.push_back(Json{
|
|
Json::Object{{"to", Json{std::move(to)}}, {"fromRanges", Json{std::move(ranges)}}}});
|
|
}
|
|
return Json{std::move(result)};
|
|
}
|
|
|
|
Json EditorDiagnostics::hover(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 auto *match = covering(*receipt, *facts, *key, position);
|
|
if (!match)
|
|
return Json{nullptr};
|
|
std::string text =
|
|
match->signature.empty() ? match->name + ": " + match->type : match->signature;
|
|
text += "\n" + match->canonical_type;
|
|
return Json{
|
|
Json::Object{{"contents", Json{Json::Object{{"kind", Json{std::string{"plaintext"}}},
|
|
{"value", Json{std::move(text)}}}}},
|
|
{"range", range_json(match->use)}}};
|
|
}
|
|
|
|
Json EditorDiagnostics::signature_help(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};
|
|
const auto offset = snapshot->position_to_offset(position);
|
|
if (!offset)
|
|
return Json{nullptr};
|
|
// The occurrence span covers the callee name only, so the innermost call
|
|
// whose argument list contains the cursor is selected explicitly.
|
|
const Occurrence *match = nullptr;
|
|
std::size_t match_start{};
|
|
for (const auto &fact : *facts) {
|
|
if (fact.use_file.get() != snapshot || fact.signature.empty())
|
|
continue;
|
|
const auto use = snapshot->range_to_offsets(fact.use);
|
|
if (!use)
|
|
continue;
|
|
if (!features::signature::in_call(snapshot->text, use->first, use->second, *offset))
|
|
continue;
|
|
if (!match || use->first > match_start) {
|
|
match = &fact;
|
|
match_start = use->first;
|
|
}
|
|
}
|
|
if (!match)
|
|
return Json{nullptr};
|
|
const std::vector<std::string> signatures{match->signature};
|
|
const auto help = features::signature::build(signatures, snapshot->text, *offset);
|
|
if (help.signatures.empty())
|
|
return Json{nullptr};
|
|
Json::Array rendered;
|
|
for (const auto &signature : help.signatures) {
|
|
Json::Array parameters;
|
|
for (const auto ¶meter : signature.parameters) {
|
|
parameters.push_back(Json{Json::Object{
|
|
{"label",
|
|
parameter.range
|
|
? Json{Json::Array{Json{static_cast<std::int64_t>(parameter.range->first)},
|
|
Json{static_cast<std::int64_t>(parameter.range->second)}}}
|
|
: Json{parameter.label}}}});
|
|
}
|
|
rendered.push_back(Json{Json::Object{{"label", Json{signature.label}},
|
|
{"parameters", Json{std::move(parameters)}}}});
|
|
}
|
|
return Json{
|
|
Json::Object{{"signatures", Json{std::move(rendered)}},
|
|
{"activeSignature", Json{static_cast<std::int64_t>(help.active_signature)}},
|
|
{"activeParameter", Json{static_cast<std::int64_t>(help.active_parameter)}}}};
|
|
}
|
|
|
|
Json EditorDiagnostics::definition(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 auto *match = covering(*receipt, *facts, *key, position);
|
|
if (!match || !match->has_definition())
|
|
return Json{nullptr};
|
|
const auto &target = *match->definition_file;
|
|
const auto target_uri = target.identity.is_file_uri
|
|
? target.identity.canonical_uri
|
|
: Documents::path_to_file_uri(target.identity.canonical_path);
|
|
return Json{Json::Object{{"uri", Json{target_uri}}, {"range", range_json(match->definition)}}};
|
|
}
|
|
} // namespace vls::semantic
|