@@ -11,6 +11,7 @@ use ide_db::{
11
11
} ,
12
12
SymbolKind ,
13
13
} ;
14
+ use smallvec:: SmallVec ;
14
15
use stdx:: { format_to, impl_from, never} ;
15
16
use syntax:: { algo, TextRange } ;
16
17
use text_edit:: TextEdit ;
@@ -76,7 +77,7 @@ pub struct CompletionItem {
76
77
ref_match : Option < Mutability > ,
77
78
78
79
/// The import data to add to completion's edits.
79
- import_to_add : Option < ImportEdit > ,
80
+ import_to_add : SmallVec < [ ImportEdit ; 1 ] > ,
80
81
}
81
82
82
83
// We use custom debug for CompletionItem to make snapshot tests more readable.
@@ -305,7 +306,7 @@ impl CompletionItem {
305
306
trigger_call_info : None ,
306
307
relevance : CompletionRelevance :: default ( ) ,
307
308
ref_match : None ,
308
- import_to_add : None ,
309
+ imports_to_add : Default :: default ( ) ,
309
310
}
310
311
}
311
312
@@ -364,8 +365,8 @@ impl CompletionItem {
364
365
self . ref_match . map ( |mutability| ( mutability, relevance) )
365
366
}
366
367
367
- pub fn import_to_add ( & self ) -> Option < & ImportEdit > {
368
- self . import_to_add . as_ref ( )
368
+ pub fn imports_to_add ( & self ) -> & [ ImportEdit ] {
369
+ & self . import_to_add
369
370
}
370
371
}
371
372
@@ -398,7 +399,7 @@ impl ImportEdit {
398
399
pub ( crate ) struct Builder {
399
400
source_range : TextRange ,
400
401
completion_kind : CompletionKind ,
401
- import_to_add : Option < ImportEdit > ,
402
+ imports_to_add : SmallVec < [ ImportEdit ; 1 ] > ,
402
403
trait_name : Option < String > ,
403
404
label : String ,
404
405
insert_text : Option < String > ,
@@ -422,14 +423,13 @@ impl Builder {
422
423
let mut lookup = self . lookup ;
423
424
let mut insert_text = self . insert_text ;
424
425
425
- if let Some ( original_path) = self
426
- . import_to_add
427
- . as_ref ( )
428
- . and_then ( |import_edit| import_edit. import . original_path . as_ref ( ) )
429
- {
430
- lookup = lookup. or_else ( || Some ( label. clone ( ) ) ) ;
431
- insert_text = insert_text. or_else ( || Some ( label. clone ( ) ) ) ;
432
- format_to ! ( label, " (use {})" , original_path)
426
+ if let [ import_edit] = & * self . imports_to_add {
427
+ // snippets can have multiple imports, but normal completions only have up to one
428
+ if let Some ( original_path) = import_edit. import . original_path . as_ref ( ) {
429
+ lookup = lookup. or_else ( || Some ( label. clone ( ) ) ) ;
430
+ insert_text = insert_text. or_else ( || Some ( label. clone ( ) ) ) ;
431
+ format_to ! ( label, " (use {})" , original_path)
432
+ }
433
433
} else if let Some ( trait_name) = self . trait_name {
434
434
insert_text = insert_text. or_else ( || Some ( label. clone ( ) ) ) ;
435
435
format_to ! ( label, " (as {})" , trait_name)
@@ -456,7 +456,7 @@ impl Builder {
456
456
trigger_call_info : self . trigger_call_info . unwrap_or ( false ) ,
457
457
relevance : self . relevance ,
458
458
ref_match : self . ref_match ,
459
- import_to_add : self . import_to_add ,
459
+ import_to_add : self . imports_to_add ,
460
460
}
461
461
}
462
462
pub ( crate ) fn lookup_by ( & mut self , lookup : impl Into < String > ) -> & mut Builder {
@@ -527,8 +527,8 @@ impl Builder {
527
527
self . trigger_call_info = Some ( true ) ;
528
528
self
529
529
}
530
- pub ( crate ) fn add_import ( & mut self , import_to_add : Option < ImportEdit > ) -> & mut Builder {
531
- self . import_to_add = import_to_add ;
530
+ pub ( crate ) fn add_import ( & mut self , import_to_add : ImportEdit ) -> & mut Builder {
531
+ self . imports_to_add . push ( import_to_add) ;
532
532
self
533
533
}
534
534
pub ( crate ) fn ref_match ( & mut self , mutability : Mutability ) -> & mut Builder {
0 commit comments