Skip to content

Commit edd60f7

Browse files
committed
Simplify bind pat filtering
1 parent 730286b commit edd60f7

File tree

1 file changed

+32
-71
lines changed

1 file changed

+32
-71
lines changed

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! fn f(a: i32, b: i32) -> i32 { a + b }
44
//! let _x /* i32 */= f(4, 4);
55
//! ```
6-
use hir::{Semantics, TypeInfo};
6+
use hir::Semantics;
77
use ide_db::{base_db::FileId, famous_defs::FamousDefs, RootDatabase};
88

99
use itertools::Itertools;
@@ -28,11 +28,41 @@ pub(super) fn hints(
2828
return None;
2929
}
3030

31+
let parent = pat.syntax().parent()?;
32+
let type_ascriptable = match_ast! {
33+
match parent {
34+
ast::Param(it) => {
35+
if it.ty().is_some() {
36+
return None;
37+
}
38+
Some(it.colon_token())
39+
},
40+
ast::LetStmt(it) => {
41+
if config.hide_closure_initialization_hints {
42+
if let Some(ast::Expr::ClosureExpr(closure)) = it.initializer() {
43+
if closure_has_block_body(&closure) {
44+
return None;
45+
}
46+
}
47+
}
48+
if it.ty().is_some() {
49+
return None;
50+
}
51+
Some(it.colon_token())
52+
},
53+
_ => None
54+
}
55+
};
56+
3157
let descended = sema.descend_node_into_attributes(pat.clone()).pop();
3258
let desc_pat = descended.as_ref().unwrap_or(pat);
3359
let ty = sema.type_of_binding_in_pat(desc_pat)?;
3460

35-
if should_not_display_type_hint(sema, config, pat, &ty) {
61+
if ty.is_unknown() {
62+
return None;
63+
}
64+
65+
if sema.resolve_bind_pat_to_const(pat).is_some() {
3666
return None;
3767
}
3868

@@ -44,11 +74,6 @@ pub(super) fn hints(
4474
return None;
4575
}
4676

47-
let type_ascriptable = desc_pat.syntax().parent().and_then(|it| {
48-
ast::LetStmt::cast(it.clone())
49-
.map(|it| it.colon_token())
50-
.or_else(|| ast::Param::cast(it).map(|it| it.colon_token()))
51-
});
5277
let text_edit = if let Some(colon_token) = &type_ascriptable {
5378
ty_to_text_edit(
5479
sema,
@@ -89,57 +114,6 @@ pub(super) fn hints(
89114
Some(())
90115
}
91116

92-
fn should_not_display_type_hint(
93-
sema: &Semantics<'_, RootDatabase>,
94-
config: &InlayHintsConfig,
95-
bind_pat: &ast::IdentPat,
96-
pat_ty: &hir::Type,
97-
) -> bool {
98-
let db = sema.db;
99-
100-
if pat_ty.is_unknown() {
101-
return true;
102-
}
103-
104-
if sema.resolve_bind_pat_to_const(bind_pat).is_some() {
105-
return true;
106-
}
107-
108-
for node in bind_pat.syntax().ancestors() {
109-
match_ast! {
110-
match node {
111-
ast::LetStmt(it) => {
112-
if config.hide_closure_initialization_hints {
113-
if let Some(ast::Expr::ClosureExpr(closure)) = it.initializer() {
114-
if closure_has_block_body(&closure) {
115-
return true;
116-
}
117-
}
118-
}
119-
return it.ty().is_some()
120-
},
121-
// FIXME: We might wanna show type hints in parameters for non-top level patterns as well
122-
ast::Param(it) => return it.ty().is_some(),
123-
ast::MatchArm(_) => return pat_is_enum_variant(db, bind_pat, pat_ty),
124-
ast::LetExpr(_) => return pat_is_enum_variant(db, bind_pat, pat_ty),
125-
ast::IfExpr(_) => return false,
126-
ast::WhileExpr(_) => return false,
127-
ast::ForExpr(it) => {
128-
// We *should* display hint only if user provided "in {expr}" and we know the type of expr (and it's not unit).
129-
// Type of expr should be iterable.
130-
return it.in_token().is_none() ||
131-
it.iterable()
132-
.and_then(|iterable_expr| sema.type_of_expr(&iterable_expr))
133-
.map(TypeInfo::original)
134-
.map_or(true, |iterable_ty| iterable_ty.is_unknown() || iterable_ty.is_unit())
135-
},
136-
_ => (),
137-
}
138-
}
139-
}
140-
false
141-
}
142-
143117
fn is_named_constructor(
144118
sema: &Semantics<'_, RootDatabase>,
145119
pat: &ast::IdentPat,
@@ -193,19 +167,6 @@ fn is_named_constructor(
193167
(ctor_name == ty_name).then_some(())
194168
}
195169

196-
fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir::Type) -> bool {
197-
if let Some(hir::Adt::Enum(enum_data)) = pat_ty.as_adt() {
198-
let pat_text = bind_pat.to_string();
199-
enum_data
200-
.variants(db)
201-
.into_iter()
202-
.map(|variant| variant.name(db).to_smol_str())
203-
.any(|enum_name| enum_name == pat_text)
204-
} else {
205-
false
206-
}
207-
}
208-
209170
#[cfg(test)]
210171
mod tests {
211172
// This module also contains tests for super::closure_ret

0 commit comments

Comments
 (0)