diff options
author | Benjamin Coenen <[email protected]> | 2020-05-21 09:53:29 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-21 09:53:29 +0100 |
commit | a7c8aa7c60c05db66ba4e89ae9e05c82e62507a5 (patch) | |
tree | e848f47bdf5d031c408df94222f595d2efcb2070 /crates/ra_assists/src/handlers/add_missing_impl_members.rs | |
parent | c6143742bd4e625d391ac3ea860be7578ab9f53f (diff) | |
parent | a4e6963a2313971fe7bbec97d03bc67266ef68a9 (diff) |
add support of feature flag for runnables #4464
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers/add_missing_impl_members.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/add_missing_impl_members.rs | 98 |
1 files changed, 57 insertions, 41 deletions
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index 22e1156d2..abacd4065 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs | |||
@@ -11,7 +11,7 @@ use ra_syntax::{ | |||
11 | use crate::{ | 11 | use crate::{ |
12 | assist_context::{AssistContext, Assists}, | 12 | assist_context::{AssistContext, Assists}, |
13 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, | 13 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, |
14 | utils::{get_missing_assoc_items, resolve_target_trait}, | 14 | utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor}, |
15 | AssistId, | 15 | AssistId, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -46,7 +46,7 @@ enum AddMissingImplMembersMode { | |||
46 | // | 46 | // |
47 | // impl Trait<u32> for () { | 47 | // impl Trait<u32> for () { |
48 | // fn foo(&self) -> u32 { | 48 | // fn foo(&self) -> u32 { |
49 | // todo!() | 49 | // ${0:todo!()} |
50 | // } | 50 | // } |
51 | // | 51 | // |
52 | // } | 52 | // } |
@@ -89,7 +89,7 @@ pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) - | |||
89 | // impl Trait for () { | 89 | // impl Trait for () { |
90 | // Type X = (); | 90 | // Type X = (); |
91 | // fn foo(&self) {} | 91 | // fn foo(&self) {} |
92 | // fn bar(&self) {} | 92 | // $0fn bar(&self) {} |
93 | // | 93 | // |
94 | // } | 94 | // } |
95 | // ``` | 95 | // ``` |
@@ -147,7 +147,7 @@ fn add_missing_impl_members_inner( | |||
147 | } | 147 | } |
148 | 148 | ||
149 | let target = impl_def.syntax().text_range(); | 149 | let target = impl_def.syntax().text_range(); |
150 | acc.add(AssistId(assist_id), label, target, |edit| { | 150 | acc.add(AssistId(assist_id), label, target, |builder| { |
151 | let n_existing_items = impl_item_list.assoc_items().count(); | 151 | let n_existing_items = impl_item_list.assoc_items().count(); |
152 | let source_scope = ctx.sema.scope_for_def(trait_); | 152 | let source_scope = ctx.sema.scope_for_def(trait_); |
153 | let target_scope = ctx.sema.scope(impl_item_list.syntax()); | 153 | let target_scope = ctx.sema.scope(impl_item_list.syntax()); |
@@ -162,13 +162,29 @@ fn add_missing_impl_members_inner( | |||
162 | }) | 162 | }) |
163 | .map(|it| edit::remove_attrs_and_docs(&it)); | 163 | .map(|it| edit::remove_attrs_and_docs(&it)); |
164 | let new_impl_item_list = impl_item_list.append_items(items); | 164 | let new_impl_item_list = impl_item_list.append_items(items); |
165 | let cursor_position = { | 165 | let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); |
166 | let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); | 166 | |
167 | first_new_item.syntax().text_range().start() | 167 | let original_range = impl_item_list.syntax().text_range(); |
168 | match ctx.config.snippet_cap { | ||
169 | None => builder.replace(original_range, new_impl_item_list.to_string()), | ||
170 | Some(cap) => { | ||
171 | let mut cursor = Cursor::Before(first_new_item.syntax()); | ||
172 | let placeholder; | ||
173 | if let ast::AssocItem::FnDef(func) = &first_new_item { | ||
174 | if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) { | ||
175 | if m.syntax().text() == "todo!()" { | ||
176 | placeholder = m; | ||
177 | cursor = Cursor::Replace(placeholder.syntax()); | ||
178 | } | ||
179 | } | ||
180 | } | ||
181 | builder.replace_snippet( | ||
182 | cap, | ||
183 | original_range, | ||
184 | render_snippet(cap, new_impl_item_list.syntax(), cursor), | ||
185 | ) | ||
186 | } | ||
168 | }; | 187 | }; |
169 | |||
170 | edit.replace_ast(impl_item_list, new_impl_item_list); | ||
171 | edit.set_cursor(cursor_position); | ||
172 | }) | 188 | }) |
173 | } | 189 | } |
174 | 190 | ||
@@ -222,7 +238,7 @@ struct S; | |||
222 | 238 | ||
223 | impl Foo for S { | 239 | impl Foo for S { |
224 | fn bar(&self) {} | 240 | fn bar(&self) {} |
225 | <|>type Output; | 241 | $0type Output; |
226 | const CONST: usize = 42; | 242 | const CONST: usize = 42; |
227 | fn foo(&self) { | 243 | fn foo(&self) { |
228 | todo!() | 244 | todo!() |
@@ -263,8 +279,8 @@ struct S; | |||
263 | 279 | ||
264 | impl Foo for S { | 280 | impl Foo for S { |
265 | fn bar(&self) {} | 281 | fn bar(&self) {} |
266 | <|>fn foo(&self) { | 282 | fn foo(&self) { |
267 | todo!() | 283 | ${0:todo!()} |
268 | } | 284 | } |
269 | 285 | ||
270 | }"#, | 286 | }"#, |
@@ -283,8 +299,8 @@ impl Foo for S { <|> }"#, | |||
283 | trait Foo { fn foo(&self); } | 299 | trait Foo { fn foo(&self); } |
284 | struct S; | 300 | struct S; |
285 | impl Foo for S { | 301 | impl Foo for S { |
286 | <|>fn foo(&self) { | 302 | fn foo(&self) { |
287 | todo!() | 303 | ${0:todo!()} |
288 | } | 304 | } |
289 | }"#, | 305 | }"#, |
290 | ); | 306 | ); |
@@ -302,8 +318,8 @@ impl Foo<u32> for S { <|> }"#, | |||
302 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 318 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
303 | struct S; | 319 | struct S; |
304 | impl Foo<u32> for S { | 320 | impl Foo<u32> for S { |
305 | <|>fn foo(&self, t: u32) -> &u32 { | 321 | fn foo(&self, t: u32) -> &u32 { |
306 | todo!() | 322 | ${0:todo!()} |
307 | } | 323 | } |
308 | }"#, | 324 | }"#, |
309 | ); | 325 | ); |
@@ -321,8 +337,8 @@ impl<U> Foo<U> for S { <|> }"#, | |||
321 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 337 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
322 | struct S; | 338 | struct S; |
323 | impl<U> Foo<U> for S { | 339 | impl<U> Foo<U> for S { |
324 | <|>fn foo(&self, t: U) -> &U { | 340 | fn foo(&self, t: U) -> &U { |
325 | todo!() | 341 | ${0:todo!()} |
326 | } | 342 | } |
327 | }"#, | 343 | }"#, |
328 | ); | 344 | ); |
@@ -340,8 +356,8 @@ impl Foo for S {}<|>"#, | |||
340 | trait Foo { fn foo(&self); } | 356 | trait Foo { fn foo(&self); } |
341 | struct S; | 357 | struct S; |
342 | impl Foo for S { | 358 | impl Foo for S { |
343 | <|>fn foo(&self) { | 359 | fn foo(&self) { |
344 | todo!() | 360 | ${0:todo!()} |
345 | } | 361 | } |
346 | }"#, | 362 | }"#, |
347 | ) | 363 | ) |
@@ -365,8 +381,8 @@ mod foo { | |||
365 | } | 381 | } |
366 | struct S; | 382 | struct S; |
367 | impl foo::Foo for S { | 383 | impl foo::Foo for S { |
368 | <|>fn foo(&self, bar: foo::Bar) { | 384 | fn foo(&self, bar: foo::Bar) { |
369 | todo!() | 385 | ${0:todo!()} |
370 | } | 386 | } |
371 | }"#, | 387 | }"#, |
372 | ); | 388 | ); |
@@ -390,8 +406,8 @@ mod foo { | |||
390 | } | 406 | } |
391 | struct S; | 407 | struct S; |
392 | impl foo::Foo for S { | 408 | impl foo::Foo for S { |
393 | <|>fn foo(&self, bar: foo::Bar<u32>) { | 409 | fn foo(&self, bar: foo::Bar<u32>) { |
394 | todo!() | 410 | ${0:todo!()} |
395 | } | 411 | } |
396 | }"#, | 412 | }"#, |
397 | ); | 413 | ); |
@@ -415,8 +431,8 @@ mod foo { | |||
415 | } | 431 | } |
416 | struct S; | 432 | struct S; |
417 | impl foo::Foo<u32> for S { | 433 | impl foo::Foo<u32> for S { |
418 | <|>fn foo(&self, bar: foo::Bar<u32>) { | 434 | fn foo(&self, bar: foo::Bar<u32>) { |
419 | todo!() | 435 | ${0:todo!()} |
420 | } | 436 | } |
421 | }"#, | 437 | }"#, |
422 | ); | 438 | ); |
@@ -443,8 +459,8 @@ mod foo { | |||
443 | struct Param; | 459 | struct Param; |
444 | struct S; | 460 | struct S; |
445 | impl foo::Foo<Param> for S { | 461 | impl foo::Foo<Param> for S { |
446 | <|>fn foo(&self, bar: Param) { | 462 | fn foo(&self, bar: Param) { |
447 | todo!() | 463 | ${0:todo!()} |
448 | } | 464 | } |
449 | }"#, | 465 | }"#, |
450 | ); | 466 | ); |
@@ -470,8 +486,8 @@ mod foo { | |||
470 | } | 486 | } |
471 | struct S; | 487 | struct S; |
472 | impl foo::Foo for S { | 488 | impl foo::Foo for S { |
473 | <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { | 489 | fn foo(&self, bar: foo::Bar<u32>::Assoc) { |
474 | todo!() | 490 | ${0:todo!()} |
475 | } | 491 | } |
476 | }"#, | 492 | }"#, |
477 | ); | 493 | ); |
@@ -497,8 +513,8 @@ mod foo { | |||
497 | } | 513 | } |
498 | struct S; | 514 | struct S; |
499 | impl foo::Foo for S { | 515 | impl foo::Foo for S { |
500 | <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { | 516 | fn foo(&self, bar: foo::Bar<foo::Baz>) { |
501 | todo!() | 517 | ${0:todo!()} |
502 | } | 518 | } |
503 | }"#, | 519 | }"#, |
504 | ); | 520 | ); |
@@ -522,8 +538,8 @@ mod foo { | |||
522 | } | 538 | } |
523 | struct S; | 539 | struct S; |
524 | impl foo::Foo for S { | 540 | impl foo::Foo for S { |
525 | <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { | 541 | fn foo(&self, bar: dyn Fn(u32) -> i32) { |
526 | todo!() | 542 | ${0:todo!()} |
527 | } | 543 | } |
528 | }"#, | 544 | }"#, |
529 | ); | 545 | ); |
@@ -580,7 +596,7 @@ trait Foo { | |||
580 | } | 596 | } |
581 | struct S; | 597 | struct S; |
582 | impl Foo for S { | 598 | impl Foo for S { |
583 | <|>type Output; | 599 | $0type Output; |
584 | fn foo(&self) { | 600 | fn foo(&self) { |
585 | todo!() | 601 | todo!() |
586 | } | 602 | } |
@@ -614,7 +630,7 @@ trait Foo { | |||
614 | } | 630 | } |
615 | struct S; | 631 | struct S; |
616 | impl Foo for S { | 632 | impl Foo for S { |
617 | <|>fn valid(some: u32) -> bool { false } | 633 | $0fn valid(some: u32) -> bool { false } |
618 | }"#, | 634 | }"#, |
619 | ) | 635 | ) |
620 | } | 636 | } |
@@ -637,8 +653,8 @@ trait Foo<T = Self> { | |||
637 | 653 | ||
638 | struct S; | 654 | struct S; |
639 | impl Foo for S { | 655 | impl Foo for S { |
640 | <|>fn bar(&self, other: &Self) { | 656 | fn bar(&self, other: &Self) { |
641 | todo!() | 657 | ${0:todo!()} |
642 | } | 658 | } |
643 | }"#, | 659 | }"#, |
644 | ) | 660 | ) |
@@ -662,8 +678,8 @@ trait Foo<T1, T2 = Self> { | |||
662 | 678 | ||
663 | struct S<T>; | 679 | struct S<T>; |
664 | impl Foo<T> for S<T> { | 680 | impl Foo<T> for S<T> { |
665 | <|>fn bar(&self, this: &T, that: &Self) { | 681 | fn bar(&self, this: &T, that: &Self) { |
666 | todo!() | 682 | ${0:todo!()} |
667 | } | 683 | } |
668 | }"#, | 684 | }"#, |
669 | ) | 685 | ) |