11092: internal: Directly use self param in completions instead of searching r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-12-21 23:20:16 +00:00 committed by GitHub
commit d4c5bf7828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 18 deletions

View File

@ -1483,6 +1483,19 @@ pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
.and_then(|params| params.self_param()) .and_then(|params| params.self_param())
.map(|value| InFile { file_id, value }) .map(|value| InFile { file_id, value })
} }
pub fn ty(&self, db: &dyn HirDatabase) -> Type {
let resolver = self.func.resolver(db.upcast());
let krate = self.func.lookup(db.upcast()).container.module(db.upcast()).krate();
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let environment = db.trait_environment(self.func.into());
Type {
krate,
env: environment.clone(),
ty: ctx.lower_ty(&db.function_data(self.func).params[0].1),
}
}
} }
impl HasVisibility for Function { impl HasVisibility for Function {

View File

@ -1,7 +1,6 @@
//! Completes references after dot (fields and method calls). //! Completes references after dot (fields and method calls).
use either::Either; use either::Either;
use hir::ScopeDef;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions}; use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
@ -36,24 +35,22 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() { if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
return; return;
} }
ctx.scope.process_all_names(&mut |name, def| { if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
if let ScopeDef::Local(local) = &def { if let Some(self_) = func.self_param(ctx.db) {
if local.is_self(ctx.db) { let ty = self_.ty(ctx.db);
let ty = local.ty(ctx.db); complete_fields(ctx, &ty, |field, ty| match field {
complete_fields(ctx, &ty, |field, ty| match field { either::Either::Left(field) => {
either::Either::Left(field) => { acc.add_field(ctx, Some(hir::known::SELF_PARAM), field, &ty)
acc.add_field(ctx, Some(name.clone()), field, &ty) }
} either::Either::Right(tuple_idx) => {
either::Either::Right(tuple_idx) => { acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), tuple_idx, &ty)
acc.add_tuple_field(ctx, Some(name.clone()), tuple_idx, &ty) }
} });
}); complete_methods(ctx, &ty, |func| {
complete_methods(ctx, &ty, |func| { acc.add_method(ctx, func, Some(hir::known::SELF_PARAM), None)
acc.add_method(ctx, func, Some(name.clone()), None) });
});
}
} }
}); }
} }
fn complete_fields( fn complete_fields(