Candidates carry a local flag set from the index record path, and the deterministic sort key orders local candidates before remote ones within the same kind. Registered feature tests cover the ranking.
144 lines
6.1 KiB
C++
144 lines
6.1 KiB
C++
#include <vls/features/Completion.hpp>
|
|
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace vls::features::completion;
|
|
using Void::Testing::expect;
|
|
using Void::Testing::require;
|
|
|
|
Candidate make_candidate(std::string label, Kind kind = Kind::function, std::string detail = {}) {
|
|
return {std::move(label), kind, std::move(detail), "gcc"};
|
|
}
|
|
|
|
std::vector<std::string> labels(const Result &result) {
|
|
std::vector<std::string> names;
|
|
for (const auto &item : result.items)
|
|
names.push_back(item.label);
|
|
return names;
|
|
}
|
|
} // namespace
|
|
|
|
VOID_TEST(Completion, prefix_is_the_identifier_before_the_offset) {
|
|
expect(prefix_at("value.", 6).empty());
|
|
expect(prefix_at("value.fo", 8) == "fo");
|
|
expect(prefix_at("fo", 2) == "fo");
|
|
expect(prefix_at(" foo_bar", 9) == "foo_bar");
|
|
expect(prefix_at("_x", 2) == "_x");
|
|
expect(prefix_at("1abc", 4).empty());
|
|
expect(prefix_at("abc", 1) == "a");
|
|
expect(prefix_at("abc", 4).empty());
|
|
}
|
|
|
|
VOID_TEST(Completion, filters_by_prefix_and_replaces_it) {
|
|
const std::vector<Candidate> candidates{make_candidate("value"), make_candidate("validate"),
|
|
make_candidate("other")};
|
|
const auto result = complete(candidates, "int va = 0;", 6);
|
|
expect(labels(result) == std::vector<std::string>{"validate", "value"});
|
|
for (const auto &item : result.items) {
|
|
expect(item.replacement_start == 4);
|
|
expect(item.replacement_end == 6);
|
|
}
|
|
expect(result.incomplete);
|
|
expect(result.backend == "symbol-index");
|
|
}
|
|
|
|
VOID_TEST(Completion, deduplicates_by_label_and_kind) {
|
|
const std::vector<Candidate> candidates{make_candidate("f", Kind::function),
|
|
make_candidate("f", Kind::function),
|
|
make_candidate("f", Kind::variable)};
|
|
const auto result = complete(candidates, "f", 1);
|
|
require(result.items.size() == 2);
|
|
expect(result.items[0].kind == Kind::function);
|
|
expect(result.items[1].kind == Kind::variable);
|
|
}
|
|
|
|
VOID_TEST(Completion, ranks_types_before_functions_before_variables) {
|
|
const std::vector<Candidate> candidates{make_candidate("zz", Kind::variable),
|
|
make_candidate("zz", Kind::function),
|
|
make_candidate("zz", Kind::type)};
|
|
const auto result = complete(candidates, "z", 1);
|
|
require(result.items.size() == 3);
|
|
expect(result.items[0].kind == Kind::type);
|
|
expect(result.items[1].kind == Kind::function);
|
|
expect(result.items[2].kind == Kind::variable);
|
|
expect(result.items[0].sort_text == "01zz");
|
|
}
|
|
|
|
VOID_TEST(Completion, empty_prefix_lists_everything_bounded) {
|
|
const std::vector<Candidate> candidates{make_candidate("b"), make_candidate("a")};
|
|
const auto result = complete(candidates, "", 0, Limits{.candidates = 1});
|
|
require(result.items.size() == 1);
|
|
expect(result.items.front().label == "a");
|
|
const auto unrestricted = complete(candidates, "", 0);
|
|
expect(labels(unrestricted) == std::vector<std::string>{"a", "b"});
|
|
}
|
|
|
|
VOID_TEST(Completion, bounds_every_field_and_rejects_oversized_labels) {
|
|
const std::vector<Candidate> candidates{
|
|
make_candidate("x", Kind::function, std::string(600, 'd')),
|
|
make_candidate(std::string(300, 'y'), Kind::function)};
|
|
const auto result = complete(candidates, "x", 1);
|
|
require(result.items.size() == 1);
|
|
expect(result.items.front().detail.size() == 512);
|
|
expect(result.items.front().provenance.size() <= 128);
|
|
}
|
|
|
|
namespace {
|
|
Identity identity(std::string name = "f", std::string path = "/project/a.cpp",
|
|
std::uint32_t line = 3) {
|
|
return {std::move(name), std::move(path), line, 4};
|
|
}
|
|
|
|
Detail detail(std::string text = "int f()", std::string provenance = "gcc") {
|
|
return {identity(), Kind::function, std::move(text), std::move(provenance)};
|
|
}
|
|
} // namespace
|
|
|
|
VOID_TEST(Completion, resolve_returns_details_for_the_same_generation) {
|
|
const std::vector<Detail> current{detail()};
|
|
const auto resolved = resolve(identity(), 7, 7, current);
|
|
expect(resolved.status == Resolved::Status::resolved);
|
|
expect(resolved.detail == "int f()");
|
|
expect(resolved.documentation.find("int f()") != std::string::npos);
|
|
expect(resolved.documentation.find("/project/a.cpp:3:4") != std::string::npos);
|
|
expect(resolved.documentation.find("Provenance: gcc") != std::string::npos);
|
|
}
|
|
|
|
VOID_TEST(Completion, resolve_is_stale_after_a_generation_change) {
|
|
const std::vector<Detail> current{detail()};
|
|
const auto resolved = resolve(identity(), 7, 8, current);
|
|
expect(resolved.status == Resolved::Status::stale);
|
|
expect(resolved.documentation.empty() && resolved.detail.empty());
|
|
}
|
|
|
|
VOID_TEST(Completion, resolve_is_unknown_for_a_missing_or_different_identity) {
|
|
const std::vector<Detail> current{detail()};
|
|
const auto missing = resolve(identity("absent"), 7, 7, current);
|
|
expect(missing.status == Resolved::Status::unknown);
|
|
const auto other_path = resolve(identity("f", "/project/b.cpp"), 7, 7, current);
|
|
expect(other_path.status == Resolved::Status::unknown);
|
|
const auto other_line = resolve(identity("f", "/project/a.cpp", 9), 7, 7, current);
|
|
expect(other_line.status == Resolved::Status::unknown);
|
|
}
|
|
|
|
VOID_TEST(Completion, resolve_fences_hostile_content) {
|
|
const std::vector<Detail> current{detail("text with ``` a fence and even ```` four")};
|
|
const auto resolved = resolve(identity(), 7, 7, current);
|
|
require(resolved.status == Resolved::Status::resolved);
|
|
expect(resolved.documentation.starts_with("`````text"));
|
|
expect(resolved.documentation.find("\n`````\n") != std::string::npos);
|
|
}
|
|
|
|
VOID_TEST(Completion, resolve_bounds_the_documentation_and_keeps_the_fence_closed) {
|
|
const std::vector<Detail> current{detail(std::string(4000, 'x'))};
|
|
const auto resolved = resolve(identity(), 7, 7, current, Limits{.detailBytes = 128});
|
|
require(resolved.status == Resolved::Status::resolved);
|
|
expect(resolved.documentation.size() <= 128);
|
|
expect(resolved.documentation.ends_with("\n```\n"));
|
|
}
|