aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs12
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs62
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};
11use rustc_hash::{FxHashMap, FxHashSet}; 11use rustc_hash::{FxHashMap, FxHashSet};
12 12
13use crate::{assist_config::SnippetCap, utils::render_snippet, AssistContext, AssistId, Assists}; 13use 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 {
81impl FunctionTemplate { 85impl 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::{
11use crate::{ 11use 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
223impl Foo for S { 231impl 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
264impl Foo for S { 272impl 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 { <|> }"#,
283trait Foo { fn foo(&self); } 291trait Foo { fn foo(&self); }
284struct S; 292struct S;
285impl Foo for S { 293impl 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 { <|> }"#,
302trait Foo<T> { fn foo(&self, t: T) -> &T; } 310trait Foo<T> { fn foo(&self, t: T) -> &T; }
303struct S; 311struct S;
304impl Foo<u32> for S { 312impl 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 { <|> }"#,
321trait Foo<T> { fn foo(&self, t: T) -> &T; } 329trait Foo<T> { fn foo(&self, t: T) -> &T; }
322struct S; 330struct S;
323impl<U> Foo<U> for S { 331impl<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 {}<|>"#,
340trait Foo { fn foo(&self); } 348trait Foo { fn foo(&self); }
341struct S; 349struct S;
342impl Foo for S { 350impl 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}
366struct S; 374struct S;
367impl foo::Foo for S { 375impl 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}
391struct S; 399struct S;
392impl foo::Foo for S { 400impl 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}
416struct S; 424struct S;
417impl foo::Foo<u32> for S { 425impl 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 {
443struct Param; 451struct Param;
444struct S; 452struct S;
445impl foo::Foo<Param> for S { 453impl 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}
471struct S; 479struct S;
472impl foo::Foo for S { 480impl 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}
498struct S; 506struct S;
499impl foo::Foo for S { 507impl 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}
523struct S; 531struct S;
524impl foo::Foo for S { 532impl 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}
581struct S; 589struct S;
582impl Foo for S { 590impl 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}
615struct S; 623struct S;
616impl Foo for S { 624impl 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
638struct S; 646struct S;
639impl Foo for S { 647impl 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
663struct S<T>; 671struct S<T>;
664impl Foo<T> for S<T> { 672impl 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}"#,