@@ -5,7 +5,7 @@ use ide_db::{
5
5
helpers:: mod_path_to_ast,
6
6
imports:: {
7
7
import_assets:: { ImportAssets , ImportCandidate , LocatedImport } ,
8
- insert_use:: { insert_use, ImportScope } ,
8
+ insert_use:: { insert_use, insert_use_as_alias , ImportScope } ,
9
9
} ,
10
10
} ;
11
11
use syntax:: { ast, AstNode , NodeOrToken , SyntaxElement } ;
@@ -129,10 +129,12 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
129
129
for import in proposed_imports {
130
130
let import_path = import. import_path ;
131
131
132
+ let ( assist_id, import_name) =
133
+ ( AssistId ( "auto_import" , AssistKind :: QuickFix ) , import_path. display ( ctx. db ( ) ) ) ;
132
134
acc. add_group (
133
135
& group_label,
134
- AssistId ( "auto_import" , AssistKind :: QuickFix ) ,
135
- format ! ( "Import `{}`" , import_path . display ( ctx . db ( ) ) ) ,
136
+ assist_id ,
137
+ format ! ( "Import `{}`" , import_name ) ,
136
138
range,
137
139
|builder| {
138
140
let scope = match scope. clone ( ) {
@@ -143,6 +145,38 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
143
145
insert_use ( & scope, mod_path_to_ast ( & import_path) , & ctx. config . insert_use ) ;
144
146
} ,
145
147
) ;
148
+
149
+ match import_assets. import_candidate ( ) {
150
+ ImportCandidate :: TraitAssocItem ( name) | ImportCandidate :: TraitMethod ( name) => {
151
+ let is_method =
152
+ matches ! ( import_assets. import_candidate( ) , ImportCandidate :: TraitMethod ( _) ) ;
153
+ let type_ = if is_method { "method" } else { "item" } ;
154
+ let group_label = GroupLabel ( format ! (
155
+ "Import a trait for {} {} by alias" ,
156
+ type_,
157
+ name. assoc_item_name. text( )
158
+ ) ) ;
159
+ acc. add_group (
160
+ & group_label,
161
+ assist_id,
162
+ format ! ( "Import `{} as _`" , import_name) ,
163
+ range,
164
+ |builder| {
165
+ let scope = match scope. clone ( ) {
166
+ ImportScope :: File ( it) => ImportScope :: File ( builder. make_mut ( it) ) ,
167
+ ImportScope :: Module ( it) => ImportScope :: Module ( builder. make_mut ( it) ) ,
168
+ ImportScope :: Block ( it) => ImportScope :: Block ( builder. make_mut ( it) ) ,
169
+ } ;
170
+ insert_use_as_alias (
171
+ & scope,
172
+ mod_path_to_ast ( & import_path) ,
173
+ & ctx. config . insert_use ,
174
+ ) ;
175
+ } ,
176
+ ) ;
177
+ }
178
+ _ => { }
179
+ }
146
180
}
147
181
Some ( ( ) )
148
182
}
@@ -723,7 +757,7 @@ fn main() {
723
757
}
724
758
" ,
725
759
r"
726
- use test_mod::TestTrait;
760
+ use test_mod::TestTrait as _ ;
727
761
728
762
mod test_mod {
729
763
pub trait TestTrait {
@@ -794,7 +828,7 @@ fn main() {
794
828
}
795
829
" ,
796
830
r"
797
- use test_mod::TestTrait;
831
+ use test_mod::TestTrait as _ ;
798
832
799
833
mod test_mod {
800
834
pub trait TestTrait {
@@ -866,7 +900,7 @@ fn main() {
866
900
}
867
901
" ,
868
902
r"
869
- use test_mod::TestTrait;
903
+ use test_mod::TestTrait as _ ;
870
904
871
905
mod test_mod {
872
906
pub trait TestTrait {
@@ -908,7 +942,7 @@ fn main() {
908
942
}
909
943
" ,
910
944
r"
911
- use dep::test_mod::TestTrait;
945
+ use dep::test_mod::TestTrait as _ ;
912
946
913
947
fn main() {
914
948
let test_struct = dep::test_mod::TestStruct {};
@@ -939,7 +973,7 @@ fn main() {
939
973
}
940
974
" ,
941
975
r"
942
- use dep::test_mod::TestTrait;
976
+ use dep::test_mod::TestTrait as _ ;
943
977
944
978
fn main() {
945
979
dep::test_mod::TestStruct::test_function
@@ -969,7 +1003,7 @@ fn main() {
969
1003
}
970
1004
" ,
971
1005
r"
972
- use dep::test_mod::TestTrait;
1006
+ use dep::test_mod::TestTrait as _ ;
973
1007
974
1008
fn main() {
975
1009
dep::test_mod::TestStruct::CONST
0 commit comments