Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
994 changes: 879 additions & 115 deletions docs/en/features/inlay-hints.md

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions docs/en/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,66 @@ Interval for re-checking the compilation database file.

Interval for sweeping workspace files for on-disk changes.

## Inlay Hints

The `[inlay_hints]` section controls which inlay hint categories the server produces. Configuration changes take effect after a server restart; a client-side refresh then requests hints with the updated values. No recompile is involved.

### `inlay_hints.enabled`

| Type | Default |
| ------ | ------- |
| `bool` | `true` |

Master switch: `false` disables all inlay hints.

### `inlay_hints.parameters`

| Type | Default |
| ------ | ------- |
| `bool` | `true` |

Parameter name hints at call sites, e.g. `draw(width: 800, height: 600)`, including `&` markers for arguments passed by mutable reference.

### `inlay_hints.deduced_types`

| Type | Default |
| ------ | ------- |
| `bool` | `true` |

Deduced type hints for `auto` variables, structured bindings, and deduced return types.

### `inlay_hints.designators`

| Type | Default |
| ------ | ------- |
| `bool` | `true` |

Field designator hints in aggregate initialization, e.g. `Point{.x=1, .y=2}` for `Point{1, 2}`.

### `inlay_hints.block_end`

| Type | Default |
| ------ | ------- |
| `bool` | `false` |

`// name` hints after the closing brace of long blocks (functions, types, namespaces, control flow).

### `inlay_hints.default_arguments`

| Type | Default |
| ------ | ------- |
| `bool` | `false` |

Show the default arguments a call omitted, abbreviated when long.

### `inlay_hints.type_name_limit`

| Type | Default |
| -------- | ------- |
| `uint32` | `32` |

Maximum length for printed type names; longer types fall back to a sugared spelling or are dropped. `0` means no limit.
Comment thread
16bit-ykiko marked this conversation as resolved.
Resolved

## Rules

`[[rules]]` is an array of rule objects. Rules are matched in declaration order — later rules override earlier ones.
Expand Down
107 changes: 81 additions & 26 deletions src/feature/inlay_hints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#include "semantic/decls.h"
#include "semantic/display.h"
#include "semantic/filtered_ast_visitor.h"
#include "semantic/resolver.h"
#include "semantic/types.h"

#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Casting.h"
#include "clang/Lex/Lexer.h"
#include "clang-tidy/utils/DesignatedInitializers.h"

