11//! Completion for derives
22use hir:: { HasAttrs , MacroDef , MacroKind } ;
3- use ide_db:: helpers:: FamousDefs ;
3+ use ide_db:: helpers:: { import_assets :: ImportAssets , insert_use :: ImportScope , FamousDefs } ;
44use itertools:: Itertools ;
55use rustc_hash:: FxHashSet ;
6- use syntax:: ast;
6+ use syntax:: { ast, SyntaxKind } ;
77
88use crate :: {
9+ completions:: flyimport:: compute_fuzzy_completion_order_key,
910 context:: CompletionContext ,
1011 item:: { CompletionItem , CompletionItemKind } ,
11- Completions ,
12+ Completions , ImportEdit ,
1213} ;
1314
1415pub ( super ) fn complete_derive (
@@ -66,6 +67,8 @@ pub(super) fn complete_derive(
6667 }
6768 item. add_to ( acc) ;
6869 }
70+
71+ flyimport_attribute ( ctx, acc) ;
6972}
7073
7174fn get_derives_in_scope ( ctx : & CompletionContext ) -> Vec < ( hir:: Name , MacroDef ) > {
@@ -80,6 +83,50 @@ fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
8083 result
8184}
8285
86+ fn flyimport_attribute ( ctx : & CompletionContext , acc : & mut Completions ) -> Option < ( ) > {
87+ if ctx. token . kind ( ) != SyntaxKind :: IDENT {
88+ return None ;
89+ } ;
90+ let potential_import_name = ctx. token . to_string ( ) ;
91+ let module = ctx. scope . module ( ) ?;
92+ let parent = ctx. token . parent ( ) ?;
93+ let user_input_lowercased = potential_import_name. to_lowercase ( ) ;
94+ let import_assets = ImportAssets :: for_fuzzy_path (
95+ module,
96+ None ,
97+ potential_import_name,
98+ & ctx. sema ,
99+ parent. clone ( ) ,
100+ ) ?;
101+ let import_scope = ImportScope :: find_insert_use_container_with_macros ( & parent, & ctx. sema ) ?;
102+ acc. add_all (
103+ import_assets
104+ . search_for_imports ( & ctx. sema , ctx. config . insert_use . prefix_kind )
105+ . into_iter ( )
106+ . filter_map ( |import| match import. original_item {
107+ hir:: ItemInNs :: Macros ( mac) => Some ( ( import, mac) ) ,
108+ _ => None ,
109+ } )
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+ CompletionItemKind :: Attribute ,
117+ ctx. source_range ( ) ,
118+ mac. name ( ctx. db ) ?. to_string ( ) ,
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+
83130struct DeriveDependencies {
84131 label : & ' static str ,
85132 dependencies : & ' static [ & ' static str ] ,
0 commit comments