From a04cababaa144d7a6db7b1dd114494b33d281ab9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 01:53:21 +0200 Subject: Use snippets in add_missing_members --- .../src/handlers/add_missing_impl_members.rs | 62 ++++++++++++---------- 1 file changed, 35 insertions(+), 27 deletions(-) (limited to 'crates/ra_assists/src/handlers/add_missing_impl_members.rs') 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::{ use crate::{ assist_context::{AssistContext, Assists}, ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, - utils::{get_missing_assoc_items, resolve_target_trait}, + utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor}, AssistId, }; @@ -45,7 +45,7 @@ enum AddMissingImplMembersMode { // } // // impl Trait for () { -// fn foo(&self) -> u32 { +// $0fn foo(&self) -> u32 { // todo!() // } // @@ -89,7 +89,7 @@ pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) - // impl Trait for () { // Type X = (); // fn foo(&self) {} -// fn bar(&self) {} +// $0fn bar(&self) {} // // } // ``` @@ -147,7 +147,7 @@ fn add_missing_impl_members_inner( } let target = impl_def.syntax().text_range(); - acc.add(AssistId(assist_id), label, target, |edit| { + acc.add(AssistId(assist_id), label, target, |builder| { let n_existing_items = impl_item_list.assoc_items().count(); let source_scope = ctx.sema.scope_for_def(trait_); let target_scope = ctx.sema.scope(impl_item_list.syntax()); @@ -162,13 +162,21 @@ fn add_missing_impl_members_inner( }) .map(|it| edit::remove_attrs_and_docs(&it)); let new_impl_item_list = impl_item_list.append_items(items); - let cursor_position = { - let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); - first_new_item.syntax().text_range().start() + let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); + + let original_range = impl_item_list.syntax().text_range(); + match ctx.config.snippet_cap { + None => builder.replace(original_range, new_impl_item_list.to_string()), + Some(cap) => builder.replace_snippet( + cap, + original_range, + render_snippet( + cap, + new_impl_item_list.syntax(), + Cursor::Before(first_new_item.syntax()), + ), + ), }; - - edit.replace_ast(impl_item_list, new_impl_item_list); - edit.set_cursor(cursor_position); }) } @@ -222,7 +230,7 @@ struct S; impl Foo for S { fn bar(&self) {} - <|>type Output; + $0type Output; const CONST: usize = 42; fn foo(&self) { todo!() @@ -263,7 +271,7 @@ struct S; impl Foo for S { fn bar(&self) {} - <|>fn foo(&self) { + $0fn foo(&self) { todo!() } @@ -283,7 +291,7 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self); } struct S; impl Foo for S { - <|>fn foo(&self) { + $0fn foo(&self) { todo!() } }"#, @@ -302,7 +310,7 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - <|>fn foo(&self, t: u32) -> &u32 { + $0fn foo(&self, t: u32) -> &u32 { todo!() } }"#, @@ -321,7 +329,7 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - <|>fn foo(&self, t: U) -> &U { + $0fn foo(&self, t: U) -> &U { todo!() } }"#, @@ -340,7 +348,7 @@ impl Foo for S {}<|>"#, trait Foo { fn foo(&self); } struct S; impl Foo for S { - <|>fn foo(&self) { + $0fn foo(&self) { todo!() } }"#, @@ -365,7 +373,7 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { + $0fn foo(&self, bar: foo::Bar) { todo!() } }"#, @@ -390,7 +398,7 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { + $0fn foo(&self, bar: foo::Bar) { todo!() } }"#, @@ -415,7 +423,7 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { + $0fn foo(&self, bar: foo::Bar) { todo!() } }"#, @@ -443,7 +451,7 @@ mod foo { struct Param; struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: Param) { + $0fn foo(&self, bar: Param) { todo!() } }"#, @@ -470,7 +478,7 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar::Assoc) { + $0fn foo(&self, bar: foo::Bar::Assoc) { todo!() } }"#, @@ -497,7 +505,7 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { + $0fn foo(&self, bar: foo::Bar) { todo!() } }"#, @@ -522,7 +530,7 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { + $0fn foo(&self, bar: dyn Fn(u32) -> i32) { todo!() } }"#, @@ -580,7 +588,7 @@ trait Foo { } struct S; impl Foo for S { - <|>type Output; + $0type Output; fn foo(&self) { todo!() } @@ -614,7 +622,7 @@ trait Foo { } struct S; impl Foo for S { - <|>fn valid(some: u32) -> bool { false } + $0fn valid(some: u32) -> bool { false } }"#, ) } @@ -637,7 +645,7 @@ trait Foo { struct S; impl Foo for S { - <|>fn bar(&self, other: &Self) { + $0fn bar(&self, other: &Self) { todo!() } }"#, @@ -662,7 +670,7 @@ trait Foo { struct S; impl Foo for S { - <|>fn bar(&self, this: &T, that: &Self) { + $0fn bar(&self, this: &T, that: &Self) { todo!() } }"#, -- cgit v1.2.3