11//! Completion for derives
2- use hir:: { HasAttrs , Macro , MacroKind } ;
3- use ide_db:: {
4- imports:: { import_assets:: ImportAssets , insert_use:: ImportScope } ,
5- SymbolKind ,
6- } ;
2+ use hir:: { HasAttrs , Macro } ;
3+ use ide_db:: SymbolKind ;
74use itertools:: Itertools ;
8- use rustc_hash:: FxHashSet ;
9- use syntax:: { SmolStr , SyntaxKind } ;
5+ use syntax:: SmolStr ;
106
117use crate :: {
12- completions:: flyimport:: compute_fuzzy_completion_order_key,
138 context:: { CompletionContext , PathCompletionCtx , PathKind } ,
149 item:: CompletionItem ,
15- Completions , ImportEdit ,
10+ Completions ,
1611} ;
1712
1813pub ( crate ) fn complete_derive ( acc : & mut Completions , ctx : & CompletionContext ) {
19- let attr = match ( & ctx. path_context , ctx. attr . as_ref ( ) ) {
20- ( Some ( PathCompletionCtx { kind : Some ( PathKind :: Derive ) , .. } ) , Some ( attr) ) => attr,
14+ match ctx. path_context {
15+ // FIXME: Enable qualified completions
16+ Some ( PathCompletionCtx { kind : Some ( PathKind :: Derive ) , qualifier : None , .. } ) => ( ) ,
2117 _ => return ,
22- } ;
18+ }
2319
2420 let core = ctx. famous_defs ( ) . core ( ) ;
25- let existing_derives: FxHashSet < _ > =
26- ctx. sema . resolve_derive_macro ( attr) . into_iter ( ) . flatten ( ) . flatten ( ) . collect ( ) ;
2721
2822 for ( name, mac) in get_derives_in_scope ( ctx) {
29- if existing_derives. contains ( & mac) {
23+ if ctx . existing_derives . contains ( & mac) {
3024 continue ;
3125 }
3226
@@ -41,7 +35,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
4135 let mut components = vec ! [ derive_completion. label] ;
4236 components. extend ( derive_completion. dependencies . iter ( ) . filter (
4337 |& & dependency| {
44- !existing_derives
38+ !ctx . existing_derives
4539 . iter ( )
4640 . map ( |it| it. name ( ctx. db ) )
4741 . any ( |it| it. to_smol_str ( ) == dependency)
@@ -66,8 +60,6 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
6660 }
6761 item. add_to ( acc) ;
6862 }
69-
70- flyimport_derive ( acc, ctx) ;
7163}
7264
7365fn get_derives_in_scope ( ctx : & CompletionContext ) -> Vec < ( hir:: Name , Macro ) > {
@@ -82,51 +74,6 @@ fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, Macro)> {
8274 result
8375}
8476
85- fn flyimport_derive ( acc : & mut Completions , ctx : & CompletionContext ) -> Option < ( ) > {
86- if ctx. token . kind ( ) != SyntaxKind :: IDENT {
87- return None ;
88- } ;
89- let potential_import_name = ctx. token . to_string ( ) ;
90- let module = ctx. module ?;
91- let parent = ctx. token . parent ( ) ?;
92- let user_input_lowercased = potential_import_name. to_lowercase ( ) ;
93- let import_assets = ImportAssets :: for_fuzzy_path (
94- module,
95- None ,
96- potential_import_name,
97- & ctx. sema ,
98- parent. clone ( ) ,
99- ) ?;
100- let import_scope = ImportScope :: find_insert_use_container ( & parent, & ctx. sema ) ?;
101- acc. add_all (
102- import_assets
103- . search_for_imports ( & ctx. sema , ctx. config . insert_use . prefix_kind )
104- . into_iter ( )
105- . filter_map ( |import| match import. original_item {
106- hir:: ItemInNs :: Macros ( mac) => Some ( ( import, mac) ) ,
107- _ => None ,
108- } )
109- . filter ( |& ( _, mac) | mac. kind ( ctx. db ) == MacroKind :: Derive )
110- . filter ( |& ( _, mac) | !ctx. is_item_hidden ( & hir:: ItemInNs :: Macros ( mac) ) )
111- . sorted_by_key ( |( import, _) | {
112- compute_fuzzy_completion_order_key ( & import. import_path , & user_input_lowercased)
113- } )
114- . filter_map ( |( import, mac) | {
115- let mut item = CompletionItem :: new (
116- SymbolKind :: Derive ,
117- ctx. source_range ( ) ,
118- mac. name ( ctx. db ) . to_smol_str ( ) ,
119- ) ;
120- item. add_import ( ImportEdit { import, scope : import_scope. clone ( ) } ) ;
121- if let Some ( docs) = mac. docs ( ctx. db ) {
122- item. documentation ( docs) ;
123- }
124- Some ( item. build ( ) )
125- } ) ,
126- ) ;
127- Some ( ( ) )
128- }
129-
13077struct DeriveDependencies {
13178 label : & ' static str ,
13279 dependencies : & ' static [ & ' static str ] ,
0 commit comments