Offer compiler quick fixes as code actions

Decode GCC SARIF fixes into bounded diagnostic edits through the same
physical-origin policy as locations; unprovable regions are dropped. A
bounded projection filters fixes to the requested range, deduplicates
and bounds them, and textDocument/codeAction serves them from retained
published diagnostics. Registered tests cover the SARIF binding and the
projection; a real stdio session returned the compiler's
Replace 'valu' with 'value' quickfix.
This commit is contained in:
faumaray committed 2026-12-05 15:03:21 +03:00
1 parent 624c24d028
commit 946b1a005e
15 files changed
+418 -10

No files matched your search

+68 -1
View File
@@ -1,5 +1,6 @@
#include <vls/semantic/EditorDiagnostics.hpp>
#include <vls/features/CodeActions.hpp>
#include <vls/features/Completion.hpp>
#include <vls/features/DocumentSymbols.hpp>
#include <vls/features/IndexNavigation.hpp>
@@ -99,8 +100,17 @@ int symbol_kind(index::SymbolKind kind) {
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{};
};
@@ -114,6 +124,8 @@ EditorReport for_owner(const DiagnosticBatch &batch) {
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)}}}},
@@ -150,6 +162,7 @@ struct Entry {
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;
@@ -331,8 +344,11 @@ struct EditorDiagnostics::State {
entry.sent = true;
entry.had_findings = has_findings;
entry.published_version = owner->version;
} else
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;
@@ -1092,6 +1108,57 @@ Json EditorDiagnostics::folding_ranges(std::string_view uri) const {
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)};
}
void EditorDiagnostics::set_transactional_rename(bool supported) {
std::lock_guard lock{state_->mutex};
state_->transactional_rename = supported;