Query for nearest parent block around the hint to resolve

This way, parameter hints will be found for resolution
This commit is contained in:
Kirill Bulatov 2023-12-11 14:50:41 +02:00
parent 457b966b17
commit be6d34b810
4 changed files with 30 additions and 20 deletions

View file

@ -422,6 +422,11 @@ fn ty_to_text_edit(
Some(builder.finish())
}
pub enum RangeLimit {
Fixed(TextRange),
NearestParentBlock(TextSize),
}
// Feature: Inlay Hints
//
// rust-analyzer shows additional information inline with the source code.
@ -443,7 +448,7 @@ fn ty_to_text_edit(
pub(crate) fn inlay_hints(
db: &RootDatabase,
file_id: FileId,
range_limit: Option<TextRange>,
range_limit: Option<RangeLimit>,
config: &InlayHintsConfig,
) -> Vec<InlayHint> {
let _p = profile::span("inlay_hints");
@ -458,13 +463,23 @@ pub(crate) fn inlay_hints(
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
match range_limit {
Some(range) => match file.covering_element(range) {
Some(RangeLimit::Fixed(range)) => match file.covering_element(range) {
NodeOrToken::Token(_) => return acc,
NodeOrToken::Node(n) => n
.descendants()
.filter(|descendant| range.intersect(descendant.text_range()).is_some())
.for_each(hints),
},
Some(RangeLimit::NearestParentBlock(position)) => {
match file
.token_at_offset(position)
.left_biased()
.and_then(|token| token.parent_ancestors().find_map(ast::BlockExpr::cast))
{
Some(parent_block) => parent_block.syntax().descendants().for_each(hints),
None => return acc,
}
}
None => file.descendants().for_each(hints),
};
}