3
3
use std:: fmt;
4
4
5
5
use hir:: { Documentation , Mutability } ;
6
- use ide_db:: {
7
- helpers:: mod_path_to_ast,
8
- imports:: {
9
- import_assets:: LocatedImport ,
10
- insert_use:: { self , ImportScope , InsertUseConfig } ,
11
- } ,
12
- SnippetCap , SymbolKind ,
13
- } ;
6
+ use ide_db:: { imports:: import_assets:: LocatedImport , SnippetCap , SymbolKind } ;
14
7
use smallvec:: SmallVec ;
15
8
use stdx:: { impl_from, never} ;
16
- use syntax:: { algo , SmolStr , TextRange } ;
9
+ use syntax:: { SmolStr , TextRange } ;
17
10
use text_edit:: TextEdit ;
18
11
19
12
/// `CompletionItem` describes a single completion variant in the editor pop-up.
@@ -73,7 +66,7 @@ pub struct CompletionItem {
73
66
ref_match : Option < Mutability > ,
74
67
75
68
/// The import data to add to completion's edits.
76
- import_to_add : SmallVec < [ ImportEdit ; 1 ] > ,
69
+ import_to_add : SmallVec < [ LocatedImport ; 1 ] > ,
77
70
}
78
71
79
72
// We use custom debug for CompletionItem to make snapshot tests more readable.
@@ -380,40 +373,17 @@ impl CompletionItem {
380
373
self . ref_match . map ( |mutability| ( mutability, relevance) )
381
374
}
382
375
383
- pub fn imports_to_add ( & self ) -> & [ ImportEdit ] {
376
+ pub fn imports_to_add ( & self ) -> & [ LocatedImport ] {
384
377
& self . import_to_add
385
378
}
386
379
}
387
380
388
- /// An extra import to add after the completion is applied.
389
- #[ derive( Debug , Clone ) ]
390
- pub struct ImportEdit {
391
- pub import : LocatedImport ,
392
- pub scope : ImportScope ,
393
- }
394
-
395
- impl ImportEdit {
396
- /// Attempts to insert the import to the given scope, producing a text edit.
397
- /// May return no edit in edge cases, such as scope already containing the import.
398
- pub fn to_text_edit ( & self , cfg : InsertUseConfig ) -> Option < TextEdit > {
399
- let _p = profile:: span ( "ImportEdit::to_text_edit" ) ;
400
-
401
- let new_ast = self . scope . clone_for_update ( ) ;
402
- insert_use:: insert_use ( & new_ast, mod_path_to_ast ( & self . import . import_path ) , & cfg) ;
403
- let mut import_insert = TextEdit :: builder ( ) ;
404
- algo:: diff ( self . scope . as_syntax_node ( ) , new_ast. as_syntax_node ( ) )
405
- . into_text_edit ( & mut import_insert) ;
406
-
407
- Some ( import_insert. finish ( ) )
408
- }
409
- }
410
-
411
381
/// A helper to make `CompletionItem`s.
412
382
#[ must_use]
413
383
#[ derive( Clone ) ]
414
384
pub ( crate ) struct Builder {
415
385
source_range : TextRange ,
416
- imports_to_add : SmallVec < [ ImportEdit ; 1 ] > ,
386
+ imports_to_add : SmallVec < [ LocatedImport ; 1 ] > ,
417
387
trait_name : Option < SmolStr > ,
418
388
label : SmolStr ,
419
389
insert_text : Option < String > ,
@@ -439,7 +409,7 @@ impl Builder {
439
409
440
410
if let [ import_edit] = & * self . imports_to_add {
441
411
// snippets can have multiple imports, but normal completions only have up to one
442
- if let Some ( original_path) = import_edit. import . original_path . as_ref ( ) {
412
+ if let Some ( original_path) = import_edit. original_path . as_ref ( ) {
443
413
lookup = lookup. or_else ( || Some ( label. clone ( ) ) ) ;
444
414
label = SmolStr :: from ( format ! ( "{} (use {})" , label, original_path) ) ;
445
415
}
@@ -533,7 +503,7 @@ impl Builder {
533
503
self . trigger_call_info = Some ( true ) ;
534
504
self
535
505
}
536
- pub ( crate ) fn add_import ( & mut self , import_to_add : ImportEdit ) -> & mut Builder {
506
+ pub ( crate ) fn add_import ( & mut self , import_to_add : LocatedImport ) -> & mut Builder {
537
507
self . imports_to_add . push ( import_to_add) ;
538
508
self
539
509
}
0 commit comments