Add return-type inlay hints after calls
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.
This commit is contained in:
1 parent
0bf5873069
commit
2cced8363d
6 files changed
+135
-7
No files matched your search
@@ -124,4 +124,71 @@ std::vector<Hint> parameter_hints(std::span<const std::string> signatures, std::
|
||||
return hints;
|
||||
}
|
||||
|
||||
std::optional<std::size_t> call_end(std::string_view text, std::size_t callee_end, const Limits &) {
|
||||
if (callee_end > text.size())
|
||||
return std::nullopt;
|
||||
const auto open = text.find('(', callee_end);
|
||||
if (open == std::string_view::npos)
|
||||
return std::nullopt;
|
||||
int depth{};
|
||||
char quote{'\0'};
|
||||
bool line_comment{};
|
||||
bool block_comment{};
|
||||
for (std::size_t index = open; 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;
|
||||
} else if (ch == '/' && next == '*') {
|
||||
block_comment = true;
|
||||
++index;
|
||||
} else if (ch == '"' || ch == '\'') {
|
||||
quote = ch;
|
||||
} else if (ch == '(') {
|
||||
++depth;
|
||||
} else if (ch == ')' && --depth == 0) {
|
||||
return index + 1;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<Hint> return_type_hint(std::string_view text, std::size_t callee_end,
|
||||
std::string_view canonical_type, Range requested,
|
||||
const Limits &limits) {
|
||||
if (canonical_type.empty())
|
||||
return std::nullopt;
|
||||
const auto end = call_end(text, callee_end);
|
||||
if (!end)
|
||||
return std::nullopt;
|
||||
const auto position = position_of(text, *end);
|
||||
if (!in_range(position, requested))
|
||||
return std::nullopt;
|
||||
std::string label = ": ";
|
||||
label += canonical_type;
|
||||
if (label.size() > limits.labelBytes)
|
||||
label.resize(limits.labelBytes);
|
||||
return Hint{position, std::move(label), 1, true, false};
|
||||
}
|
||||
|
||||
} // namespace vls::features::inlay_hints
|
||||
@@ -1324,15 +1324,20 @@ Json EditorDiagnostics::inlay_hints(std::string_view uri, Documents::Range range
|
||||
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())
|
||||
if (occurrence.use_file.get() != snapshot)
|
||||
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());
|
||||
if (!occurrence.signature.empty()) {
|
||||
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());
|
||||
}
|
||||
if (const auto returned = features::inlay_hints::return_type_hint(
|
||||
snapshot->text, use->second, occurrence.canonical_type, requested))
|
||||
hints.push_back(*returned);
|
||||
}
|
||||
std::ranges::sort(hints);
|
||||
const auto unique = std::ranges::unique(hints);
|
||||
|
||||
Reference in new issue
Block a user