From ef0a1b2e58b6047223a68f700ef16fcc21f9f208 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Fri, 4 Sep 2020 17:55:27 +0300 Subject: [PATCH] Fix tests --- .../replace_impl_trait_with_generic.rs | 37 +++++++++++-------- crates/assists/src/tests/generated.rs | 4 +- crates/syntax/src/ast/make.rs | 2 +- 3 files changed, 25 insertions(+), 18 deletions(-) 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 612c48466e..748c528d45 100644 --- a/crates/assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/assists/src/handlers/replace_impl_trait_with_generic.rs @@ -7,11 +7,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // Replaces `impl Trait` function argument with the named generic. // // ``` -// fn foo(bar: <|>impl Bar) {} +// fn foo(bar: <|>impl Bar) {} // ``` // -> // ``` -// fn foo(bar: B) {} +// fn foo(bar: B) {} // ``` pub(crate) fn replace_impl_trait_with_generic( acc: &mut Assists, @@ -21,13 +21,7 @@ pub(crate) fn replace_impl_trait_with_generic( let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; - let impl_trait_ty = type_impl_trait - .syntax() - .descendants() - .last() - .and_then(ast::NameRef::cast)? - .text() - .to_string(); + let impl_trait_ty = type_impl_trait.type_bound_list()?; let target = type_fn.syntax().text_range(); acc.add( @@ -35,7 +29,7 @@ pub(crate) fn replace_impl_trait_with_generic( "Replace impl trait with generic", target, |edit| { - let generic_letter = impl_trait_ty.chars().next().unwrap().to_string(); + let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string(); let generic_param_list = type_fn .generic_param_list() @@ -65,7 +59,7 @@ mod tests { fn foo(bar: <|>impl Bar) {} "#, r#" - fn foo(bar: B) {} + fn foo(bar: B) {} "#, ); } @@ -78,7 +72,7 @@ mod tests { fn foo(bar: <|>impl Bar) {} "#, r#" - fn foo(bar: B) {} + fn foo(bar: B) {} "#, ); } @@ -91,7 +85,7 @@ mod tests { fn foo(foo: impl Foo, bar: <|>impl Bar) {} "#, r#" - fn foo(foo: impl Foo, bar: B) {} + fn foo(foo: impl Foo, bar: B) {} "#, ); } @@ -104,7 +98,7 @@ mod tests { fn foo<>(bar: <|>impl Bar) {} "#, r#" - fn foo(bar: B) {} + fn foo(bar: B) {} "#, ); } @@ -133,7 +127,7 @@ mod tests { fn foo(bar: <|>impl Bar) {} "#, r#" - fn foo(bar: C) {} + fn foo(bar: C) {} "#, ); } @@ -158,4 +152,17 @@ mod tests { "#, ); } + + #[test] + fn replace_impl_trait_multiple() { + check_assist( + replace_impl_trait_with_generic, + r#" + fn foo(bar: <|>impl Foo + Bar) {} + "#, + r#" + fn foo(bar: F) {} + "#, + ); + } } diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index 3963136d82..4e5ca38251 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs @@ -819,10 +819,10 @@ fn doctest_replace_impl_trait_with_generic() { check_doc_test( "replace_impl_trait_with_generic", r#####" -fn foo(bar: <|>impl Bar) {} +fn foo(bar: <|>impl Bar) {} "#####, r#####" -fn foo(bar: B) {} +fn foo(bar: B) {} "#####, ) } diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 7329e3039c..dac4174cd9 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -294,7 +294,7 @@ pub fn param_list(pats: impl IntoIterator) -> ast::ParamList ast_from_text(&format!("fn f({}) {{ }}", args)) } -pub fn generic_param(name: String, ty: Option) -> ast::GenericParam { +pub fn generic_param(name: String, ty: Option) -> ast::GenericParam { let bound = match ty { Some(it) => format!(": {}", it), None => String::new(),