namespace clice::feature {

Expand Down Expand Up @@ -73,15 +75,37 @@ bool is_setter(const clang::FunctionDecl* callee,

// Checks if the callee is one of the builtins
// addressof, as_const, forward, move(_if_noexcept)
//
// The real criterion is "so common and so obvious that a parameter hint is
// pure noise", and neither the builtin-ID check nor the name fallback below
// expresses it well: the ID set is whatever clang happens to fold, and the
// single-parameter guard only shields the std::move algorithm overload.
// FIXME: Replace both with a curated suppression table (qualified name +
// signature shape) so the set is explicit and extensible beyond cast-like
// single-parameter forms.
bool is_simple_builtin(const clang::FunctionDecl* callee) {
switch(callee->getBuiltinID()) {
case clang::Builtin::BIaddressof:
case clang::Builtin::BIas_const:
case clang::Builtin::BIforward:
case clang::Builtin::BIforward_like:
case clang::Builtin::BImove:
case clang::Builtin::BImove_if_noexcept: return true;
default: return false;
default: break;
}

// Freestanding compiles (-ffreestanding) strip library-builtin IDs, so
// match the same set by name; their arguments stay uninteresting either
// way. All of these are single-parameter cast-like forms — the check
// must not swallow e.g. the three-argument std::move algorithm, whose
// parameters are worth hinting.
if(callee->getNumParams() == 1 && callee->isInStdNamespace()) {
llvm::StringRef name = display::identifier_of(callee);
return name == "addressof" || name == "as_const" || name == "forward" ||
name == "forward_like" || name == "move" || name == "move_if_noexcept";
}

return false;
}

struct Callee {
Expand Down Expand Up @@ -358,9 +382,10 @@ class Builder {
clang::CharSourceRange::getTokenRange(params[i]->getDefaultArgRange()),
unit.context().getSourceManager(),
unit.lang_options());
const auto abbrev =
(text.size() > options.type_name_limit || text.contains("\n")) ? "..."
: text;
// type_name_limit = 0 means unlimited, as for type hints.
const bool too_long =
options.type_name_limit && text.size() > options.type_name_limit;
const auto abbrev = (too_long || text.contains("\n")) ? "..." : text;
if(name_hint) {
formatted_default_args.emplace_back(std::format("{0}: {1}", name, abbrev));
} else {
Expand All @@ -383,7 +408,8 @@ class Builder {
llvm::ListSeparator sep(", ");
for(auto&& element: formatted_default_args) {
os << sep;
if(hint.size() + element.size() >= options.type_name_limit) {
if(options.type_name_limit &&
hint.size() + element.size() >= options.type_name_limit) {
os << "...";
break;
}
Expand Down Expand Up @@ -508,6 +534,23 @@ class Builder {
/*Suffix=*/"=");
}

/// Hint the unwritten designators of a syntactic init list, e.g. `.x`
/// for the `1` in `Point{1}`. Explicitly written designators and inits
/// already carrying a `/*name=*/` comment stay bare.
void add_designators(const clang::InitListExpr* syntactic) {
auto designators = clang::tidy::utils::getUnwrittenDesignators(syntactic);
for(const clang::Expr* init: syntactic->inits()) {
if(llvm::isa<clang::DesignatedInitExpr>(init)) {
continue;
}
auto it = designators.find(init->getBeginLoc());
if(it == designators.end() || has_param_name_comment(init, it->second)) {
continue;
}
add_designator_hint(init->getSourceRange(), it->second);
}
}

void add_return_type_hint(clang::FunctionDecl* decl, clang::SourceRange range) {
auto* type = decl->getReturnType()->getContainedAutoType();
if(!type || type->getDeducedType().isNull()) {
Expand Down Expand Up @@ -692,7 +735,7 @@ class Visitor : public FilteredASTVisitor<Visitor> {
}

bool VisitCallExpr(clang::CallExpr* expr) {
if(!options.parameters) {
if(!options.parameters && !options.default_arguments) {
Comment thread
16bit-ykiko marked this conversation as resolved.
Resolved
return true;
}

Expand All @@ -714,17 +757,35 @@ class Visitor : public FilteredASTVisitor<Visitor> {
llvm::isa<clang::UserDefinedLiteral>(expr))
return true;

/// FIXME: Use template resolver here.
Callee callee;
bool dependent_callee = false;
if(expr->isTypeDependent() || expr->isValueDependent()) {
return true;
}

auto callee_decl = expr->getCalleeDecl();
dependent_callee = true;
// A dependent call has no resolved callee. The template
// resolver's arity-filtered candidate set stands in; only a
// unique candidate gives trustworthy parameter names — with
// several overloads left we must not pick one arbitrarily.
auto candidates = unit.resolver().lookup(expr);
if(candidates.size() != 1) {
return true;
Comment thread
16bit-ykiko marked this conversation as resolved.
Resolved
}

Callee callee;
if(const auto* FD = llvm::dyn_cast<clang::FunctionDecl>(callee_decl)) {
auto* target = candidates.front();
if(auto* shadow = dyn_cast<clang::UsingShadowDecl>(target)) {
target = shadow->getTargetDecl();
}
if(auto* FTD = dyn_cast<clang::FunctionTemplateDecl>(target)) {
callee.decl = FTD->getTemplatedDecl();
} else {
callee.decl = dyn_cast<clang::FunctionDecl>(target);
}
if(!callee.decl) {
return true;
}
} else if(const auto* FD = dyn_cast_or_null<clang::FunctionDecl>(expr->getCalleeDecl())) {
callee.decl = FD;
} else if(const auto* FTD = llvm::dyn_cast<clang::FunctionTemplateDecl>(callee_decl)) {
} else if(const auto* FTD =
dyn_cast_or_null<clang::FunctionTemplateDecl>(expr->getCalleeDecl())) {
callee.decl = FTD->getTemplatedDecl();
} else if(clang::FunctionProtoTypeLoc loc = decls::proto_type_loc(expr->getCallee())) {
callee.loc = loc;
Expand All @@ -744,9 +805,12 @@ class Visitor : public FilteredASTVisitor<Visitor> {
// this correspondence...
llvm::ArrayRef<const clang::Expr*> args = {expr->getArgs(), expr->getNumArgs()};

// We don't have the implied object argument through a function pointer either.
// We don't have the implied object argument through a function pointer
// either, nor in a dependent member call: there the object stays the
// member expression's base and never joins the argument list.
if(const auto* method = llvm::dyn_cast_or_null<clang::CXXMethodDecl>(callee.decl)) {
if(is_functor || method->hasCXXExplicitFunctionObjectParameter()) {
if(is_functor ||
(!dependent_callee && method->hasCXXExplicitFunctionObjectParameter())) {
args = args.drop_front(1);
}
}
Expand Down Expand Up @@ -850,16 +914,7 @@ class Visitor : public FilteredASTVisitor<Visitor> {
return true;
}

/// FIXME:
// llvm::DenseMap<SourceLocation, std::string> Designators =
// tidy::utils::getUnwrittenDesignators(Syn);
// for(const Expr* Init: Syn->inits()) {
// if(llvm::isa<DesignatedInitExpr>(Init))
// continue;
// auto It = Designators.find(Init->getBeginLoc());
// if(It != Designators.end() && !isPrecededByParamNameComment(Init, It->second))
// addDesignatorHint(Init->getSourceRange(), It->second);
// }
builder.add_designators(expr);
return true;
}

Expand Down
14 changes: 14 additions & 0 deletions src/server/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,20 @@ Compiler::RawResult Compiler::forward_query(worker::QueryKind kind,
wp.kind = kind;
wp.path = path;

if(kind == worker::QueryKind::InlayHints) {
// apply_defaults() has filled every field of the config section.
auto& ih = workspace.config.inlay_hints;
wp.inlay_options = {
.enabled = *ih.enabled,
.parameters = *ih.parameters,
.deduced_types = *ih.deduced_types,
.designators = *ih.designators,
.block_end = *ih.block_end,
.default_arguments = *ih.default_arguments,
.type_name_limit = *ih.type_name_limit,
};
}

if(position) {
wp.offset = clamped_offset(map, *position);
}
Expand Down
6 changes: 6 additions & 0 deletions src/server/protocol/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "compile/dep_file.h"
#include "feature/document_link.h"
#include "feature/feature.h"
#include "syntax/token.h"

#include "kota/codec/json/json.h"
Expand Down Expand Up @@ -100,6 +101,11 @@ struct QueryParams {
std::string path;
uint32_t offset = 0; ///< Byte offset for position-sensitive queries (Hover, GoToDefinition).
LocalSourceRange range; ///< Byte range for range-sensitive queries (InlayHints).

/// Options for InlayHints queries, resolved from the workspace config by
/// the master per query — the worker holds no config state and a config
/// change simply shows up on the next request.
feature::InlayHintsOptions inlay_options;
};

/// Parameters for stateful compilation (builds AST, publishes diagnostics).
Expand Down
19 changes: 19 additions & 0 deletions src/server/state/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>

#include "feature/feature.h"
#include "support/filesystem.h"
#include "support/glob_pattern.h"
#include "support/logging.h"
Expand Down Expand Up @@ -96,6 +97,24 @@ void Config::apply_defaults(llvm::StringRef workspace_root) {
if(!t.workspace_poll_seconds)
t.workspace_poll_seconds = 30;

// The feature options struct is the single source of the defaults.
auto& ih = inlay_hints;
feature::InlayHintsOptions inlay_defaults;
if(!ih.enabled)
ih.enabled = inlay_defaults.enabled;
if(!ih.parameters)
ih.parameters = inlay_defaults.parameters;
if(!ih.deduced_types)
ih.deduced_types = inlay_defaults.deduced_types;
if(!ih.designators)
ih.designators = inlay_defaults.designators;
if(!ih.block_end)
ih.block_end = inlay_defaults.block_end;
if(!ih.default_arguments)
ih.default_arguments = inlay_defaults.default_arguments;
if(!ih.type_name_limit)
ih.type_name_limit = inlay_defaults.type_name_limit;

if(p.cache_dir.empty() && !workspace_root.empty()) {
p.cache_dir = resolve_xdg_cache_dir(workspace_root);
if(p.cache_dir.empty())
Expand Down
15 changes: 15 additions & 0 deletions src/server/state/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ struct ProjectConfig {
defaulted<std::uint64_t> worker_memory_limit = {};
};

/// Corresponds to the `[inlay_hints]` section in clice.toml. Mirrors
/// feature::InlayHintsOptions field by field; unset fields take that
/// struct's defaults in apply_defaults(), so the two never disagree.
struct InlayHintsConfig {
std::optional<bool> enabled;
std::optional<bool> parameters;
std::optional<bool> deduced_types;
std::optional<bool> designators;
std::optional<bool> block_end;
std::optional<bool> default_arguments;
std::optional<std::uint32_t> type_name_limit;
};

/// Corresponds to the `[tracker]` section in clice.toml: the stat-polling
/// file tracker's intervals. 0 disables the loop (integration tests drive
/// ticks through the clice/internal/poll hook instead).
Expand Down Expand Up @@ -88,6 +101,8 @@ struct Config {

defaulted<TrackerConfig> tracker;

defaulted<InlayHintsConfig> inlay_hints;

defaulted<std::vector<ConfigRule>> rules;

kota::meta::annotation<std::vector<CompiledRule>, kota::meta::attrs::skip> compiled_rules;
Expand Down
2 changes: 1 addition & 1 deletion src/server/worker/stateful_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ void StatefulWorker::register_handlers() {
range = LocalSourceRange{0, static_cast<uint32_t>(doc.text.size())};
return to_raw(feature::inlay_hints(doc.unit,
range,
{},
params.inlay_options,
feature::PositionEncoding::UTF16));
});
case K::FoldingRange:
Expand Down
Loading
Loading