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.
73 lines
3.0 KiB
C++
73 lines
3.0 KiB
C++
#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");
|
|
}
|