Skip to content

Commit 276687a

Browse files
committed
internal: Directly use self param in completions instead of searching
1 parent 60dfe8c commit 276687a

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

crates/hir/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,19 @@ impl SelfParam {
14831483
.and_then(|params| params.self_param())
14841484
.map(|value| InFile { file_id, value })
14851485
}
1486+
1487+
pub fn ty(&self, db: &dyn HirDatabase) -> Type {
1488+
let resolver = self.func.resolver(db.upcast());
1489+
let krate = self.func.lookup(db.upcast()).container.module(db.upcast()).krate();
1490+
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1491+
let environment = db.trait_environment(self.func.into());
1492+
1493+
Type {
1494+
krate,
1495+
env: environment.clone(),
1496+
ty: ctx.lower_ty(&db.function_data(self.func).params[0].1),
1497+
}
1498+
}
14861499
}
14871500

14881501
impl HasVisibility for Function {

crates/ide_completion/src/completions/dot.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Completes references after dot (fields and method calls).
22
33
use either::Either;
4-
use hir::ScopeDef;
54
use rustc_hash::FxHashSet;
65

76
use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
@@ -36,24 +35,22 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
3635
if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
3736
return;
3837
}
39-
ctx.scope.process_all_names(&mut |name, def| {
40-
if let ScopeDef::Local(local) = &def {
41-
if local.is_self(ctx.db) {
42-
let ty = local.ty(ctx.db);
43-
complete_fields(ctx, &ty, |field, ty| match field {
44-
either::Either::Left(field) => {
45-
acc.add_field(ctx, Some(name.clone()), field, &ty)
46-
}
47-
either::Either::Right(tuple_idx) => {
48-
acc.add_tuple_field(ctx, Some(name.clone()), tuple_idx, &ty)
49-
}
50-
});
51-
complete_methods(ctx, &ty, |func| {
52-
acc.add_method(ctx, func, Some(name.clone()), None)
53-
});
54-
}
38+
if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
39+
if let Some(self_) = func.self_param(ctx.db) {
40+
let ty = self_.ty(ctx.db);
41+
complete_fields(ctx, &ty, |field, ty| match field {
42+
either::Either::Left(field) => {
43+
acc.add_field(ctx, Some(hir::known::SELF_PARAM), field, &ty)
44+
}
45+
either::Either::Right(tuple_idx) => {
46+
acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), tuple_idx, &ty)
47+
}
48+
});
49+
complete_methods(ctx, &ty, |func| {
50+
acc.add_method(ctx, func, Some(hir::known::SELF_PARAM), None)
51+
});
5552
}
56-
});
53+
}
5754
}
5855

5956
fn complete_fields(

0 commit comments

Comments
 (0)