diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs index b2f7be011..b68b1d06f 100644 --- a/crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use hir::{Adt, ModuleDef}; | 1 | use hir::{Adt, ModuleDef}; |
2 | use ide_db::defs::Definition; | 2 | use ide_db::defs::{Definition, NameRefClass}; |
3 | use syntax::{ | 3 | use syntax::{ |
4 | ast::{self, AstNode, GenericParamsOwner, VisibilityOwner}, | 4 | ast::{self, AstNode, GenericParamsOwner, VisibilityOwner}, |
5 | match_ast, | 5 | match_ast, |
@@ -80,11 +80,9 @@ fn edit_struct_references( | |||
80 | strukt: &ast::Struct, | 80 | strukt: &ast::Struct, |
81 | names: &[ast::Name], | 81 | names: &[ast::Name], |
82 | ) { | 82 | ) { |
83 | let strukt_def = ctx.sema.to_def(strukt).unwrap(); | 83 | let strukt = ctx.sema.to_def(strukt).unwrap(); |
84 | let usages = Definition::ModuleDef(ModuleDef::Adt(Adt::Struct(strukt_def))) | 84 | let strukt_def = Definition::ModuleDef(ModuleDef::Adt(Adt::Struct(strukt))); |
85 | .usages(&ctx.sema) | 85 | let usages = strukt_def.usages(&ctx.sema).include_self_kw_refs(true).all(); |
86 | .include_self_kw_refs(true) | ||
87 | .all(); | ||
88 | 86 | ||
89 | for (file_id, refs) in usages { | 87 | for (file_id, refs) in usages { |
90 | edit.edit_file(file_id); | 88 | edit.edit_file(file_id); |
@@ -109,16 +107,26 @@ fn edit_struct_references( | |||
109 | .to_string(), | 107 | .to_string(), |
110 | ); | 108 | ); |
111 | }, | 109 | }, |
112 | // for tuple struct creations like: Foo(42) | 110 | // for tuple struct creations like Foo(42) |
113 | ast::CallExpr(call_expr) => { | 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 | let arg_list = | 123 | let arg_list = |
116 | call_expr.syntax().descendants().find_map(ast::ArgList::cast).unwrap(); | 124 | call_expr.syntax().descendants().find_map(ast::ArgList::cast).unwrap(); |
117 | 125 | ||
118 | edit.replace( | 126 | edit.replace( |
119 | call_expr.syntax().text_range(), | 127 | call_expr.syntax().text_range(), |
120 | ast::make::record_expr( | 128 | ast::make::record_expr( |
121 | path.path().unwrap(), | 129 | path, |
122 | ast::make::record_expr_field_list(arg_list.args().zip(names).map( | 130 | ast::make::record_expr_field_list(arg_list.args().zip(names).map( |
123 | |(expr, name)| { | 131 | |(expr, name)| { |
124 | ast::make::record_expr_field( | 132 | ast::make::record_expr_field( |
@@ -191,8 +199,12 @@ struct Inner; | |||
191 | struct A$0(Inner); | 199 | struct A$0(Inner); |
192 | 200 | ||
193 | impl A { | 201 | impl A { |
194 | fn new() -> A { | 202 | fn new(inner: Inner) -> A { |
195 | A(Inner) | 203 | A(inner) |
204 | } | ||
205 | |||
206 | fn new_with_default() -> A { | ||
207 | A::new(Inner) | ||
196 | } | 208 | } |
197 | 209 | ||
198 | fn into_inner(self) -> Inner { | 210 | fn into_inner(self) -> Inner { |
@@ -204,8 +216,12 @@ struct Inner; | |||
204 | struct A { field1: Inner } | 216 | struct A { field1: Inner } |
205 | 217 | ||
206 | impl A { | 218 | impl A { |
207 | fn new() -> A { | 219 | fn new(inner: Inner) -> A { |
208 | A { field1: Inner } | 220 | A { field1: inner } |
221 | } | ||
222 | |||
223 | fn new_with_default() -> A { | ||
224 | A::new(Inner) | ||
209 | } | 225 | } |
210 | 226 | ||
211 | fn into_inner(self) -> Inner { | 227 | fn into_inner(self) -> Inner { |
@@ -224,8 +240,12 @@ struct Inner; | |||
224 | struct A$0(Inner); | 240 | struct A$0(Inner); |
225 | 241 | ||
226 | impl A { | 242 | impl A { |
227 | fn new() -> Self { | 243 | fn new(inner: Inner) -> Self { |
228 | Self(Inner) | 244 | Self(inner) |
245 | } | ||
246 | |||
247 | fn new_with_default() -> Self { | ||
248 | Self::new(Inner) | ||
229 | } | 249 | } |
230 | 250 | ||
231 | fn into_inner(self) -> Inner { | 251 | fn into_inner(self) -> Inner { |
@@ -237,8 +257,12 @@ struct Inner; | |||
237 | struct A { field1: Inner } | 257 | struct A { field1: Inner } |
238 | 258 | ||
239 | impl A { | 259 | impl A { |
240 | fn new() -> Self { | 260 | fn new(inner: Inner) -> Self { |
241 | Self { field1: Inner } | 261 | Self { field1: inner } |
262 | } | ||
263 | |||
264 | fn new_with_default() -> Self { | ||
265 | Self::new(Inner) | ||
242 | } | 266 | } |
243 | 267 | ||
244 | fn into_inner(self) -> Inner { | 268 | fn into_inner(self) -> Inner { |