|
| 1 | +//! Completion of names from the current scope in type position. |
| 2 | +
|
| 3 | +use hir::ScopeDef; |
| 4 | +use syntax::{ast, AstNode}; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + context::{PathCompletionCtx, PathKind, PathQualifierCtx}, |
| 8 | + patterns::ImmediateLocation, |
| 9 | + CompletionContext, Completions, |
| 10 | +}; |
| 11 | + |
| 12 | +pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) { |
| 13 | + let _p = profile::span("complete_type_path"); |
| 14 | + if ctx.is_path_disallowed() { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + let (&is_absolute_path, qualifier) = match &ctx.path_context { |
| 19 | + Some(PathCompletionCtx { |
| 20 | + kind: Some(PathKind::Type), is_absolute_path, qualifier, .. |
| 21 | + }) => (is_absolute_path, qualifier), |
| 22 | + _ => return, |
| 23 | + }; |
| 24 | + |
| 25 | + match qualifier { |
| 26 | + Some(PathQualifierCtx { .. }) => return, |
| 27 | + None if is_absolute_path => acc.add_crate_roots(ctx), |
| 28 | + None => { |
| 29 | + acc.add_nameref_keywords_with_colon(ctx); |
| 30 | + if let Some(ImmediateLocation::TypeBound) = &ctx.completion_location { |
| 31 | + ctx.process_all_names(&mut |name, res| { |
| 32 | + let add_resolution = match res { |
| 33 | + ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db), |
| 34 | + ScopeDef::ModuleDef( |
| 35 | + hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_), |
| 36 | + ) => true, |
| 37 | + _ => false, |
| 38 | + }; |
| 39 | + if add_resolution { |
| 40 | + acc.add_resolution(ctx, name, res); |
| 41 | + } |
| 42 | + }); |
| 43 | + return; |
| 44 | + } |
| 45 | + if let Some(ImmediateLocation::GenericArgList(arg_list)) = &ctx.completion_location { |
| 46 | + if let Some(path_seg) = arg_list.syntax().parent().and_then(ast::PathSegment::cast) |
| 47 | + { |
| 48 | + if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) = |
| 49 | + ctx.sema.resolve_path(&path_seg.parent_path()) |
| 50 | + { |
| 51 | + trait_.items(ctx.sema.db).into_iter().for_each(|it| { |
| 52 | + if let hir::AssocItem::TypeAlias(alias) = it { |
| 53 | + acc.add_type_alias_with_eq(ctx, alias) |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + ctx.process_all_names(&mut |name, def| { |
| 60 | + use hir::{GenericParam::*, ModuleDef::*}; |
| 61 | + let add_resolution = match def { |
| 62 | + ScopeDef::GenericParam(LifetimeParam(_)) | ScopeDef::Label(_) => false, |
| 63 | + // no values in type places |
| 64 | + ScopeDef::ModuleDef(Function(_) | Variant(_) | Static(_)) |
| 65 | + | ScopeDef::Local(_) => false, |
| 66 | + // unless its a constant in a generic arg list position |
| 67 | + ScopeDef::ModuleDef(Const(_)) | ScopeDef::GenericParam(ConstParam(_)) => { |
| 68 | + ctx.expects_generic_arg() |
| 69 | + } |
| 70 | + ScopeDef::ImplSelfType(_) => { |
| 71 | + !ctx.previous_token_is(syntax::T![impl]) |
| 72 | + && !ctx.previous_token_is(syntax::T![for]) |
| 73 | + } |
| 74 | + // Don't suggest attribute macros and derives. |
| 75 | + ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db), |
| 76 | + // Type things are fine |
| 77 | + ScopeDef::ModuleDef( |
| 78 | + BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TypeAlias(_), |
| 79 | + ) |
| 80 | + | ScopeDef::AdtSelfType(_) |
| 81 | + | ScopeDef::Unknown |
| 82 | + | ScopeDef::GenericParam(TypeParam(_)) => true, |
| 83 | + }; |
| 84 | + if add_resolution { |
| 85 | + acc.add_resolution(ctx, name, def); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments