Skip to content

feat: Highlight closure captures when cursor is on pipe or move keyword #14711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions crates/hir-ty/src/infer/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub(crate) struct HirPlace {
pub(crate) local: BindingId,
pub(crate) projections: Vec<ProjectionElem<Infallible, Ty>>,
}

impl HirPlace {
fn ty(&self, ctx: &mut InferenceContext<'_>) -> Ty {
let mut ty = ctx.table.resolve_completely(ctx.result[self.local].clone());
Expand Down Expand Up @@ -161,6 +162,10 @@ pub struct CapturedItem {
}

impl CapturedItem {
pub fn local(&self) -> BindingId {
self.place.local
}

pub fn display_kind(&self) -> &'static str {
match self.kind {
CaptureKind::ByRef(k) => match k {
Expand Down
24 changes: 22 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3209,11 +3209,11 @@ impl Closure {
self.clone().as_ty().display(db).with_closure_style(ClosureStyle::ImplFn).to_string()
}

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

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClosureCapture {
owner: DefWithBodyId,
capture: hir_ty::CapturedItem,
}

impl ClosureCapture {
pub fn local(&self) -> Local {
Local { parent: self.owner, binding_id: self.capture.local() }
}

pub fn display_kind(&self) -> &'static str {
self.capture.display_kind()
}

pub fn display_place(&self, owner: ClosureId, db: &dyn HirDatabase) -> String {
self.capture.display_place(owner, db)
}
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Type {
env: Arc<TraitEnvironment>,
Expand Down
Loading