Serve parameter inlay hints from occurrence signatures
A bounded core finds top-level argument starts (nested brackets, literals and comments aware) and projects parameter-type hints from the compiler signature, filtered to the requested range. The server advertises inlayHintProvider and serves textDocument/inlayHint through EditorDiagnostics. Registered tests cover splitting, filtering, short signatures and bounds; a real stdio session returned the std::string_view hint for the fnv1a64 argument. Refresh the parity matrix.
This commit is contained in:
1 parent
12a71ff692
commit
130cb57a7f
12 files changed
+360
-10
No files matched your search
@@ -266,6 +266,8 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
capabilities.emplace("selectionRangeProvider", Json{true});
|
||||
capabilities.emplace("foldingRangeProvider", Json{true});
|
||||
}
|
||||
if (diagnostics_ && diagnostics_->supports_occurrences())
|
||||
capabilities.emplace("inlayHintProvider", Json{true});
|
||||
if (diagnostics_ && diagnostics_->supports_occurrences())
|
||||
capabilities.emplace(
|
||||
"semanticTokensProvider",
|
||||
@@ -386,6 +388,32 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
}
|
||||
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
|
||||
}
|
||||
if (*method == "textDocument/inlayHint") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
|
||||
const auto *range = member(params, "range");
|
||||
const auto *start = member(range, "start");
|
||||
const auto *end = member(range, "end");
|
||||
const auto coordinate = [](const Json *value,
|
||||
std::string_view key) -> std::optional<std::uint32_t> {
|
||||
const auto *field = member(value, key);
|
||||
const auto *number = field ? std::get_if<std::int64_t>(&field->value) : nullptr;
|
||||
if (!number || *number < 0 ||
|
||||
*number > Void::LanguageServer::Documents::kMaxPositionValue)
|
||||
return std::nullopt;
|
||||
return static_cast<std::uint32_t>(*number);
|
||||
};
|
||||
const auto start_line = coordinate(start, "line"),
|
||||
start_character = coordinate(start, "character");
|
||||
const auto end_line = coordinate(end, "line"), end_character = coordinate(end, "character");
|
||||
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri) || !start_line ||
|
||||
!start_character || !end_line || !end_character)
|
||||
return error(id, -32602, "Expected textDocument.uri and a bounded range");
|
||||
return reply(id, "result",
|
||||
diagnostics_->inlay_hints(
|
||||
*uri, {{*start_line, *start_character}, {*end_line, *end_character}}));
|
||||
}
|
||||
if (*method == "textDocument/semanticTokens/full") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
#include <vls/features/InlayHints.hpp>
|
||||
|
||||
#include <vls/features/SignatureHelp.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace vls::features::inlay_hints {
|
||||
namespace {
|
||||
Position position_of(std::string_view text, std::size_t offset) {
|
||||
offset = std::min(offset, text.size());
|
||||
Position position;
|
||||
std::size_t start{};
|
||||
while (true) {
|
||||
const auto newline = text.find('\n', start);
|
||||
const auto end = newline == std::string_view::npos ? text.size() : newline;
|
||||
if (offset <= end)
|
||||
return {position.line, static_cast<std::uint32_t>(offset - start)};
|
||||
start = newline + 1;
|
||||
++position.line;
|
||||
}
|
||||
}
|
||||
|
||||
bool in_range(Position position, Range range) {
|
||||
return range.start <= position && position <= range.end;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<std::size_t> argument_starts(std::string_view text, std::size_t callee_end,
|
||||
const Limits &limits) {
|
||||
std::vector<std::size_t> starts;
|
||||
if (callee_end > text.size())
|
||||
return starts;
|
||||
const auto open = text.find('(', callee_end);
|
||||
if (open == std::string_view::npos)
|
||||
return starts;
|
||||
int depth{};
|
||||
char quote{'\0'};
|
||||
bool line_comment{};
|
||||
bool block_comment{};
|
||||
bool pending = true; // next non-space content starts an argument
|
||||
for (std::size_t index = open + 1; index < text.size(); ++index) {
|
||||
const char ch = text[index];
|
||||
const char next = index + 1 < text.size() ? text[index + 1] : '\0';
|
||||
if (line_comment) {
|
||||
if (ch == '\n')
|
||||
line_comment = false;
|
||||
continue;
|
||||
}
|
||||
if (block_comment) {
|
||||
if (ch == '*' && next == '/') {
|
||||
block_comment = false;
|
||||
++index;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (quote != '\0') {
|
||||
if (ch == '\\')
|
||||
++index;
|
||||
else if (ch == quote)
|
||||
quote = '\0';
|
||||
continue;
|
||||
}
|
||||
if (ch == '/' && next == '/') {
|
||||
line_comment = true;
|
||||
++index;
|
||||
continue;
|
||||
}
|
||||
if (ch == '/' && next == '*') {
|
||||
block_comment = true;
|
||||
++index;
|
||||
continue;
|
||||
}
|
||||
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
|
||||
continue;
|
||||
if (ch == ')' && depth == 0)
|
||||
break;
|
||||
if (ch == ',' && depth == 0) {
|
||||
pending = true;
|
||||
continue;
|
||||
}
|
||||
if (pending) {
|
||||
if (starts.size() == limits.arguments)
|
||||
break;
|
||||
starts.push_back(index);
|
||||
pending = false;
|
||||
}
|
||||
if (ch == '"' || ch == '\'')
|
||||
quote = ch;
|
||||
else if (ch == '(' || ch == '[' || ch == '{')
|
||||
++depth;
|
||||
else if ((ch == ')' || ch == ']' || ch == '}') && depth > 0)
|
||||
--depth;
|
||||
}
|
||||
return starts;
|
||||
}
|
||||
|
||||
std::vector<Hint> parameter_hints(std::span<const std::string> signatures, std::string_view text,
|
||||
std::size_t callee_end, Range requested, const Limits &limits) {
|
||||
std::vector<Hint> hints;
|
||||
const auto arguments = argument_starts(text, callee_end, limits);
|
||||
if (arguments.empty())
|
||||
return hints;
|
||||
std::vector<std::string> parameters;
|
||||
for (const auto &signature : signatures) {
|
||||
const auto parsed = signature::parse(signature);
|
||||
parameters.clear();
|
||||
for (const auto ¶meter : parsed.parameters)
|
||||
parameters.push_back(parameter.label);
|
||||
if (parameters.size() < arguments.size())
|
||||
continue;
|
||||
for (std::size_t index = 0; index != arguments.size(); ++index) {
|
||||
if (hints.size() == limits.hints)
|
||||
return hints;
|
||||
const auto position = position_of(text, arguments[index]);
|
||||
if (!in_range(position, requested))
|
||||
continue;
|
||||
std::string label = parameters[index];
|
||||
if (label.size() > limits.labelBytes)
|
||||
label.resize(limits.labelBytes);
|
||||
hints.push_back({position, label + ":", 2, false, true});
|
||||
}
|
||||
break;
|
||||
}
|
||||
return hints;
|
||||
}
|
||||
|
||||
} // namespace vls::features::inlay_hints
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <vls/features/Completion.hpp>
|
||||
#include <vls/features/DocumentSymbols.hpp>
|
||||
#include <vls/features/IndexNavigation.hpp>
|
||||
#include <vls/features/InlayHints.hpp>
|
||||
#include <vls/features/SemanticTokens.hpp>
|
||||
#include <vls/features/SignatureHelp.hpp>
|
||||
#include <vls/features/TextStructure.hpp>
|
||||
@@ -1199,6 +1200,59 @@ Json EditorDiagnostics::semantic_tokens(std::string_view uri) const {
|
||||
return Json{Json::Object{{"data", Json{std::move(data)}}}};
|
||||
}
|
||||
|
||||
Json EditorDiagnostics::inlay_hints(std::string_view uri, Documents::Range range) const {
|
||||
auto &s = *state_;
|
||||
auto key = Documents::client_uri_key(uri);
|
||||
std::shared_ptr<const Receipt> receipt;
|
||||
std::shared_ptr<const std::vector<Occurrence>> facts;
|
||||
{
|
||||
std::lock_guard lock{s.mutex};
|
||||
const auto i = key ? s.entries.find(*key) : s.entries.end();
|
||||
if (i == s.entries.end())
|
||||
return Json{Json::Array{}};
|
||||
receipt = i->second.receipt;
|
||||
facts = i->second.occurrences;
|
||||
}
|
||||
if (!receipt || !facts || !s.valid(*receipt))
|
||||
return Json{Json::Array{}};
|
||||
const Documents::DocumentSnapshot *snapshot = nullptr;
|
||||
for (const auto &entry : receipt->capture->entries())
|
||||
if (entry.snapshot && entry.snapshot->identity.canonical_uri == *key) {
|
||||
snapshot = entry.snapshot.get();
|
||||
break;
|
||||
}
|
||||
if (!snapshot)
|
||||
return Json{Json::Array{}};
|
||||
std::vector<features::inlay_hints::Hint> hints;
|
||||
const features::inlay_hints::Range requested{{range.start.line, range.start.character},
|
||||
{range.end.line, range.end.character}};
|
||||
for (const auto &occurrence : *facts) {
|
||||
if (occurrence.use_file.get() != snapshot || occurrence.signature.empty())
|
||||
continue;
|
||||
const auto use = snapshot->range_to_offsets(occurrence.use);
|
||||
if (!use)
|
||||
continue;
|
||||
const std::vector<std::string> signatures{occurrence.signature};
|
||||
const auto produced = features::inlay_hints::parameter_hints(signatures, snapshot->text,
|
||||
use->second, requested);
|
||||
hints.insert(hints.end(), produced.begin(), produced.end());
|
||||
}
|
||||
std::ranges::sort(hints);
|
||||
const auto unique = std::ranges::unique(hints);
|
||||
hints.erase(unique.begin(), unique.end());
|
||||
Json::Array result;
|
||||
for (const auto &hint : hints) {
|
||||
if (result.size() == 256)
|
||||
break;
|
||||
result.push_back(
|
||||
Json{Json::Object{{"position", position({hint.position.line, hint.position.character})},
|
||||
{"label", Json{hint.label}},
|
||||
{"kind", Json{static_cast<std::int64_t>(hint.kind)}},
|
||||
{"paddingRight", Json{hint.padding_right}}}});
|
||||
}
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
|
||||
void EditorDiagnostics::set_transactional_rename(bool supported) {
|
||||
std::lock_guard lock{state_->mutex};
|
||||
state_->transactional_rename = supported;
|
||||
|
||||
Reference in new issue
Block a user