From 477fa75cfbdc923be366c75cb03b1e352f63c8e8 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Fri, 4 Sep 2020 15:24:36 +0300 Subject: Fix nitpicks --- .../src/handlers/replace_impl_trait_with_generic.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/assists/src/handlers/replace_impl_trait_with_generic.rs index 5b0d5d971..612c48466 100644 --- a/crates/assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/assists/src/handlers/replace_impl_trait_with_generic.rs @@ -5,13 +5,21 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // Assist: replace_impl_trait_with_generic // // Replaces `impl Trait` function argument with the named generic. +// +// ``` +// fn foo(bar: <|>impl Bar) {} +// ``` +// -> +// ``` +// fn foo(bar: B) {} +// ``` pub(crate) fn replace_impl_trait_with_generic( acc: &mut Assists, ctx: &AssistContext, ) -> Option<()> { let type_impl_trait = ctx.find_node_at_offset::()?; let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; - let type_fn = type_param.syntax().ancestors().nth(2).and_then(ast::Fn::cast)?; + let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; let impl_trait_ty = type_impl_trait .syntax() @@ -27,7 +35,7 @@ pub(crate) fn replace_impl_trait_with_generic( "Replace impl trait with generic", target, |edit| { - let generic_letter = impl_trait_ty[..1].to_string(); + let generic_letter = impl_trait_ty.chars().next().unwrap().to_string(); let generic_param_list = type_fn .generic_param_list() @@ -36,7 +44,7 @@ pub(crate) fn replace_impl_trait_with_generic( let new_type_fn = type_fn .replace_descendant::(type_impl_trait.into(), make::ty(&generic_letter)) - .with_generic_params(generic_param_list); + .with_generic_param_list(generic_param_list); edit.replace_ast(type_fn.clone(), new_type_fn); }, @@ -103,8 +111,6 @@ mod tests { #[test] fn replace_impl_trait_with_empty_multiline_generic_params() { - // FIXME: It would be more correct to place the generic parameter - // on the next line after the left angle. check_assist( replace_impl_trait_with_generic, r#" @@ -147,8 +153,7 @@ mod tests { fn foo< G: Foo, F, - H, - B: Bar, + H, B: Bar, >(bar: B) {} "#, ); -- cgit v1.2.3