Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 08dc0e2

Browse files
committed
feat: Render hover actions for closure captures and sig
1 parent 2f8cd66 commit 08dc0e2

File tree

5 files changed

+131
-16
lines changed

5 files changed

+131
-16
lines changed

crates/hir-ty/src/infer/closure.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ impl CapturedItem {
166166
self.place.local
167167
}
168168

169+
pub fn ty(&self, subst: &Substitution) -> Ty {
170+
self.ty.clone().substitute(Interner, utils::ClosureSubst(subst).parent_subst())
171+
}
172+
169173
pub fn kind(&self) -> CaptureKind {
170174
self.kind
171175
}

crates/hir/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,20 @@ impl Closure {
32213221
.collect()
32223222
}
32233223

3224+
pub fn capture_types(&self, db: &dyn HirDatabase) -> Vec<Type> {
3225+
let owner = db.lookup_intern_closure((self.id).into()).0;
3226+
let infer = &db.infer(owner);
3227+
let (captures, _) = infer.closure_info(&self.id);
3228+
captures
3229+
.iter()
3230+
.cloned()
3231+
.map(|capture| Type {
3232+
env: db.trait_environment_for_body(owner),
3233+
ty: capture.ty(&self.subst),
3234+
})
3235+
.collect()
3236+
}
3237+
32243238
pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait {
32253239
let owner = db.lookup_intern_closure((self.id).into()).0;
32263240
let infer = &db.infer(owner);

crates/ide/src/highlight_related.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ pub(crate) fn highlight_related(
5454

5555
let token = pick_best_token(syntax.token_at_offset(offset), |kind| match kind {
5656
T![?] => 4, // prefer `?` when the cursor is sandwiched like in `await$0?`
57-
T![->] | T![|] => 3,
58-
kind if kind.is_keyword() => 2,
59-
IDENT | INT_NUMBER => 1,
57+
T![->] => 4,
58+
kind if kind.is_keyword() => 3,
59+
IDENT | INT_NUMBER => 2,
60+
T![|] => 1,
6061
_ => 0,
6162
})?;
6263
// most if not all of these should be re-implemented with information seeded from hir

crates/ide/src/hover/render.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,39 @@ pub(super) fn type_info_of(
3535
_config: &HoverConfig,
3636
expr_or_pat: &Either<ast::Expr, ast::Pat>,
3737
) -> Option<HoverResult> {
38-
let TypeInfo { original, adjusted } = match expr_or_pat {
38+
let ty_info = match expr_or_pat {
3939
Either::Left(expr) => sema.type_of_expr(expr)?,
4040
Either::Right(pat) => sema.type_of_pat(pat)?,
4141
};
42-
type_info(sema, _config, original, adjusted)
42+
type_info(sema, _config, ty_info)
4343
}
4444

4545
pub(super) fn closure_expr(
4646
sema: &Semantics<'_, RootDatabase>,
4747
config: &HoverConfig,
4848
c: ast::ClosureExpr,
4949
) -> Option<HoverResult> {
50-
let ty = &sema.type_of_expr(&c.into())?.original;
50+
let ty = sema.type_of_expr(&c.into())?;
51+
closure_ty(sema, config, &ty.original)
52+
}
53+
54+
fn closure_ty(
55+
sema: &Semantics<'_, RootDatabase>,
56+
config: &HoverConfig,
57+
ty: &hir::Type,
58+
) -> Option<HoverResult> {
59+
let c = ty.as_closure()?;
5160
let layout = if config.memory_layout {
5261
ty.layout(sema.db)
5362
.map(|x| format!(" // size = {}, align = {}", x.size.bytes(), x.align.abi.bytes()))
5463
.unwrap_or_default()
5564
} else {
5665
String::default()
5766
};
58-
let c = ty.as_closure()?;
59-
let mut captures = c
60-
.captured_items(sema.db)
67+
let mut captures_rendered = c.captured_items(sema.db)
6168
.into_iter()
6269
.map(|it| {
63-
let borrow_kind= match it.kind() {
70+
let borrow_kind = match it.kind() {
6471
CaptureKind::SharedRef => "immutable borrow",
6572
CaptureKind::UniqueSharedRef => "unique immutable borrow ([read more](https://doc.rust-lang.org/stable/reference/types/closure.html#unique-immutable-borrows-in-captures))",
6673
CaptureKind::MutableRef => "mutable borrow",
@@ -69,16 +76,28 @@ pub(super) fn closure_expr(
6976
format!("* `{}` by {}", it.display_place(sema.db), borrow_kind)
7077
})
7178
.join("\n");
72-
if captures.trim().is_empty() {
73-
captures = "This closure captures nothing".to_string();
79+
if captures_rendered.trim().is_empty() {
80+
captures_rendered = "This closure captures nothing".to_string();
7481
}
82+
let mut targets: Vec<hir::ModuleDef> = Vec::new();
83+
let mut push_new_def = |item: hir::ModuleDef| {
84+
if !targets.contains(&item) {
85+
targets.push(item);
86+
}
87+
};
88+
walk_and_push_ty(sema.db, ty, &mut push_new_def);
89+
c.capture_types(sema.db).into_iter().for_each(|ty| {
90+
walk_and_push_ty(sema.db, &ty, &mut push_new_def);
91+
});
92+
7593
let mut res = HoverResult::default();
94+
res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets));
7695
res.markup = format!(
7796
"```rust\n{}{}\n{}\n```\n\n## Captures\n{}",
7897
c.display_with_id(sema.db),
7998
layout,
8099
c.display_with_impl(sema.db),
81-
captures,
100+
captures_rendered,
82101
)
83102
.into();
84103
Some(res)
@@ -522,10 +541,12 @@ pub(super) fn definition(
522541

523542
fn type_info(
524543
sema: &Semantics<'_, RootDatabase>,
525-
_config: &HoverConfig,
526-
original: hir::Type,
527-
adjusted: Option<hir::Type>,
544+
config: &HoverConfig,
545+
TypeInfo { original, adjusted }: TypeInfo,
528546
) -> Option<HoverResult> {
547+
if let Some(res) = closure_ty(sema, config, &original) {
548+
return Some(res);
549+
}
529550
let mut res = HoverResult::default();
530551
let mut targets: Vec<hir::ModuleDef> = Vec::new();
531552
let mut push_new_def = |item: hir::ModuleDef| {

crates/ide/src/hover/tests.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ fn check_hover_range(ra_fixture: &str, expect: Expect) {
114114
expect.assert_eq(hover.info.markup.as_str())
115115
}
116116

117+
fn check_hover_range_actions(ra_fixture: &str, expect: Expect) {
118+
let (analysis, range) = fixture::range(ra_fixture);
119+
let hover = analysis
120+
.hover(&HoverConfig { links_in_hover: true, ..HOVER_BASE_CONFIG }, range)
121+
.unwrap()
122+
.unwrap();
123+
expect.assert_debug_eq(&hover.info.actions);
124+
}
125+
117126
fn check_hover_range_no_results(ra_fixture: &str) {
118127
let (analysis, range) = fixture::range(ra_fixture);
119128
let hover = analysis.hover(&HOVER_BASE_CONFIG, range).unwrap();
@@ -294,6 +303,72 @@ fn main() {
294303
);
295304
}
296305

306+
#[test]
307+
fn hover_ranged_closure() {
308+
check_hover_range(
309+
r#"
310+
struct S;
311+
struct S2;
312+
fn main() {
313+
let x = &S;
314+
let y = $0|| {x; S2}$0;
315+
}
316+
"#,
317+
expect![[r#"
318+
```rust
319+
{closure#0} // size = 8, align = 8
320+
impl FnOnce() -> S2
321+
```
322+
323+
## Captures
324+
* `x` by move"#]],
325+
);
326+
check_hover_range_actions(
327+
r#"
328+
struct S;
329+
struct S2;
330+
fn main() {
331+
let x = &S;
332+
let y = $0|| {x; S2}$0;
333+
}
334+
"#,
335+
expect![[r#"
336+
[
337+
GoToType(
338+
[
339+
HoverGotoTypeData {
340+
mod_path: "test::S2",
341+
nav: NavigationTarget {
342+
file_id: FileId(
343+
0,
344+
),
345+
full_range: 10..20,
346+
focus_range: 17..19,
347+
name: "S2",
348+
kind: Struct,
349+
description: "struct S2",
350+
},
351+
},
352+
HoverGotoTypeData {
353+
mod_path: "test::S",
354+
nav: NavigationTarget {
355+
file_id: FileId(
356+
0,
357+
),
358+
full_range: 0..9,
359+
focus_range: 7..8,
360+
name: "S",
361+
kind: Struct,
362+
description: "struct S",
363+
},
364+
},
365+
],
366+
),
367+
]
368+
"#]],
369+
);
370+
}
371+
297372
#[test]
298373
fn hover_shows_long_type_of_an_expression() {
299374
check(

0 commit comments

Comments
 (0)