Skip to content

Commit d87a01b

Browse files
committed
Clean up inlay_hints
1 parent e106857 commit d87a01b

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

crates/assists/src/utils.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub(crate) mod insert_use;
33

44
use std::{iter, ops};
55

6-
use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type};
6+
use hir::{Adt, Crate, Enum, Module, ScopeDef, Semantics, Trait, Type};
77
use ide_db::RootDatabase;
88
use itertools::Itertools;
99
use rustc_hash::FxHashSet;
@@ -100,7 +100,7 @@ pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor
100100
escape(&mut placeholder);
101101
let tab_stop = match cursor {
102102
Cursor::Replace(placeholder) => format!("${{0:{}}}", placeholder),
103-
Cursor::Before(placeholder) => format!("$0{}", placeholder),
103+
Cursor::Before(placeholder) => format!("{}", placeholder),
104104
};
105105

106106
let mut buf = node.to_string();
@@ -373,6 +373,10 @@ pub use prelude::*;
373373
self.find_trait("core:iter:traits:iterator:Iterator")
374374
}
375375

376+
pub fn core_iter(&self) -> Option<Module> {
377+
self.find_module("core:iter")
378+
}
379+
376380
fn find_trait(&self, path: &str) -> Option<Trait> {
377381
match self.find_def(path)? {
378382
hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
@@ -387,6 +391,13 @@ pub use prelude::*;
387391
}
388392
}
389393

394+
fn find_module(&self, path: &str) -> Option<Module> {
395+
match self.find_def(path)? {
396+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
397+
_ => None,
398+
}
399+
}
400+
390401
fn find_def(&self, path: &str) -> Option<ScopeDef> {
391402
let db = self.0.db;
392403
let mut path = path.split(':');

crates/ide/src/inlay_hints.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use assists::utils::FamousDefs;
2-
use hir::{known, Adt, AssocItem, Callable, HirDisplay, Semantics, Type};
2+
use hir::{known, HirDisplay, Semantics};
33
use ide_db::RootDatabase;
44
use stdx::to_lower_snake_case;
55
use syntax::{
@@ -120,7 +120,7 @@ fn get_chaining_hints(
120120
return None;
121121
}
122122
if matches!(expr, ast::Expr::PathExpr(_)) {
123-
if let Some(Adt::Struct(st)) = ty.as_adt() {
123+
if let Some(hir::Adt::Struct(st)) = ty.as_adt() {
124124
if st.fields(sema.db).is_empty() {
125125
return None;
126126
}
@@ -208,7 +208,7 @@ fn get_bind_pat_hints(
208208
fn hint_iterator(
209209
sema: &Semantics<RootDatabase>,
210210
config: &InlayHintsConfig,
211-
ty: &Type,
211+
ty: &hir::Type,
212212
) -> Option<SmolStr> {
213213
let db = sema.db;
214214
let strukt = std::iter::successors(Some(ty.clone()), |ty| ty.remove_ref())
@@ -218,17 +218,13 @@ fn hint_iterator(
218218
if krate.declaration_name(db).as_deref() != Some("core") {
219219
return None;
220220
}
221-
// assert this type comes from `core::iter`
222-
strukt
223-
.module(db)
224-
.path_to_root(db)
225-
.into_iter()
226-
.rev()
227-
.find(|module| module.name(db) == Some(known::iter))?;
228221
let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?;
222+
let iter_mod = FamousDefs(sema, krate).core_iter()?;
223+
// assert this type comes from `core::iter`
224+
iter_mod.visibility_of(db, &iter_trait.into()).filter(|&vis| vis == hir::Visibility::Public)?;
229225
if ty.impls_trait(db, iter_trait, &[]) {
230226
let assoc_type_item = iter_trait.items(db).into_iter().find_map(|item| match item {
231-
AssocItem::TypeAlias(alias) if alias.name(db) == known::Item => Some(alias),
227+
hir::AssocItem::TypeAlias(alias) if alias.name(db) == known::Item => Some(alias),
232228
_ => None,
233229
})?;
234230
if let Some(ty) = ty.normalize_trait_assoc_type(db, iter_trait, &[], assoc_type_item) {
@@ -248,8 +244,8 @@ fn hint_iterator(
248244
None
249245
}
250246

251-
fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &Type) -> bool {
252-
if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() {
247+
fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir::Type) -> bool {
248+
if let Some(hir::Adt::Enum(enum_data)) = pat_ty.as_adt() {
253249
let pat_text = bind_pat.to_string();
254250
enum_data
255251
.variants(db)
@@ -264,15 +260,15 @@ fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &Typ
264260
fn should_not_display_type_hint(
265261
sema: &Semantics<RootDatabase>,
266262
bind_pat: &ast::IdentPat,
267-
pat_ty: &Type,
263+
pat_ty: &hir::Type,
268264
) -> bool {
269265
let db = sema.db;
270266

271267
if pat_ty.is_unknown() {
272268
return true;
273269
}
274270

275-
if let Some(Adt::Struct(s)) = pat_ty.as_adt() {
271+
if let Some(hir::Adt::Struct(s)) = pat_ty.as_adt() {
276272
if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() {
277273
return true;
278274
}
@@ -316,7 +312,7 @@ fn should_not_display_type_hint(
316312

317313
fn should_show_param_name_hint(
318314
sema: &Semantics<RootDatabase>,
319-
callable: &Callable,
315+
callable: &hir::Callable,
320316
param_name: &str,
321317
argument: &ast::Expr,
322318
) -> bool {
@@ -363,7 +359,7 @@ fn is_enum_name_similar_to_param_name(
363359
param_name: &str,
364360
) -> bool {
365361
match sema.type_of_expr(argument).and_then(|t| t.as_adt()) {
366-
Some(Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
362+
Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
367363
_ => false,
368364
}
369365
}
@@ -384,7 +380,7 @@ fn is_obvious_param(param_name: &str) -> bool {
384380
param_name.len() == 1 || is_obvious_param_name
385381
}
386382

387-
fn get_callable(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<Callable> {
383+
fn get_callable(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Callable> {
388384
match expr {
389385
ast::Expr::CallExpr(expr) => sema.type_of_expr(&expr.expr()?)?.as_callable(sema.db),
390386
ast::Expr::MethodCallExpr(expr) => sema.resolve_method_call_as_callable(expr),

0 commit comments

Comments
 (0)