From b73dbbfbf2cad646eb3f8e3342a1c390a874dc53 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 11:50:43 +0200 Subject: Add missing members generates indented blocks --- .../src/handlers/add_missing_impl_members.rs | 194 ++++++++++++--------- 1 file changed, 116 insertions(+), 78 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 2d6d44980..e466c9a86 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -1,6 +1,10 @@ use hir::HasSource; use ra_syntax::{ - ast::{self, edit, make, AstNode, NameOwner}, + ast::{ + self, + edit::{self, IndentLevel}, + make, AstNode, NameOwner, + }, SmolStr, }; @@ -40,7 +44,9 @@ enum AddMissingImplMembersMode { // } // // impl Trait for () { -// fn foo(&self) -> u32 { todo!() } +// fn foo(&self) -> u32 { +// todo!() +// } // // } // ``` @@ -165,7 +171,9 @@ fn add_missing_impl_members_inner( fn add_body(fn_def: ast::FnDef) -> ast::FnDef { if fn_def.body().is_none() { - fn_def.with_body(make::block_from_expr(make::expr_todo())) + let body = make::block_expr(None, Some(make::expr_todo())); + let body = IndentLevel(1).increase_indent(body); + fn_def.with_body(body) } else { fn_def } @@ -181,7 +189,7 @@ mod tests { fn test_add_missing_impl_members() { check_assist( add_missing_impl_members, - " + r#" trait Foo { type Output; @@ -197,8 +205,8 @@ struct S; impl Foo for S { fn bar(&self) {} <|> -}", - " +}"#, + r#" trait Foo { type Output; @@ -215,10 +223,14 @@ impl Foo for S { fn bar(&self) {} <|>type Output; const CONST: usize = 42; - fn foo(&self) { todo!() } - fn baz(&self) { todo!() } + fn foo(&self) { + todo!() + } + fn baz(&self) { + todo!() + } -}", +}"#, ); } @@ -226,7 +238,7 @@ impl Foo for S { fn test_copied_overriden_members() { check_assist( add_missing_impl_members, - " + r#" trait Foo { fn foo(&self); fn bar(&self) -> bool { true } @@ -238,8 +250,8 @@ struct S; impl Foo for S { fn bar(&self) {} <|> -}", - " +}"#, + r#" trait Foo { fn foo(&self); fn bar(&self) -> bool { true } @@ -250,9 +262,11 @@ struct S; impl Foo for S { fn bar(&self) {} - <|>fn foo(&self) { todo!() } + <|>fn foo(&self) { + todo!() + } -}", +}"#, ); } @@ -260,16 +274,18 @@ impl Foo for S { fn test_empty_impl_def() { check_assist( add_missing_impl_members, - " + r#" trait Foo { fn foo(&self); } struct S; -impl Foo for S { <|> }", - " +impl Foo for S { <|> }"#, + r#" trait Foo { fn foo(&self); } struct S; impl Foo for S { - <|>fn foo(&self) { todo!() } -}", + <|>fn foo(&self) { + todo!() + } +}"#, ); } @@ -277,16 +293,18 @@ impl Foo for S { fn fill_in_type_params_1() { check_assist( add_missing_impl_members, - " + r#" trait Foo { fn foo(&self, t: T) -> &T; } struct S; -impl Foo for S { <|> }", - " +impl Foo for S { <|> }"#, + r#" trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - <|>fn foo(&self, t: u32) -> &u32 { todo!() } -}", + <|>fn foo(&self, t: u32) -> &u32 { + todo!() + } +}"#, ); } @@ -294,16 +312,18 @@ impl Foo for S { fn fill_in_type_params_2() { check_assist( add_missing_impl_members, - " + r#" trait Foo { fn foo(&self, t: T) -> &T; } struct S; -impl Foo for S { <|> }", - " +impl Foo for S { <|> }"#, + r#" trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - <|>fn foo(&self, t: U) -> &U { todo!() } -}", + <|>fn foo(&self, t: U) -> &U { + todo!() + } +}"#, ); } @@ -311,16 +331,18 @@ impl Foo for S { fn test_cursor_after_empty_impl_def() { check_assist( add_missing_impl_members, - " + r#" trait Foo { fn foo(&self); } struct S; -impl Foo for S {}<|>", - " +impl Foo for S {}<|>"#, + r#" trait Foo { fn foo(&self); } struct S; impl Foo for S { - <|>fn foo(&self) { todo!() } -}", + <|>fn foo(&self) { + todo!() + } +}"#, ) } @@ -328,22 +350,24 @@ impl Foo for S { fn test_qualify_path_1() { check_assist( add_missing_impl_members, - " + r#" mod foo { pub struct Bar; trait Foo { fn foo(&self, bar: Bar); } } struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { pub struct Bar; trait Foo { fn foo(&self, bar: Bar); } } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { todo!() } -}", + <|>fn foo(&self, bar: foo::Bar) { + todo!() + } +}"#, ); } @@ -351,22 +375,24 @@ impl foo::Foo for S { fn test_qualify_path_generic() { check_assist( add_missing_impl_members, - " + r#" mod foo { pub struct Bar; trait Foo { fn foo(&self, bar: Bar); } } struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { pub struct Bar; trait Foo { fn foo(&self, bar: Bar); } } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { todo!() } -}", + <|>fn foo(&self, bar: foo::Bar) { + todo!() + } +}"#, ); } @@ -374,22 +400,24 @@ impl foo::Foo for S { fn test_qualify_path_and_substitute_param() { check_assist( add_missing_impl_members, - " + r#" mod foo { pub struct Bar; trait Foo { fn foo(&self, bar: Bar); } } struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { pub struct Bar; trait Foo { fn foo(&self, bar: Bar); } } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { todo!() } -}", + <|>fn foo(&self, bar: foo::Bar) { + todo!() + } +}"#, ); } @@ -398,15 +426,15 @@ impl foo::Foo for S { // when substituting params, the substituted param should not be qualified! check_assist( add_missing_impl_members, - " + r#" mod foo { trait Foo { fn foo(&self, bar: T); } pub struct Param; } struct Param; struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { trait Foo { fn foo(&self, bar: T); } pub struct Param; @@ -414,8 +442,10 @@ mod foo { struct Param; struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: Param) { todo!() } -}", + <|>fn foo(&self, bar: Param) { + todo!() + } +}"#, ); } @@ -423,15 +453,15 @@ impl foo::Foo for S { fn test_qualify_path_associated_item() { check_assist( add_missing_impl_members, - " + r#" mod foo { pub struct Bar; impl Bar { type Assoc = u32; } trait Foo { fn foo(&self, bar: Bar::Assoc); } } struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { pub struct Bar; impl Bar { type Assoc = u32; } @@ -439,8 +469,10 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar::Assoc) { todo!() } -}", + <|>fn foo(&self, bar: foo::Bar::Assoc) { + todo!() + } +}"#, ); } @@ -448,15 +480,15 @@ impl foo::Foo for S { fn test_qualify_path_nested() { check_assist( add_missing_impl_members, - " + r#" mod foo { pub struct Bar; pub struct Baz; trait Foo { fn foo(&self, bar: Bar); } } struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { pub struct Bar; pub struct Baz; @@ -464,8 +496,10 @@ mod foo { } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: foo::Bar) { todo!() } -}", + <|>fn foo(&self, bar: foo::Bar) { + todo!() + } +}"#, ); } @@ -473,22 +507,24 @@ impl foo::Foo for S { fn test_qualify_path_fn_trait_notation() { check_assist( add_missing_impl_members, - " + r#" mod foo { pub trait Fn { type Output; } trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } } struct S; -impl foo::Foo for S { <|> }", - " +impl foo::Foo for S { <|> }"#, + r#" mod foo { pub trait Fn { type Output; } trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } } struct S; impl foo::Foo for S { - <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { todo!() } -}", + <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { + todo!() + } +}"#, ); } @@ -496,10 +532,10 @@ impl foo::Foo for S { fn test_empty_trait() { check_assist_not_applicable( add_missing_impl_members, - " + r#" trait Foo; struct S; -impl Foo for S { <|> }", +impl Foo for S { <|> }"#, ) } @@ -507,13 +543,13 @@ impl Foo for S { <|> }", fn test_ignore_unnamed_trait_members_and_default_methods() { check_assist_not_applicable( add_missing_impl_members, - " + r#" trait Foo { fn (arg: u32); fn valid(some: u32) -> bool { false } } struct S; -impl Foo for S { <|> }", +impl Foo for S { <|> }"#, ) } @@ -544,7 +580,9 @@ trait Foo { struct S; impl Foo for S { <|>type Output; - fn foo(&self) { todo!() } + fn foo(&self) { + todo!() + } }"#, ) } @@ -553,7 +591,7 @@ impl Foo for S { fn test_default_methods() { check_assist( add_missing_default_members, - " + r#" trait Foo { type Output; @@ -563,8 +601,8 @@ trait Foo { fn foo(some: u32) -> bool; } struct S; -impl Foo for S { <|> }", - " +impl Foo for S { <|> }"#, + r#" trait Foo { type Output; @@ -576,7 +614,7 @@ trait Foo { struct S; impl Foo for S { <|>fn valid(some: u32) -> bool { false } -}", +}"#, ) } } -- cgit v1.2.3 From 92665358cd98913e3fef8294e1889cc0bb919e3f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 May 2020 23:56:10 +0800 Subject: Rename ImplItem to AssocItem --- .../src/handlers/add_missing_impl_members.rs | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 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 e466c9a86..e47feda71 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -10,7 +10,7 @@ use ra_syntax::{ use crate::{ ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, - utils::{get_missing_impl_items, resolve_target_trait}, + utils::{get_missing_assoc_items, resolve_target_trait}, Assist, AssistCtx, AssistId, }; @@ -112,25 +112,25 @@ fn add_missing_impl_members_inner( let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?; - let def_name = |item: &ast::ImplItem| -> Option { + let def_name = |item: &ast::AssocItem| -> Option { match item { - ast::ImplItem::FnDef(def) => def.name(), - ast::ImplItem::TypeAliasDef(def) => def.name(), - ast::ImplItem::ConstDef(def) => def.name(), + ast::AssocItem::FnDef(def) => def.name(), + ast::AssocItem::TypeAliasDef(def) => def.name(), + ast::AssocItem::ConstDef(def) => def.name(), } .map(|it| it.text().clone()) }; - let missing_items = get_missing_impl_items(&ctx.sema, &impl_node) + let missing_items = get_missing_assoc_items(&ctx.sema, &impl_node) .iter() .map(|i| match i { - hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value), - hir::AssocItem::TypeAlias(i) => ast::ImplItem::TypeAliasDef(i.source(ctx.db).value), - hir::AssocItem::Const(i) => ast::ImplItem::ConstDef(i.source(ctx.db).value), + hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value), + hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAliasDef(i.source(ctx.db).value), + hir::AssocItem::Const(i) => ast::AssocItem::ConstDef(i.source(ctx.db).value), }) .filter(|t| def_name(&t).is_some()) .filter(|t| match t { - ast::ImplItem::FnDef(def) => match mode { + ast::AssocItem::FnDef(def) => match mode { AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), }, @@ -145,7 +145,7 @@ fn add_missing_impl_members_inner( let sema = ctx.sema; ctx.add_assist(AssistId(assist_id), label, |edit| { - let n_existing_items = impl_item_list.impl_items().count(); + let n_existing_items = impl_item_list.assoc_items().count(); let source_scope = sema.scope_for_def(trait_); let target_scope = sema.scope(impl_item_list.syntax()); let ast_transform = QualifyPaths::new(&target_scope, &source_scope) @@ -154,13 +154,13 @@ fn add_missing_impl_members_inner( .into_iter() .map(|it| ast_transform::apply(&*ast_transform, it)) .map(|it| match it { - ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), + ast::AssocItem::FnDef(def) => ast::AssocItem::FnDef(add_body(def)), _ => it, }) .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.impl_items().nth(n_existing_items).unwrap(); + let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); first_new_item.syntax().text_range().start() }; -- cgit v1.2.3 From 25e6bbde01d4a9cd08fa79db5b8b7da6bbf1a623 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 10:16:55 +0200 Subject: Merge assits::test_helpers and tests --- crates/ra_assists/src/handlers/add_missing_impl_members.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (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 e47feda71..f7a101503 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -181,7 +181,7 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef { #[cfg(test)] mod tests { - use crate::helpers::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; -- cgit v1.2.3 From 233f01c9ba555e5d06f336cb0ff64e7a83e4a23a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 12:51:28 +0200 Subject: Move target to AssistLabel Target is used for assists sorting, so we need it before we compute the action. --- crates/ra_assists/src/handlers/add_missing_impl_members.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 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 f7a101503..7df786590 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -107,10 +107,10 @@ fn add_missing_impl_members_inner( label: &'static str, ) -> Option { let _p = ra_prof::profile("add_missing_impl_members_inner"); - let impl_node = ctx.find_node_at_offset::()?; - let impl_item_list = impl_node.item_list()?; + let impl_def = ctx.find_node_at_offset::()?; + let impl_item_list = impl_def.item_list()?; - let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?; + let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?; let def_name = |item: &ast::AssocItem| -> Option { match item { @@ -121,7 +121,7 @@ fn add_missing_impl_members_inner( .map(|it| it.text().clone()) }; - let missing_items = get_missing_assoc_items(&ctx.sema, &impl_node) + let missing_items = get_missing_assoc_items(&ctx.sema, &impl_def) .iter() .map(|i| match i { hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value), @@ -143,13 +143,13 @@ fn add_missing_impl_members_inner( } let sema = ctx.sema; - - ctx.add_assist(AssistId(assist_id), label, |edit| { + let target = impl_def.syntax().text_range(); + ctx.add_assist(AssistId(assist_id), label, target, |edit| { let n_existing_items = impl_item_list.assoc_items().count(); let source_scope = sema.scope_for_def(trait_); let target_scope = sema.scope(impl_item_list.syntax()); let ast_transform = QualifyPaths::new(&target_scope, &source_scope) - .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_node)); + .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def)); let items = missing_items .into_iter() .map(|it| ast_transform::apply(&*ast_transform, it)) -- cgit v1.2.3 From 4867968d22899395e6551f22641b3617e995140c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 18:45:35 +0200 Subject: Refactor assists API to be more convenient for adding new assists It now duplicates completion API in its shape. --- .../src/handlers/add_missing_impl_members.rs | 32 ++++++++++++---------- 1 file changed, 17 insertions(+), 15 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 7df786590..3482a75bf 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -9,9 +9,10 @@ use ra_syntax::{ }; use crate::{ + assist_context::{AssistContext, Assists}, ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, utils::{get_missing_assoc_items, resolve_target_trait}, - Assist, AssistCtx, AssistId, + AssistId, }; #[derive(PartialEq)] @@ -50,8 +51,9 @@ enum AddMissingImplMembersMode { // // } // ``` -pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option { +pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { add_missing_impl_members_inner( + acc, ctx, AddMissingImplMembersMode::NoDefaultMethods, "add_impl_missing_members", @@ -91,8 +93,9 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option { // // } // ``` -pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option { +pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { add_missing_impl_members_inner( + acc, ctx, AddMissingImplMembersMode::DefaultMethodsOnly, "add_impl_default_members", @@ -101,11 +104,12 @@ pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option { } fn add_missing_impl_members_inner( - ctx: AssistCtx, + acc: &mut Assists, + ctx: &AssistContext, mode: AddMissingImplMembersMode, assist_id: &'static str, label: &'static str, -) -> Option { +) -> Option<()> { let _p = ra_prof::profile("add_missing_impl_members_inner"); let impl_def = ctx.find_node_at_offset::()?; let impl_item_list = impl_def.item_list()?; @@ -142,12 +146,11 @@ fn add_missing_impl_members_inner( return None; } - let sema = ctx.sema; let target = impl_def.syntax().text_range(); - ctx.add_assist(AssistId(assist_id), label, target, |edit| { + acc.add(AssistId(assist_id), label, target, |edit| { let n_existing_items = impl_item_list.assoc_items().count(); - let source_scope = sema.scope_for_def(trait_); - let target_scope = sema.scope(impl_item_list.syntax()); + let source_scope = ctx.sema.scope_for_def(trait_); + let target_scope = ctx.sema.scope(impl_item_list.syntax()); let ast_transform = QualifyPaths::new(&target_scope, &source_scope) .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def)); let items = missing_items @@ -170,13 +173,12 @@ fn add_missing_impl_members_inner( } fn add_body(fn_def: ast::FnDef) -> ast::FnDef { - if fn_def.body().is_none() { - let body = make::block_expr(None, Some(make::expr_todo())); - let body = IndentLevel(1).increase_indent(body); - fn_def.with_body(body) - } else { - fn_def + if fn_def.body().is_some() { + return fn_def; } + let body = make::block_expr(None, Some(make::expr_todo())); + let body = IndentLevel(1).increase_indent(body); + fn_def.with_body(body) } #[cfg(test)] -- cgit v1.2.3 From 231fddab5420ffe5edf7b93609ea21155653254a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 14:40:11 +0200 Subject: More fluent indent API --- crates/ra_assists/src/handlers/add_missing_impl_members.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 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 3482a75bf..c1ce87914 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -2,7 +2,7 @@ use hir::HasSource; use ra_syntax::{ ast::{ self, - edit::{self, IndentLevel}, + edit::{self, AstNodeEdit, IndentLevel}, make, AstNode, NameOwner, }, SmolStr, @@ -176,8 +176,7 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef { if fn_def.body().is_some() { return fn_def; } - let body = make::block_expr(None, Some(make::expr_todo())); - let body = IndentLevel(1).increase_indent(body); + let body = make::block_expr(None, Some(make::expr_todo())).indent(IndentLevel(1)); fn_def.with_body(body) } -- cgit v1.2.3 From 00f3b6c59ae3df9a7bfb1cd8b694d5f9b6a78be4 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Wed, 13 May 2020 16:06:42 +0300 Subject: Correctly fill default type parameters --- .../src/handlers/add_missing_impl_members.rs | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (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 c1ce87914..22e1156d2 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -615,6 +615,56 @@ trait Foo { struct S; impl Foo for S { <|>fn valid(some: u32) -> bool { false } +}"#, + ) + } + + #[test] + fn test_generic_single_default_parameter() { + check_assist( + add_missing_impl_members, + r#" +trait Foo { + fn bar(&self, other: &T); +} + +struct S; +impl Foo for S { <|> }"#, + r#" +trait Foo { + fn bar(&self, other: &T); +} + +struct S; +impl Foo for S { + <|>fn bar(&self, other: &Self) { + todo!() + } +}"#, + ) + } + + #[test] + fn test_generic_default_parameter_is_second() { + check_assist( + add_missing_impl_members, + r#" +trait Foo { + fn bar(&self, this: &T1, that: &T2); +} + +struct S; +impl Foo for S { <|> }"#, + r#" +trait Foo { + fn bar(&self, this: &T1, that: &T2); +} + +struct S; +impl Foo for S { + <|>fn bar(&self, this: &T, that: &Self) { + todo!() + } }"#, ) } -- cgit v1.2.3 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 From 767d169a2ae543f28544e85e15bac1b6aa1cab23 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 02:07:21 +0200 Subject: Better cursor placement when adding impl members --- .../src/handlers/add_missing_impl_members.rs | 84 ++++++++++++---------- 1 file changed, 46 insertions(+), 38 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 d7aa06947..abacd4065 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -45,8 +45,8 @@ enum AddMissingImplMembersMode { // } // // impl Trait for () { -// $0fn foo(&self) -> u32 { -// todo!() +// fn foo(&self) -> u32 { +// ${0:todo!()} // } // // } @@ -167,15 +167,23 @@ fn add_missing_impl_members_inner( 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( + Some(cap) => { + let mut cursor = Cursor::Before(first_new_item.syntax()); + let placeholder; + if let ast::AssocItem::FnDef(func) = &first_new_item { + if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) { + if m.syntax().text() == "todo!()" { + placeholder = m; + cursor = Cursor::Replace(placeholder.syntax()); + } + } + } + builder.replace_snippet( cap, - new_impl_item_list.syntax(), - Cursor::Before(first_new_item.syntax()), - ), - ), + original_range, + render_snippet(cap, new_impl_item_list.syntax(), cursor), + ) + } }; }) } @@ -271,8 +279,8 @@ struct S; impl Foo for S { fn bar(&self) {} - $0fn foo(&self) { - todo!() + fn foo(&self) { + ${0:todo!()} } }"#, @@ -291,8 +299,8 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self); } struct S; impl Foo for S { - $0fn foo(&self) { - todo!() + fn foo(&self) { + ${0:todo!()} } }"#, ); @@ -310,8 +318,8 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - $0fn foo(&self, t: u32) -> &u32 { - todo!() + fn foo(&self, t: u32) -> &u32 { + ${0:todo!()} } }"#, ); @@ -329,8 +337,8 @@ impl Foo for S { <|> }"#, trait Foo { fn foo(&self, t: T) -> &T; } struct S; impl Foo for S { - $0fn foo(&self, t: U) -> &U { - todo!() + fn foo(&self, t: U) -> &U { + ${0:todo!()} } }"#, ); @@ -348,8 +356,8 @@ impl Foo for S {}<|>"#, trait Foo { fn foo(&self); } struct S; impl Foo for S { - $0fn foo(&self) { - todo!() + fn foo(&self) { + ${0:todo!()} } }"#, ) @@ -373,8 +381,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -398,8 +406,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -423,8 +431,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -451,8 +459,8 @@ mod foo { struct Param; struct S; impl foo::Foo for S { - $0fn foo(&self, bar: Param) { - todo!() + fn foo(&self, bar: Param) { + ${0:todo!()} } }"#, ); @@ -478,8 +486,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar::Assoc) { - todo!() + fn foo(&self, bar: foo::Bar::Assoc) { + ${0:todo!()} } }"#, ); @@ -505,8 +513,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: foo::Bar) { - todo!() + fn foo(&self, bar: foo::Bar) { + ${0:todo!()} } }"#, ); @@ -530,8 +538,8 @@ mod foo { } struct S; impl foo::Foo for S { - $0fn foo(&self, bar: dyn Fn(u32) -> i32) { - todo!() + fn foo(&self, bar: dyn Fn(u32) -> i32) { + ${0:todo!()} } }"#, ); @@ -645,8 +653,8 @@ trait Foo { struct S; impl Foo for S { - $0fn bar(&self, other: &Self) { - todo!() + fn bar(&self, other: &Self) { + ${0:todo!()} } }"#, ) @@ -670,8 +678,8 @@ trait Foo { struct S; impl Foo for S { - $0fn bar(&self, this: &T, that: &Self) { - todo!() + fn bar(&self, this: &T, that: &Self) { + ${0:todo!()} } }"#, ) -- cgit v1.2.3