diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-20 00:54:08 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-20 00:54:08 +0100 |
commit | b26dbf80090ccdd753b35d6254246229e9ff8728 (patch) | |
tree | b54b03a1220f6977d77ba5e27a82c10c5e386eb0 /crates/ra_assists/src/handlers | |
parent | a36202390caff0ed97ff778cf4556fc00ddee70d (diff) | |
parent | a04cababaa144d7a6db7b1dd114494b33d281ab9 (diff) |
Merge #4524
4524: Use snippets in add_missing_members r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r-- | crates/ra_assists/src/handlers/add_function.rs | 12 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_missing_impl_members.rs | 62 |
2 files changed, 45 insertions, 29 deletions
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index a0709630d..24f931a85 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs | |||
@@ -10,7 +10,11 @@ use ra_syntax::{ | |||
10 | }; | 10 | }; |
11 | use rustc_hash::{FxHashMap, FxHashSet}; | 11 | use rustc_hash::{FxHashMap, FxHashSet}; |
12 | 12 | ||
13 | use crate::{assist_config::SnippetCap, utils::render_snippet, AssistContext, AssistId, Assists}; | 13 | use crate::{ |
14 | assist_config::SnippetCap, | ||
15 | utils::{render_snippet, Cursor}, | ||
16 | AssistContext, AssistId, Assists, | ||
17 | }; | ||
14 | 18 | ||
15 | // Assist: add_function | 19 | // Assist: add_function |
16 | // | 20 | // |
@@ -81,7 +85,11 @@ struct FunctionTemplate { | |||
81 | impl FunctionTemplate { | 85 | impl FunctionTemplate { |
82 | fn to_string(&self, cap: Option<SnippetCap>) -> String { | 86 | fn to_string(&self, cap: Option<SnippetCap>) -> String { |
83 | let f = match cap { | 87 | let f = match cap { |
84 | Some(cap) => render_snippet(cap, self.fn_def.syntax(), self.placeholder_expr.syntax()), | 88 | Some(cap) => render_snippet( |
89 | cap, | ||
90 | self.fn_def.syntax(), | ||
91 | Cursor::Replace(self.placeholder_expr.syntax()), | ||
92 | ), | ||
85 | None => self.fn_def.to_string(), | 93 | None => self.fn_def.to_string(), |
86 | }; | 94 | }; |
87 | format!("{}{}{}", self.leading_ws, f, self.trailing_ws) | 95 | format!("{}{}{}", self.leading_ws, f, self.trailing_ws) |
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..d7aa06947 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 | ||
@@ -45,7 +45,7 @@ enum AddMissingImplMembersMode { | |||
45 | // } | 45 | // } |
46 | // | 46 | // |
47 | // impl Trait<u32> for () { | 47 | // impl Trait<u32> for () { |
48 | // fn foo(&self) -> u32 { | 48 | // $0fn foo(&self) -> u32 { |
49 | // todo!() | 49 | // todo!() |
50 | // } | 50 | // } |
51 | // | 51 | // |
@@ -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,21 @@ 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) => builder.replace_snippet( | ||
171 | cap, | ||
172 | original_range, | ||
173 | render_snippet( | ||
174 | cap, | ||
175 | new_impl_item_list.syntax(), | ||
176 | Cursor::Before(first_new_item.syntax()), | ||
177 | ), | ||
178 | ), | ||
168 | }; | 179 | }; |
169 | |||
170 | edit.replace_ast(impl_item_list, new_impl_item_list); | ||
171 | edit.set_cursor(cursor_position); | ||
172 | }) | 180 | }) |
173 | } | 181 | } |
174 | 182 | ||
@@ -222,7 +230,7 @@ struct S; | |||
222 | 230 | ||
223 | impl Foo for S { | 231 | impl Foo for S { |
224 | fn bar(&self) {} | 232 | fn bar(&self) {} |
225 | <|>type Output; | 233 | $0type Output; |
226 | const CONST: usize = 42; | 234 | const CONST: usize = 42; |
227 | fn foo(&self) { | 235 | fn foo(&self) { |
228 | todo!() | 236 | todo!() |
@@ -263,7 +271,7 @@ struct S; | |||
263 | 271 | ||
264 | impl Foo for S { | 272 | impl Foo for S { |
265 | fn bar(&self) {} | 273 | fn bar(&self) {} |
266 | <|>fn foo(&self) { | 274 | $0fn foo(&self) { |
267 | todo!() | 275 | todo!() |
268 | } | 276 | } |
269 | 277 | ||
@@ -283,7 +291,7 @@ impl Foo for S { <|> }"#, | |||
283 | trait Foo { fn foo(&self); } | 291 | trait Foo { fn foo(&self); } |
284 | struct S; | 292 | struct S; |
285 | impl Foo for S { | 293 | impl Foo for S { |
286 | <|>fn foo(&self) { | 294 | $0fn foo(&self) { |
287 | todo!() | 295 | todo!() |
288 | } | 296 | } |
289 | }"#, | 297 | }"#, |
@@ -302,7 +310,7 @@ impl Foo<u32> for S { <|> }"#, | |||
302 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 310 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
303 | struct S; | 311 | struct S; |
304 | impl Foo<u32> for S { | 312 | impl Foo<u32> for S { |
305 | <|>fn foo(&self, t: u32) -> &u32 { | 313 | $0fn foo(&self, t: u32) -> &u32 { |
306 | todo!() | 314 | todo!() |
307 | } | 315 | } |
308 | }"#, | 316 | }"#, |
@@ -321,7 +329,7 @@ impl<U> Foo<U> for S { <|> }"#, | |||
321 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 329 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
322 | struct S; | 330 | struct S; |
323 | impl<U> Foo<U> for S { | 331 | impl<U> Foo<U> for S { |
324 | <|>fn foo(&self, t: U) -> &U { | 332 | $0fn foo(&self, t: U) -> &U { |
325 | todo!() | 333 | todo!() |
326 | } | 334 | } |
327 | }"#, | 335 | }"#, |
@@ -340,7 +348,7 @@ impl Foo for S {}<|>"#, | |||
340 | trait Foo { fn foo(&self); } | 348 | trait Foo { fn foo(&self); } |
341 | struct S; | 349 | struct S; |
342 | impl Foo for S { | 350 | impl Foo for S { |
343 | <|>fn foo(&self) { | 351 | $0fn foo(&self) { |
344 | todo!() | 352 | todo!() |
345 | } | 353 | } |
346 | }"#, | 354 | }"#, |
@@ -365,7 +373,7 @@ mod foo { | |||
365 | } | 373 | } |
366 | struct S; | 374 | struct S; |
367 | impl foo::Foo for S { | 375 | impl foo::Foo for S { |
368 | <|>fn foo(&self, bar: foo::Bar) { | 376 | $0fn foo(&self, bar: foo::Bar) { |
369 | todo!() | 377 | todo!() |
370 | } | 378 | } |
371 | }"#, | 379 | }"#, |
@@ -390,7 +398,7 @@ mod foo { | |||
390 | } | 398 | } |
391 | struct S; | 399 | struct S; |
392 | impl foo::Foo for S { | 400 | impl foo::Foo for S { |
393 | <|>fn foo(&self, bar: foo::Bar<u32>) { | 401 | $0fn foo(&self, bar: foo::Bar<u32>) { |
394 | todo!() | 402 | todo!() |
395 | } | 403 | } |
396 | }"#, | 404 | }"#, |
@@ -415,7 +423,7 @@ mod foo { | |||
415 | } | 423 | } |
416 | struct S; | 424 | struct S; |
417 | impl foo::Foo<u32> for S { | 425 | impl foo::Foo<u32> for S { |
418 | <|>fn foo(&self, bar: foo::Bar<u32>) { | 426 | $0fn foo(&self, bar: foo::Bar<u32>) { |
419 | todo!() | 427 | todo!() |
420 | } | 428 | } |
421 | }"#, | 429 | }"#, |
@@ -443,7 +451,7 @@ mod foo { | |||
443 | struct Param; | 451 | struct Param; |
444 | struct S; | 452 | struct S; |
445 | impl foo::Foo<Param> for S { | 453 | impl foo::Foo<Param> for S { |
446 | <|>fn foo(&self, bar: Param) { | 454 | $0fn foo(&self, bar: Param) { |
447 | todo!() | 455 | todo!() |
448 | } | 456 | } |
449 | }"#, | 457 | }"#, |
@@ -470,7 +478,7 @@ mod foo { | |||
470 | } | 478 | } |
471 | struct S; | 479 | struct S; |
472 | impl foo::Foo for S { | 480 | impl foo::Foo for S { |
473 | <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { | 481 | $0fn foo(&self, bar: foo::Bar<u32>::Assoc) { |
474 | todo!() | 482 | todo!() |
475 | } | 483 | } |
476 | }"#, | 484 | }"#, |
@@ -497,7 +505,7 @@ mod foo { | |||
497 | } | 505 | } |
498 | struct S; | 506 | struct S; |
499 | impl foo::Foo for S { | 507 | impl foo::Foo for S { |
500 | <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { | 508 | $0fn foo(&self, bar: foo::Bar<foo::Baz>) { |
501 | todo!() | 509 | todo!() |
502 | } | 510 | } |
503 | }"#, | 511 | }"#, |
@@ -522,7 +530,7 @@ mod foo { | |||
522 | } | 530 | } |
523 | struct S; | 531 | struct S; |
524 | impl foo::Foo for S { | 532 | impl foo::Foo for S { |
525 | <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { | 533 | $0fn foo(&self, bar: dyn Fn(u32) -> i32) { |
526 | todo!() | 534 | todo!() |
527 | } | 535 | } |
528 | }"#, | 536 | }"#, |
@@ -580,7 +588,7 @@ trait Foo { | |||
580 | } | 588 | } |
581 | struct S; | 589 | struct S; |
582 | impl Foo for S { | 590 | impl Foo for S { |
583 | <|>type Output; | 591 | $0type Output; |
584 | fn foo(&self) { | 592 | fn foo(&self) { |
585 | todo!() | 593 | todo!() |
586 | } | 594 | } |
@@ -614,7 +622,7 @@ trait Foo { | |||
614 | } | 622 | } |
615 | struct S; | 623 | struct S; |
616 | impl Foo for S { | 624 | impl Foo for S { |
617 | <|>fn valid(some: u32) -> bool { false } | 625 | $0fn valid(some: u32) -> bool { false } |
618 | }"#, | 626 | }"#, |
619 | ) | 627 | ) |
620 | } | 628 | } |
@@ -637,7 +645,7 @@ trait Foo<T = Self> { | |||
637 | 645 | ||
638 | struct S; | 646 | struct S; |
639 | impl Foo for S { | 647 | impl Foo for S { |
640 | <|>fn bar(&self, other: &Self) { | 648 | $0fn bar(&self, other: &Self) { |
641 | todo!() | 649 | todo!() |
642 | } | 650 | } |
643 | }"#, | 651 | }"#, |
@@ -662,7 +670,7 @@ trait Foo<T1, T2 = Self> { | |||
662 | 670 | ||
663 | struct S<T>; | 671 | struct S<T>; |
664 | impl Foo<T> for S<T> { | 672 | impl Foo<T> for S<T> { |
665 | <|>fn bar(&self, this: &T, that: &Self) { | 673 | $0fn bar(&self, this: &T, that: &Self) { |
666 | todo!() | 674 | todo!() |
667 | } | 675 | } |
668 | }"#, | 676 | }"#, |