diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/add_missing_impl_members.rs | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 4435c4b5d..b09478d30 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs | |||
@@ -35,6 +35,7 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) -> | |||
35 | trait_fns | 35 | trait_fns |
36 | .into_iter() | 36 | .into_iter() |
37 | .filter(|t| def_name(t).is_some()) | 37 | .filter(|t| def_name(t).is_some()) |
38 | .filter(|t| t.body().is_none()) | ||
38 | .filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t))) | 39 | .filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t))) |
39 | .collect() | 40 | .collect() |
40 | }; | 41 | }; |
@@ -103,11 +104,13 @@ fn build_func_body(def: &ast::FnDef) -> String { | |||
103 | let mut buf = String::new(); | 104 | let mut buf = String::new(); |
104 | 105 | ||
105 | for child in def.syntax().children() { | 106 | for child in def.syntax().children() { |
106 | if child.kind() == SyntaxKind::SEMI { | 107 | match (child.prev_sibling().map(|c| c.kind()), child.kind()) { |
107 | buf.push_str(" { unimplemented!() }") | 108 | (_, SyntaxKind::SEMI) => buf.push_str(" { unimplemented!() }"), |
108 | } else { | 109 | (_, SyntaxKind::ATTR) | (_, SyntaxKind::COMMENT) => {} |
109 | child.text().push_to(&mut buf); | 110 | (Some(SyntaxKind::ATTR), SyntaxKind::WHITESPACE) |
110 | } | 111 | | (Some(SyntaxKind::COMMENT), SyntaxKind::WHITESPACE) => {} |
112 | _ => child.text().push_to(&mut buf), | ||
113 | }; | ||
111 | } | 114 | } |
112 | 115 | ||
113 | buf.trim_end().to_string() | 116 | buf.trim_end().to_string() |
@@ -180,8 +183,7 @@ struct S; | |||
180 | 183 | ||
181 | impl Foo for S { | 184 | impl Foo for S { |
182 | fn bar(&self) {} | 185 | fn bar(&self) {} |
183 | fn foo(&self) { unimplemented!() } | 186 | fn foo(&self) { unimplemented!() }<|> |
184 | fn baz(&self) -> u32 { 42 }<|> | ||
185 | }", | 187 | }", |
186 | ); | 188 | ); |
187 | } | 189 | } |
@@ -193,7 +195,7 @@ impl Foo for S { | |||
193 | " | 195 | " |
194 | trait Foo { fn foo(&self); } | 196 | trait Foo { fn foo(&self); } |
195 | struct S; | 197 | struct S; |
196 | impl Foo for S {<|>}", | 198 | impl Foo for S { <|> }", |
197 | " | 199 | " |
198 | trait Foo { fn foo(&self); } | 200 | trait Foo { fn foo(&self); } |
199 | struct S; | 201 | struct S; |
@@ -232,8 +234,8 @@ impl Foo for S { <|> }", | |||
232 | } | 234 | } |
233 | 235 | ||
234 | #[test] | 236 | #[test] |
235 | fn test_ignore_unnamed_trait_members() { | 237 | fn test_ignore_unnamed_trait_members_and_default_methods() { |
236 | check_assist( | 238 | check_assist_not_applicable( |
237 | add_missing_impl_members, | 239 | add_missing_impl_members, |
238 | " | 240 | " |
239 | trait Foo { | 241 | trait Foo { |
@@ -242,15 +244,6 @@ trait Foo { | |||
242 | } | 244 | } |
243 | struct S; | 245 | struct S; |
244 | impl Foo for S { <|> }", | 246 | impl Foo for S { <|> }", |
245 | " | ||
246 | trait Foo { | ||
247 | fn (arg: u32); | ||
248 | fn valid(some: u32) -> bool { false } | ||
249 | } | ||
250 | struct S; | ||
251 | impl Foo for S { | ||
252 | fn valid(some: u32) -> bool { false }<|> | ||
253 | }", | ||
254 | ) | 247 | ) |
255 | } | 248 | } |
256 | 249 | ||
@@ -260,7 +253,7 @@ impl Foo for S { | |||
260 | add_missing_impl_members, | 253 | add_missing_impl_members, |
261 | " | 254 | " |
262 | trait Foo { | 255 | trait Foo { |
263 | fn valid(some: u32) -> bool { false } | 256 | fn valid(some: u32) -> bool; |
264 | } | 257 | } |
265 | struct S; | 258 | struct S; |
266 | 259 | ||
@@ -269,15 +262,42 @@ mod my_mod { | |||
269 | }", | 262 | }", |
270 | " | 263 | " |
271 | trait Foo { | 264 | trait Foo { |
272 | fn valid(some: u32) -> bool { false } | 265 | fn valid(some: u32) -> bool; |
273 | } | 266 | } |
274 | struct S; | 267 | struct S; |
275 | 268 | ||
276 | mod my_mod { | 269 | mod my_mod { |
277 | impl crate::Foo for S { | 270 | impl crate::Foo for S { |
278 | fn valid(some: u32) -> bool { false }<|> | 271 | fn valid(some: u32) -> bool { unimplemented!() }<|> |
279 | } | 272 | } |
280 | }", | 273 | }", |
281 | ) | 274 | ) |
282 | } | 275 | } |
276 | |||
277 | #[test] | ||
278 | fn test_with_docstring_and_attrs() { | ||
279 | check_assist( | ||
280 | add_missing_impl_members, | ||
281 | r#" | ||
282 | #[doc(alias = "test alias")] | ||
283 | trait Foo { | ||
284 | /// doc string | ||
285 | #[must_use] | ||
286 | fn foo(&self); | ||
287 | } | ||
288 | struct S; | ||
289 | impl Foo for S {}<|>"#, | ||
290 | r#" | ||
291 | #[doc(alias = "test alias")] | ||
292 | trait Foo { | ||
293 | /// doc string | ||
294 | #[must_use] | ||
295 | fn foo(&self); | ||
296 | } | ||
297 | struct S; | ||
298 | impl Foo for S { | ||
299 | fn foo(&self) { unimplemented!() }<|> | ||
300 | }"#, | ||
301 | ) | ||
302 | } | ||
283 | } | 303 | } |