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

+31
View File
@@ -266,6 +266,11 @@ DispatchResult Server::handle(std::string_view body) {
capabilities.emplace("selectionRangeProvider", Json{true});
capabilities.emplace("foldingRangeProvider", Json{true});
}
if (diagnostics_ && diagnostics_->supports_occurrences())
capabilities.emplace(
"codeActionProvider",
Json{Json::Object{
{"codeActionKinds", Json{Json::Array{Json{std::string{"quickfix"}}}}}}});
if (diagnostics_ && diagnostics_->supports_occurrences()) {
capabilities.emplace("hoverProvider", Json{true});
capabilities.emplace("definitionProvider", Json{true});
@@ -373,6 +378,32 @@ DispatchResult Server::handle(std::string_view body) {
}
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
}
if (*method == "textDocument/codeAction") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())
return error(id, -32601, "Method not found");
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
const auto *range = member(params, "range");
const auto *start = member(range, "start");
const auto *end = member(range, "end");
const auto coordinate = [](const Json *value,
std::string_view key) -> std::optional<std::uint32_t> {
const auto *field = member(value, key);
const auto *number = field ? std::get_if<std::int64_t>(&field->value) : nullptr;
if (!number || *number < 0 ||
*number > Void::LanguageServer::Documents::kMaxPositionValue)
return std::nullopt;
return static_cast<std::uint32_t>(*number);
};
const auto start_line = coordinate(start, "line"),
start_character = coordinate(start, "character");
const auto end_line = coordinate(end, "line"), end_character = coordinate(end, "character");
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri) || !start_line ||
!start_character || !end_line || !end_character)
return error(id, -32602, "Expected textDocument.uri and a bounded range");
return reply(id, "result",
diagnostics_->code_actions(
*uri, {{*start_line, *start_character}, {*end_line, *end_character}}));
}
if (*method == "textDocument/documentSymbol") {
if (!diagnostics_)
return error(id, -32601, "Method not found");
+47
View File
@@ -0,0 +1,47 @@
#include <vls/features/CodeActions.hpp>
#include <algorithm>
#include <set>
#include <tuple>
#include <utility>
namespace vls::features::code_actions {
namespace {
bool intersects(const Range &left, const Range &right) {
return left.start <= right.end && right.start <= left.end;
}
std::string bounded(std::string value, std::size_t limit) {
if (value.size() > limit)
value.resize(limit);
return value;
}
} // namespace
std::vector<Action> actions(std::span<const Fix> fixes, Range requested, const Limits &limits) {
std::vector<Action> result;
std::set<std::tuple<std::string, std::string, std::vector<Edit>>> seen;
for (const auto &fix : fixes) {
if (fix.path.empty() || result.size() >= limits.actions)
continue;
Action action{.title = bounded(fix.title, limits.titleBytes), .path = fix.path, .edits = {}};
std::set<Edit> unique;
for (const auto &edit : fix.edits) {
if (action.edits.size() >= limits.edits)
break;
if (edit.range.end < edit.range.start || !intersects(edit.range, requested))
continue;
if (!unique.insert(edit).second)
continue;
action.edits.push_back({edit.range, bounded(edit.replacement, limits.editBytes)});
}
if (action.edits.empty())
continue;
if (!seen.emplace(action.title, action.path, action.edits).second)
continue;
result.push_back(std::move(action));
}
return result;
}
} // namespace vls::features::code_actions
+72 -1
View File
@@ -17,6 +17,7 @@ template <class T> using Result = std::expected<T, Error>;
constexpr std::size_t max_report_bytes = 4 * 1024 * 1024;
constexpr std::size_t max_artifacts = 2048, max_results = 4096, max_locations = 16384;
constexpr std::size_t max_name = 16384, max_message = 65536;
constexpr std::size_t max_fixes = 8, max_edits = 4, max_fix_text = 4096;
constexpr std::string_view bom = "\xef\xbb\xbf";
// Cap container allocation in Void's JSON value tree before parsing. Brackets
@@ -317,6 +318,74 @@ Result<void> append_locations(Reader &reader, const Json *value, std::size_t ind
}
return {};
}
Result<void> append_fixes(Reader &reader, const Json *value, std::size_t index,
CompilerDiagnostic &diagnostic) {
if (!value)
return {};
const auto *fixes = P::as_array(value);
if (!fixes || fixes->size() > max_fixes)
return std::unexpected(Error::malformed_report);
if (fixes->empty())
return {};
if (!diagnostic.location.snapshot || !diagnostic.location.range)
return {};
const auto &snapshot = *diagnostic.location.snapshot;
for (const auto &item : *fixes) {
const auto *fix = P::as_object(&item);
if (!fix)
return std::unexpected(Error::malformed_report);
const auto *description = string(object(fix, "description"), "text");
if (description && description->size() > max_message)
return std::unexpected(Error::resource_limit);
const auto *changes = P::as_array(member(fix, "artifactChanges"));
if (!changes || changes->size() != 1)
return std::unexpected(Error::malformed_report);
const auto *change = P::as_object(&changes->front());
if (!change)
return std::unexpected(Error::malformed_report);
const auto file = reader.binding(object(change, "artifactLocation"));
const auto *replacements = P::as_array(member(change, "replacements"));
if (!file || !file->file || file->file->snapshot != diagnostic.location.snapshot ||
!file->contents_match || !replacements || replacements->size() > max_edits)
continue;
DiagnosticFix parsed;
parsed.description = description ? *description : std::string{};
for (const auto &replacement_item : *replacements) {
const auto *replacement = P::as_object(&replacement_item);
const auto *region = object(replacement, "deletedRegion");
const auto first = natural(member(region, "startLine"));
const auto left = natural(member(region, "startColumn"));
const auto last =
member(region, "endLine") ? natural(member(region, "endLine")) : first;
const auto right = natural(member(region, "endColumn"));
const auto *content = string(object(replacement, "insertedContent"), "text");
if (!first || !left || !last || !right || !*first || !*left || !*last || !*right ||
*last < *first || (*last == *first && *right < *left))
continue;
if (content && content->size() > max_fix_text)
return std::unexpected(Error::resource_limit);
const LocationRequest request{
*file->file, {*first, *left, *last, *right}, index, 0, false};
const auto policy =
reader.job.physical_origin ? reader.job.physical_origin(request) : std::nullopt;
if (!policy)
continue;
auto start = reader.position(snapshot, *first, *left, *policy);
if (!start)
return std::unexpected(start.error());
auto end = reader.position(snapshot, *last, *right, *policy);
if (!end)
return std::unexpected(end.error());
if (!*start || !*end || **end < **start)
continue;
parsed.edits.push_back(
{Documents::Range{**start, **end}, content ? *content : std::string{}});
}
if (!parsed.edits.empty())
diagnostic.fixes.push_back(std::move(parsed));
}
return {};
}
} // namespace
std::expected<DiagnosticBatch, DiagnosticError>
@@ -377,7 +446,7 @@ decode_gcc_diagnostics(const DiagnosticJob &job, const DiagnosticOutput &output)
return std::unexpected(Error::malformed_report);
if (message->size() > max_message || rule->size() > max_name)
return std::unexpected(Error::resource_limit);
CompilerDiagnostic diagnostic{*level, *message, *rule, {}, {}};
CompilerDiagnostic diagnostic{*level, *message, *rule, {}, {}, {}};
if (auto primary =
append_locations(reader, member(record, "locations"), i, false, diagnostic);
!primary)
@@ -386,6 +455,8 @@ decode_gcc_diagnostics(const DiagnosticJob &job, const DiagnosticOutput &output)
append_locations(reader, member(record, "relatedLocations"), i, true, diagnostic);
!related)
return std::unexpected(related.error());
if (auto fixes = append_fixes(reader, member(record, "fixes"), i, diagnostic); !fixes)
return std::unexpected(fixes.error());
source_error |= *level == DiagnosticSeverity::error;
if (!diagnostic.location.range || *rule == "fatal error" ||
*rule == "internal compiler error")
+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;