@@ -22,12 +22,15 @@ pub(crate) mod vis;
2222
2323use std:: iter;
2424
25- use hir:: { db :: HirDatabase , known, ScopeDef } ;
25+ use hir:: { known, ScopeDef } ;
2626use ide_db:: SymbolKind ;
2727use syntax:: ast;
2828
2929use crate :: {
30- context:: Visible ,
30+ context:: {
31+ ItemListKind , NameContext , NameKind , NameRefContext , NameRefKind , PathKind , PatternContext ,
32+ TypeLocation , Visible ,
33+ } ,
3134 item:: Builder ,
3235 render:: {
3336 const_:: render_const,
@@ -43,22 +46,6 @@ use crate::{
4346 CompletionContext , CompletionItem , CompletionItemKind ,
4447} ;
4548
46- fn module_or_attr ( db : & dyn HirDatabase , def : ScopeDef ) -> Option < ScopeDef > {
47- match def {
48- ScopeDef :: ModuleDef ( hir:: ModuleDef :: Macro ( m) ) if m. is_attr ( db) => Some ( def) ,
49- ScopeDef :: ModuleDef ( hir:: ModuleDef :: Module ( _) ) => Some ( def) ,
50- _ => None ,
51- }
52- }
53-
54- fn module_or_fn_macro ( db : & dyn HirDatabase , def : ScopeDef ) -> Option < ScopeDef > {
55- match def {
56- ScopeDef :: ModuleDef ( hir:: ModuleDef :: Macro ( m) ) if m. is_fn_like ( db) => Some ( def) ,
57- ScopeDef :: ModuleDef ( hir:: ModuleDef :: Module ( _) ) => Some ( def) ,
58- _ => None ,
59- }
60- }
61-
6249/// Represents an in-progress set of completions being built.
6350#[ derive( Debug , Default ) ]
6451pub struct Completions {
@@ -181,6 +168,15 @@ impl Completions {
181168 self . add ( render_resolution_simple ( RenderContext :: new ( ctx) , local_name, resolution) . build ( ) ) ;
182169 }
183170
171+ pub ( crate ) fn add_module (
172+ & mut self ,
173+ ctx : & CompletionContext ,
174+ module : hir:: Module ,
175+ local_name : hir:: Name ,
176+ ) {
177+ self . add_resolution ( ctx, local_name, hir:: ScopeDef :: ModuleDef ( module. into ( ) ) ) ;
178+ }
179+
184180 pub ( crate ) fn add_macro (
185181 & mut self ,
186182 ctx : & CompletionContext ,
@@ -437,3 +433,124 @@ fn enum_variants_with_paths(
437433 }
438434 }
439435}
436+
437+ pub ( super ) fn complete_name (
438+ acc : & mut Completions ,
439+ ctx : & CompletionContext ,
440+ NameContext { name, kind } : & NameContext ,
441+ ) {
442+ match kind {
443+ NameKind :: Const => {
444+ item_list:: trait_impl:: complete_trait_impl_const ( acc, ctx, name) ;
445+ }
446+ NameKind :: Function => {
447+ item_list:: trait_impl:: complete_trait_impl_fn ( acc, ctx, name) ;
448+ }
449+ NameKind :: IdentPat ( pattern_ctx) => complete_patterns ( acc, ctx, pattern_ctx) ,
450+ NameKind :: Module ( mod_under_caret) => {
451+ mod_:: complete_mod ( acc, ctx, mod_under_caret) ;
452+ }
453+ NameKind :: TypeAlias => {
454+ item_list:: trait_impl:: complete_trait_impl_type_alias ( acc, ctx, name) ;
455+ }
456+ NameKind :: RecordField => {
457+ field:: complete_field_list_record_variant ( acc, ctx) ;
458+ }
459+ NameKind :: ConstParam
460+ | NameKind :: Enum
461+ | NameKind :: MacroDef
462+ | NameKind :: MacroRules
463+ | NameKind :: Rename
464+ | NameKind :: SelfParam
465+ | NameKind :: Static
466+ | NameKind :: Struct
467+ | NameKind :: Trait
468+ | NameKind :: TypeParam
469+ | NameKind :: Union
470+ | NameKind :: Variant => ( ) ,
471+ }
472+ }
473+
474+ pub ( super ) fn complete_name_ref (
475+ acc : & mut Completions ,
476+ ctx : & CompletionContext ,
477+ NameRefContext { nameref, kind } : & NameRefContext ,
478+ ) {
479+ match kind {
480+ NameRefKind :: Path ( path_ctx) => {
481+ flyimport:: import_on_the_fly_path ( acc, ctx, path_ctx) ;
482+
483+ match & path_ctx. kind {
484+ PathKind :: Expr { expr_ctx } => {
485+ expr:: complete_expr_path ( acc, ctx, path_ctx, expr_ctx) ;
486+
487+ dot:: complete_undotted_self ( acc, ctx, path_ctx, expr_ctx) ;
488+ item_list:: complete_item_list_in_expr ( acc, ctx, path_ctx, expr_ctx) ;
489+ record:: complete_record_expr_func_update ( acc, ctx, path_ctx, expr_ctx) ;
490+ snippet:: complete_expr_snippet ( acc, ctx, path_ctx, expr_ctx) ;
491+ }
492+ PathKind :: Type { location } => {
493+ r#type:: complete_type_path ( acc, ctx, path_ctx, location) ;
494+
495+ match location {
496+ TypeLocation :: TupleField => {
497+ field:: complete_field_list_tuple_variant ( acc, ctx, path_ctx) ;
498+ }
499+ TypeLocation :: TypeAscription ( ascription) => {
500+ r#type:: complete_ascribed_type ( acc, ctx, path_ctx, ascription) ;
501+ }
502+ TypeLocation :: GenericArgList ( _)
503+ | TypeLocation :: TypeBound
504+ | TypeLocation :: ImplTarget
505+ | TypeLocation :: ImplTrait
506+ | TypeLocation :: Other => ( ) ,
507+ }
508+ }
509+ PathKind :: Attr { attr_ctx } => {
510+ attribute:: complete_attribute_path ( acc, ctx, path_ctx, attr_ctx) ;
511+ }
512+ PathKind :: Derive { existing_derives } => {
513+ attribute:: complete_derive_path ( acc, ctx, path_ctx, existing_derives) ;
514+ }
515+ PathKind :: Item { kind } => {
516+ item_list:: complete_item_list ( acc, ctx, path_ctx, kind) ;
517+
518+ snippet:: complete_item_snippet ( acc, ctx, path_ctx, kind) ;
519+ if let ItemListKind :: TraitImpl ( impl_) = kind {
520+ item_list:: trait_impl:: complete_trait_impl_item_by_name (
521+ acc, ctx, path_ctx, nameref, impl_,
522+ ) ;
523+ }
524+ }
525+ PathKind :: Pat { .. } => {
526+ pattern:: complete_pattern_path ( acc, ctx, path_ctx) ;
527+ }
528+ PathKind :: Vis { has_in_token } => {
529+ vis:: complete_vis_path ( acc, ctx, path_ctx, has_in_token) ;
530+ }
531+ PathKind :: Use => {
532+ use_:: complete_use_path ( acc, ctx, path_ctx, nameref) ;
533+ }
534+ }
535+ }
536+ NameRefKind :: DotAccess ( dot_access) => {
537+ flyimport:: import_on_the_fly_dot ( acc, ctx, dot_access) ;
538+ dot:: complete_dot ( acc, ctx, dot_access) ;
539+ postfix:: complete_postfix ( acc, ctx, dot_access) ;
540+ }
541+ NameRefKind :: Keyword ( item) => {
542+ keyword:: complete_for_and_where ( acc, ctx, item) ;
543+ }
544+ NameRefKind :: RecordExpr ( record_expr) => {
545+ record:: complete_record_expr_fields ( acc, ctx, record_expr) ;
546+ }
547+ NameRefKind :: Pattern ( pattern_ctx) => complete_patterns ( acc, ctx, pattern_ctx) ,
548+ }
549+ }
550+
551+ fn complete_patterns ( acc : & mut Completions , ctx : & CompletionContext , pattern_ctx : & PatternContext ) {
552+ flyimport:: import_on_the_fly_pat ( acc, ctx, pattern_ctx) ;
553+ fn_param:: complete_fn_param ( acc, ctx, pattern_ctx) ;
554+ pattern:: complete_pattern ( acc, ctx, pattern_ctx) ;
555+ record:: complete_record_pattern_fields ( acc, ctx, pattern_ctx) ;
556+ }
0 commit comments