Skip to content

Commit 1ad58a4

Browse files
committed
Auto merge of rust-lang#14711 - Veykril:highlight-captures, r=Veykril
feat: Highlight closure captures when cursor is on pipe or move keyword This runs into the same issue on vscode as exit points for `->`, where highlights are only triggered on identifiers, rust-lang/rust-analyzer#9395 Though putting the cursor on `move` should at least work.
2 parents 98e76bd + 5a97a32 commit 1ad58a4

File tree

10 files changed

+178
-78
lines changed

10 files changed

+178
-78
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub(crate) struct HirPlace {
118118
pub(crate) local: BindingId,
119119
pub(crate) projections: Vec<ProjectionElem<Infallible, Ty>>,
120120
}
121+
121122
impl HirPlace {
122123
fn ty(&self, ctx: &mut InferenceContext<'_>) -> Ty {
123124
let mut ty = ctx.table.resolve_completely(ctx.result[self.local].clone());
@@ -161,6 +162,10 @@ pub struct CapturedItem {
161162
}
162163

163164
impl CapturedItem {
165+
pub fn local(&self) -> BindingId {
166+
self.place.local
167+
}
168+
164169
pub fn display_kind(&self) -> &'static str {
165170
match self.kind {
166171
CaptureKind::ByRef(k) => match k {

crates/hir/src/lib.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -3209,11 +3209,11 @@ impl Closure {
32093209
self.clone().as_ty().display(db).with_closure_style(ClosureStyle::ImplFn).to_string()
32103210
}
32113211

3212-
pub fn captured_items(&self, db: &dyn HirDatabase) -> Vec<hir_ty::CapturedItem> {
3212+
pub fn captured_items(&self, db: &dyn HirDatabase) -> Vec<ClosureCapture> {
32133213
let owner = db.lookup_intern_closure((self.id).into()).0;
32143214
let infer = &db.infer(owner);
32153215
let info = infer.closure_info(&self.id);
3216-
info.0.clone()
3216+
info.0.iter().cloned().map(|capture| ClosureCapture { owner, capture }).collect()
32173217
}
32183218

32193219
pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait {
@@ -3224,6 +3224,26 @@ impl Closure {
32243224
}
32253225
}
32263226

3227+
#[derive(Clone, Debug, PartialEq, Eq)]
3228+
pub struct ClosureCapture {
3229+
owner: DefWithBodyId,
3230+
capture: hir_ty::CapturedItem,
3231+
}
3232+
3233+
impl ClosureCapture {
3234+
pub fn local(&self) -> Local {
3235+
Local { parent: self.owner, binding_id: self.capture.local() }
3236+
}
3237+
3238+
pub fn display_kind(&self) -> &'static str {
3239+
self.capture.display_kind()
3240+
}
3241+
3242+
pub fn display_place(&self, owner: ClosureId, db: &dyn HirDatabase) -> String {
3243+
self.capture.display_place(owner, db)
3244+
}
3245+
}
3246+
32273247
#[derive(Clone, PartialEq, Eq, Debug)]
32283248
pub struct Type {
32293249
env: Arc<TraitEnvironment>,

0 commit comments

Comments
 (0)