1
1
use hir:: { Adt , ModuleDef } ;
2
- use ide_db:: defs:: Definition ;
2
+ use ide_db:: defs:: { Definition , NameRefClass } ;
3
3
use syntax:: {
4
4
ast:: { self , AstNode , GenericParamsOwner , VisibilityOwner } ,
5
5
match_ast,
@@ -80,11 +80,9 @@ fn edit_struct_references(
80
80
strukt : & ast:: Struct ,
81
81
names : & [ ast:: Name ] ,
82
82
) {
83
- let strukt_def = ctx. sema . to_def ( strukt) . unwrap ( ) ;
84
- let usages = Definition :: ModuleDef ( ModuleDef :: Adt ( Adt :: Struct ( strukt_def) ) )
85
- . usages ( & ctx. sema )
86
- . include_self_kw_refs ( true )
87
- . all ( ) ;
83
+ let strukt = ctx. sema . to_def ( strukt) . unwrap ( ) ;
84
+ let strukt_def = Definition :: ModuleDef ( ModuleDef :: Adt ( Adt :: Struct ( strukt) ) ) ;
85
+ let usages = strukt_def. usages ( & ctx. sema ) . include_self_kw_refs ( true ) . all ( ) ;
88
86
89
87
for ( file_id, refs) in usages {
90
88
edit. edit_file ( file_id) ;
@@ -109,16 +107,26 @@ fn edit_struct_references(
109
107
. to_string( ) ,
110
108
) ;
111
109
} ,
112
- // for tuple struct creations like: Foo(42)
110
+ // for tuple struct creations like Foo(42)
113
111
ast:: CallExpr ( call_expr) => {
114
- let path = call_expr. syntax( ) . descendants( ) . find_map( ast:: PathExpr :: cast) . unwrap( ) ;
112
+ let path = call_expr. syntax( ) . descendants( ) . find_map( ast:: PathExpr :: cast) . unwrap( ) . path( ) . unwrap( ) ;
113
+
114
+ // this also includes method calls like Foo::new(42), we should skip them
115
+ if let Some ( Some ( name_ref) ) = path. segment( ) . map( |s| s. name_ref( ) ) {
116
+ match NameRefClass :: classify( & ctx. sema, & name_ref) {
117
+ Some ( NameRefClass :: Definition ( Definition :: SelfType ( _) ) ) => { } ,
118
+ Some ( NameRefClass :: Definition ( def) ) if def == strukt_def => { } ,
119
+ _ => continue ,
120
+ } ;
121
+ }
122
+
115
123
let arg_list =
116
124
call_expr. syntax( ) . descendants( ) . find_map( ast:: ArgList :: cast) . unwrap( ) ;
117
125
118
126
edit. replace(
119
127
call_expr. syntax( ) . text_range( ) ,
120
128
ast:: make:: record_expr(
121
- path. path ( ) . unwrap ( ) ,
129
+ path,
122
130
ast:: make:: record_expr_field_list( arg_list. args( ) . zip( names) . map(
123
131
|( expr, name) | {
124
132
ast:: make:: record_expr_field(
@@ -191,8 +199,12 @@ struct Inner;
191
199
struct A$0(Inner);
192
200
193
201
impl A {
194
- fn new() -> A {
195
- A(Inner)
202
+ fn new(inner: Inner) -> A {
203
+ A(inner)
204
+ }
205
+
206
+ fn new_with_default() -> A {
207
+ A::new(Inner)
196
208
}
197
209
198
210
fn into_inner(self) -> Inner {
@@ -204,8 +216,12 @@ struct Inner;
204
216
struct A { field1: Inner }
205
217
206
218
impl A {
207
- fn new() -> A {
208
- A { field1: Inner }
219
+ fn new(inner: Inner) -> A {
220
+ A { field1: inner }
221
+ }
222
+
223
+ fn new_with_default() -> A {
224
+ A::new(Inner)
209
225
}
210
226
211
227
fn into_inner(self) -> Inner {
@@ -224,8 +240,12 @@ struct Inner;
224
240
struct A$0(Inner);
225
241
226
242
impl A {
227
- fn new() -> Self {
228
- Self(Inner)
243
+ fn new(inner: Inner) -> Self {
244
+ Self(inner)
245
+ }
246
+
247
+ fn new_with_default() -> Self {
248
+ Self::new(Inner)
229
249
}
230
250
231
251
fn into_inner(self) -> Inner {
@@ -237,8 +257,12 @@ struct Inner;
237
257
struct A { field1: Inner }
238
258
239
259
impl A {
240
- fn new() -> Self {
241
- Self { field1: Inner }
260
+ fn new(inner: Inner) -> Self {
261
+ Self { field1: inner }
262
+ }
263
+
264
+ fn new_with_default() -> Self {
265
+ Self::new(Inner)
242
266
}
243
267
244
268
fn into_inner(self) -> Inner {
0 commit comments