The bounded inlay-hint core now matches a call's closing parenthesis (nested calls, literals and comments aware) and places a type hint labelled with the compiler's canonical type. EditorDiagnostics emits it for every occurrence independently of parameter signatures. Registered tests cover nested matching, placement, skipped cases and bounds; a real stdio session showed both the parameter and return hints at the fnv1a64 call.
105 lines
4.2 KiB
C++
105 lines
4.2 KiB
C++
#include <vls/features/InlayHints.hpp>
|
|
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace vls::features::inlay_hints;
|
|
using Void::Testing::expect;
|
|
using Void::Testing::require;
|
|
|
|
Range whole_document() { return {{0, 0}, {1000, 0}}; }
|
|
} // namespace
|
|
|
|
VOID_TEST(InlayHints, finds_top_level_argument_starts) {
|
|
const std::string text = "call(1, two, three);";
|
|
const auto starts = argument_starts(text, 4);
|
|
require(starts.size() == 3);
|
|
expect(text.substr(starts[0], 1) == "1");
|
|
expect(text.substr(starts[1], 3) == "two");
|
|
expect(text.substr(starts[2], 5) == "three");
|
|
expect(argument_starts("call();", 4).empty());
|
|
expect(argument_starts("call", 4).empty());
|
|
}
|
|
|
|
VOID_TEST(InlayHints, nested_brackets_literals_and_comments_do_not_split) {
|
|
const std::string text = R"(call(inner(1, 2), [3, 4], {5, 6}, "a, b", /* , */ tail));)";
|
|
const auto starts = argument_starts(text, 4);
|
|
require(starts.size() == 5);
|
|
expect(text.substr(starts[1], 1) == "[");
|
|
expect(text.substr(starts[3], 1) == "\"");
|
|
expect(text.find("tail", starts[4]) == starts[4]);
|
|
}
|
|
|
|
VOID_TEST(InlayHints, parameter_hints_follow_the_signature) {
|
|
const std::vector<std::string> signatures{"int add(int, double)"};
|
|
const std::string text = "auto x = add(1, 2.0);";
|
|
const auto hints = parameter_hints(signatures, text, text.find("add") + 3, whole_document());
|
|
require(hints.size() == 2);
|
|
expect(hints[0].label == "int:" && hints[0].position.character == 13);
|
|
expect(hints[1].label == "double:" && hints[1].position.character == 16);
|
|
expect(hints[0].kind == 2 && hints[0].padding_right && !hints[0].padding_left);
|
|
}
|
|
|
|
VOID_TEST(InlayHints, hints_are_filtered_by_the_requested_range) {
|
|
const std::vector<std::string> signatures{"int add(int, int)"};
|
|
const std::string text = "add(1, 2);";
|
|
const auto hints = parameter_hints(signatures, text, 3, {{0, 4}, {0, 5}});
|
|
require(hints.size() == 1);
|
|
expect(hints.front().label == "int:");
|
|
expect(hints.front().position.character == 4);
|
|
}
|
|
|
|
VOID_TEST(InlayHints, a_short_signature_or_missing_signature_contributes_nothing) {
|
|
const std::string text = "add(1, 2, 3);";
|
|
expect(parameter_hints(std::vector<std::string>{"int add(int, int)"}, text, 3, whole_document())
|
|
.empty());
|
|
expect(parameter_hints({}, text, 3, whole_document()).empty());
|
|
expect(parameter_hints(std::vector<std::string>{"int add(int, int, int)"}, "add();", 3,
|
|
whole_document())
|
|
.empty());
|
|
}
|
|
|
|
VOID_TEST(InlayHints, hints_and_labels_are_bounded) {
|
|
std::vector<std::string> signatures{"void f(int, int, int, int)"};
|
|
const std::string text = "f(1, 2, 3, 4);";
|
|
Limits limits;
|
|
limits.arguments = 2;
|
|
limits.hints = 1;
|
|
limits.labelBytes = 2;
|
|
const auto hints = parameter_hints(signatures, text, 1, whole_document(), limits);
|
|
require(hints.size() == 1);
|
|
expect(hints.front().label == "in:");
|
|
}
|
|
|
|
VOID_TEST(InlayHints, call_end_matches_nested_calls_and_literals) {
|
|
const std::string text = "f(g(1, 2), \")\", h()) + 1;";
|
|
const auto end = call_end(text, 1);
|
|
require(end.has_value());
|
|
expect(*end == text.find(") + 1") + 1);
|
|
expect(!call_end("f(1, 2", 1).has_value());
|
|
expect(!call_end("f", 1).has_value());
|
|
}
|
|
|
|
VOID_TEST(InlayHints, return_type_hint_follows_the_closing_paren) {
|
|
const std::string text = "auto x = f(1, 2);";
|
|
const auto hint = return_type_hint(text, text.find("f") + 1, "int", whole_document());
|
|
require(hint.has_value());
|
|
expect(hint->label == ": int");
|
|
expect(hint->kind == 1 && hint->padding_left && !hint->padding_right);
|
|
expect(hint->position.character == text.find(")") + 1);
|
|
}
|
|
|
|
VOID_TEST(InlayHints, return_type_hint_requires_type_call_and_range) {
|
|
const std::string text = "auto x = f(1);";
|
|
expect(!return_type_hint(text, 10, "", whole_document()).has_value());
|
|
expect(!return_type_hint("auto x = f(1", 10, "int", whole_document()).has_value());
|
|
expect(!return_type_hint(text, 10, "int", {{0, 0}, {0, 5}}).has_value());
|
|
const auto bounded = return_type_hint(text, 10, std::string(300, 't'), whole_document());
|
|
require(bounded.has_value());
|
|
expect(bounded->label.size() == 128);
|
|
}
|