22
33use assists:: utils:: { insert_use, mod_path_to_ast, ImportScope } ;
44use either:: Either ;
5- use hir:: { db :: HirDatabase , MacroDef , ModuleDef } ;
5+ use hir:: ScopeDef ;
66use ide_db:: imports_locator;
77use syntax:: { algo, AstNode } ;
8- use text_edit:: TextEdit ;
98
10- use crate :: { context:: CompletionContext , item:: CompletionKind , CompletionItem , CompletionItemKind } ;
9+ use crate :: {
10+ context:: CompletionContext ,
11+ render:: { render_resolution, RenderContext } ,
12+ } ;
1113
1214use super :: Completions ;
1315
@@ -25,65 +27,49 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) ->
2527 let possible_imports =
2628 imports_locator:: find_similar_imports ( & ctx. sema , ctx. krate ?, & potential_import_name)
2729 . filter_map ( |import_candidate| {
28- let use_path = match import_candidate {
29- Either :: Left ( module_def) => current_module. find_use_path ( ctx. db , module_def) ,
30- Either :: Right ( macro_def) => current_module. find_use_path ( ctx. db , macro_def) ,
31- } ?;
32- Some ( ( use_path, additional_completion ( ctx. db , import_candidate) ) )
30+ Some ( match import_candidate {
31+ Either :: Left ( module_def) => (
32+ current_module. find_use_path ( ctx. db , module_def) ?,
33+ ScopeDef :: ModuleDef ( module_def) ,
34+ ) ,
35+ Either :: Right ( macro_def) => (
36+ current_module. find_use_path ( ctx. db , macro_def) ?,
37+ ScopeDef :: MacroDef ( macro_def) ,
38+ ) ,
39+ } )
3340 } )
34- . filter_map ( |( mod_path, additional_completion) | {
35- let mut builder = TextEdit :: builder ( ) ;
41+ . filter_map ( |( mod_path, definition) | {
42+ let mut resolution_with_missing_import = render_resolution (
43+ RenderContext :: new ( ctx) ,
44+ mod_path. segments . last ( ) ?. to_string ( ) ,
45+ & definition,
46+ ) ?;
3647
37- let correct_qualifier = format ! (
38- "{}{}" ,
39- mod_path. segments. last( ) ?,
40- additional_completion. unwrap_or_default( )
41- ) ;
42- builder. replace ( anchor. syntax ( ) . text_range ( ) , correct_qualifier) ;
48+ let mut text_edits =
49+ resolution_with_missing_import. text_edit ( ) . to_owned ( ) . into_builder ( ) ;
4350
4451 let rewriter =
4552 insert_use ( & import_scope, mod_path_to_ast ( & mod_path) , ctx. config . merge ) ;
4653 let old_ast = rewriter. rewrite_root ( ) ?;
47- algo:: diff ( & old_ast, & rewriter. rewrite ( & old_ast) ) . into_text_edit ( & mut builder) ;
48-
49- let completion_item: CompletionItem = CompletionItem :: new (
50- CompletionKind :: Magic ,
51- ctx. source_range ( ) ,
52- mod_path. to_string ( ) ,
53- )
54- . kind ( CompletionItemKind :: Struct )
55- . text_edit ( builder. finish ( ) )
56- . into ( ) ;
57- Some ( completion_item)
54+ algo:: diff ( & old_ast, & rewriter. rewrite ( & old_ast) ) . into_text_edit ( & mut text_edits) ;
55+
56+ resolution_with_missing_import. update_text_edit ( text_edits. finish ( ) ) ;
57+
58+ Some ( resolution_with_missing_import)
5859 } ) ;
59- acc. add_all ( possible_imports) ;
6060
61+ acc. add_all ( possible_imports) ;
6162 Some ( ( ) )
6263}
6364
64- fn additional_completion (
65- db : & dyn HirDatabase ,
66- import_candidate : Either < ModuleDef , MacroDef > ,
67- ) -> Option < String > {
68- match import_candidate {
69- Either :: Left ( ModuleDef :: Function ( _) ) => Some ( "()" . to_string ( ) ) ,
70- Either :: Right ( macro_def) => {
71- let ( left_brace, right_brace) =
72- crate :: render:: macro_:: guess_macro_braces ( db, macro_def) ;
73- Some ( format ! ( "!{}{}" , left_brace, right_brace) )
74- }
75- _ => None ,
76- }
77- }
78-
7965#[ cfg( test) ]
8066mod tests {
8167 use crate :: test_utils:: check_edit;
8268
8369 #[ test]
8470 fn function_magic_completion ( ) {
8571 check_edit (
86- "dep::io:: stdin" ,
72+ "stdin" ,
8773 r#"
8874//- /lib.rs crate:dep
8975pub mod io {
@@ -99,7 +85,7 @@ fn main() {
9985use dep::io::stdin;
10086
10187fn main() {
102- stdin()
88+ stdin()$0
10389}
10490"# ,
10591 ) ;
@@ -108,7 +94,7 @@ fn main() {
10894 #[ test]
10995 fn macro_magic_completion ( ) {
11096 check_edit (
111- "dep:: macro_with_curlies" ,
97+ "macro_with_curlies! " ,
11298 r#"
11399//- /lib.rs crate:dep
114100/// Please call me as macro_with_curlies! {}
@@ -126,7 +112,7 @@ fn main() {
126112use dep::macro_with_curlies;
127113
128114fn main() {
129- macro_with_curlies! {}
115+ macro_with_curlies! {$0 }
130116}
131117"# ,
132118 ) ;
@@ -135,7 +121,7 @@ fn main() {
135121 #[ test]
136122 fn case_insensitive_magic_completion_works ( ) {
137123 check_edit (
138- "dep::some_module:: ThirdStruct" ,
124+ "ThirdStruct" ,
139125 r#"
140126//- /lib.rs crate:dep
141127pub struct FirstStruct;
0 commit comments