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:
1 parent
624c24d028
commit
946b1a005e
15 files changed
+418
-10
No files matched your search
@@ -333,3 +333,31 @@ VOID_TEST(DiagnosticTests, report_structure_and_output_sizes_are_bounded) {
|
||||
0)
|
||||
.has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, compiler_fixes_bind_to_the_owner_snapshot) {
|
||||
Capture f{"int value = 1;\nint main() { return valu; }\n"};
|
||||
const auto region = R"({"startLine":2,"startColumn":21,"endColumn":25})"sv;
|
||||
const auto fix =
|
||||
R"("fixes":[{"description":{"text":"Replace 'valu' with 'value'"},"artifactChanges":[{"artifactLocation":)" +
|
||||
std::string{primary} +
|
||||
R"(,"replacements":[{"deletedRegion":{"startLine":2,"startColumn":21,"endColumn":25},"insertedContent":{"text":"value"}}]}]}])";
|
||||
const auto findings =
|
||||
R"({"level":"error","ruleId":"error","message":{"text":"did you mean value?"},"locations":[)" +
|
||||
location(primary, region) + "]," + fix + "}";
|
||||
const auto batch = f.batch(report(false, findings));
|
||||
require(batch.diagnostics.size() == 1);
|
||||
const auto &diagnostic = batch.diagnostics.front();
|
||||
require(diagnostic.fixes.size() == 1);
|
||||
expect(diagnostic.fixes[0].description == "Replace 'valu' with 'value'");
|
||||
require(diagnostic.fixes[0].edits.size() == 1);
|
||||
expect(diagnostic.fixes[0].edits[0].range == D::Range{{1, 20}, {1, 24}});
|
||||
expect(diagnostic.fixes[0].edits[0].replacement == "value");
|
||||
|
||||
// A fix for another artifact, or one whose region cannot be proven, is dropped.
|
||||
const auto foreign =
|
||||
R"("fixes":[{"description":{"text":"x"},"artifactChanges":[{"artifactLocation":{"uri":"a.hpp","uriBaseId":"PWD"},"replacements":[{"deletedRegion":{"startLine":2,"startColumn":21,"endColumn":25},"insertedContent":{"text":"value"}}]}]}])";
|
||||
const auto dropped = f.batch(
|
||||
report(false, R"({"level":"error","ruleId":"error","message":{"text":"m"},"locations":[)" +
|
||||
location(primary, region) + "]," + foreign + "}"));
|
||||
expect(dropped.diagnostics.front().fixes.empty());
|
||||
}
|
||||
Reference in new issue
Block a user