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
@@ -22,7 +22,7 @@ vls_test_suite(vls_scheduler_tests scheduler/SchedulerTests.cpp)
|
||||
vls_test_suite(vls_index_tests index/SymbolIndexTests.cpp)
|
||||
vls_test_suite(vls_navigation_tests navigation/NavigationTests.cpp)
|
||||
vls_test_suite(vls_reflection_tests reflection/ProvenanceTests.cpp)
|
||||
vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp)
|
||||
vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp)
|
||||
vls_test_suite(vls_live_tests debug/LiveSessionTests.cpp debug/LiveDapTests.cpp)
|
||||
target_link_libraries(vls_live_tests PRIVATE Void::System)
|
||||
add_dependencies(vls_live_tests vls_live_worker_fixture vls-dap)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <vls/features/CodeActions.hpp>
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace vls::features::code_actions;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::require;
|
||||
|
||||
Range range(std::uint32_t line, std::uint32_t character, std::uint32_t end_character) {
|
||||
return {{line, character}, {line, end_character}};
|
||||
}
|
||||
|
||||
Fix fix(std::string title, std::vector<Edit> edits, std::string path = "/project/a.cpp") {
|
||||
return {std::move(title), std::move(path), std::move(edits)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(CodeActions, keeps_only_fixes_intersecting_the_requested_range) {
|
||||
const std::vector<Fix> fixes{fix("Replace value", {{range(3, 12, 16), "value"}}),
|
||||
fix("Replace other", {{range(9, 0, 4), "other"}}),
|
||||
fix("Adjoining", {{range(4, 0, 2), "next"}})};
|
||||
const auto result = actions(fixes, range(3, 14, 14));
|
||||
require(result.size() == 1);
|
||||
expect(result.front().title == "Replace value");
|
||||
expect(result.front().edits.front().replacement == "value");
|
||||
}
|
||||
|
||||
VOID_TEST(CodeActions, empty_and_malformed_fixes_are_dropped) {
|
||||
const std::vector<Fix> fixes{fix("No edits", {}), fix("No path", {{range(1, 0, 2), "x"}}, ""),
|
||||
fix("Reversed", {{{{2, 4}, {2, 1}}, "x"}}),
|
||||
fix("Valid", {{range(1, 0, 2), "x"}})};
|
||||
const auto result = actions(fixes, range(1, 0, 1));
|
||||
require(result.size() == 1);
|
||||
expect(result.front().title == "Valid");
|
||||
}
|
||||
|
||||
VOID_TEST(CodeActions, deduplicates_edits_and_actions) {
|
||||
const std::vector<Fix> fixes{fix("Replace", {{range(1, 0, 2), "x"}, {range(1, 0, 2), "x"}}),
|
||||
fix("Replace", {{range(1, 0, 2), "x"}})};
|
||||
const auto result = actions(fixes, range(1, 0, 1));
|
||||
require(result.size() == 1);
|
||||
expect(result.front().edits.size() == 1);
|
||||
}
|
||||
|
||||
VOID_TEST(CodeActions, bounds_actions_edits_and_text) {
|
||||
std::vector<Fix> fixes;
|
||||
for (int index = 0; index != 5; ++index)
|
||||
fixes.push_back(
|
||||
fix("Action " + std::to_string(index),
|
||||
{{range(1, 0, 2), "one"}, {range(1, 3, 4), "two"}, {range(1, 5, 6), "three"}}));
|
||||
const auto result = actions(fixes, range(1, 0, 6), Limits{.actions = 2, .edits = 2});
|
||||
expect(result.size() == 2);
|
||||
expect(result.front().edits.size() == 2);
|
||||
const auto bounded = actions(fixes, range(1, 0, 6), Limits{.titleBytes = 3, .editBytes = 2});
|
||||
require(!bounded.empty());
|
||||
expect(bounded.front().title == "Act");
|
||||
expect(bounded.front().edits.front().replacement == "on");
|
||||
}
|
||||
|
||||
VOID_TEST(CodeActions, preserves_input_order) {
|
||||
const std::vector<Fix> fixes{fix("Second alphabetically", {{range(1, 0, 2), "b"}}),
|
||||
fix("First alphabetically", {{range(1, 0, 2), "a"}})};
|
||||
const auto result = actions(fixes, range(1, 0, 1));
|
||||
require(result.size() == 2);
|
||||
expect(result[0].title == "Second alphabetically");
|
||||
expect(result[1].title == "First alphabetically");
|
||||
}
|
||||
@@ -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