From edeb4927829380ed25e3899e85b2809bbb39a846 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 17:41:25 +0300 Subject: minor: fix test style --- .../handlers/replace_impl_trait_with_generic.rs | 86 ++++++++-------------- 1 file changed, 31 insertions(+), 55 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs index ff25b61ea..8509a5e11 100644 --- a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs @@ -55,12 +55,8 @@ mod tests { fn replace_impl_trait_with_generic_params() { check_assist( replace_impl_trait_with_generic, - r#" - fn foo(bar: $0impl Bar) {} - "#, - r#" - fn foo(bar: B) {} - "#, + r#"fn foo(bar: $0impl Bar) {}"#, + r#"fn foo(bar: B) {}"#, ); } @@ -68,12 +64,8 @@ mod tests { fn replace_impl_trait_without_generic_params() { check_assist( replace_impl_trait_with_generic, - r#" - fn foo(bar: $0impl Bar) {} - "#, - r#" - fn foo(bar: B) {} - "#, + r#"fn foo(bar: $0impl Bar) {}"#, + r#"fn foo(bar: B) {}"#, ); } @@ -81,12 +73,8 @@ mod tests { fn replace_two_impl_trait_with_generic_params() { check_assist( replace_impl_trait_with_generic, - r#" - fn foo(foo: impl Foo, bar: $0impl Bar) {} - "#, - r#" - fn foo(foo: impl Foo, bar: B) {} - "#, + r#"fn foo(foo: impl Foo, bar: $0impl Bar) {}"#, + r#"fn foo(foo: impl Foo, bar: B) {}"#, ); } @@ -94,12 +82,8 @@ mod tests { fn replace_impl_trait_with_empty_generic_params() { check_assist( replace_impl_trait_with_generic, - r#" - fn foo<>(bar: $0impl Bar) {} - "#, - r#" - fn foo(bar: B) {} - "#, + r#"fn foo<>(bar: $0impl Bar) {}"#, + r#"fn foo(bar: B) {}"#, ); } @@ -108,13 +92,13 @@ mod tests { check_assist( replace_impl_trait_with_generic, r#" - fn foo< - >(bar: $0impl Bar) {} - "#, +fn foo< +>(bar: $0impl Bar) {} +"#, r#" - fn foo(bar: B) {} - "#, +fn foo(bar: B) {} +"#, ); } @@ -123,12 +107,8 @@ mod tests { fn replace_impl_trait_with_exist_generic_letter() { check_assist( replace_impl_trait_with_generic, - r#" - fn foo(bar: $0impl Bar) {} - "#, - r#" - fn foo(bar: C) {} - "#, + r#"fn foo(bar: $0impl Bar) {}"#, + r#"fn foo(bar: C) {}"#, ); } @@ -137,19 +117,19 @@ mod tests { check_assist( replace_impl_trait_with_generic, r#" - fn foo< - G: Foo, - F, - H, - >(bar: $0impl Bar) {} - "#, - r#" - fn foo< - G: Foo, - F, - H, B: Bar - >(bar: B) {} - "#, +fn foo< + G: Foo, + F, + H, +>(bar: $0impl Bar) {} +"#, + r#" +fn foo< + G: Foo, + F, + H, B: Bar +>(bar: B) {} +"#, ); } @@ -157,12 +137,8 @@ mod tests { fn replace_impl_trait_multiple() { check_assist( replace_impl_trait_with_generic, - r#" - fn foo(bar: $0impl Foo + Bar) {} - "#, - r#" - fn foo(bar: F) {} - "#, + r#"fn foo(bar: $0impl Foo + Bar) {}"#, + r#"fn foo(bar: F) {}"#, ); } } -- cgit v1.2.3 From d9c9f6dc2cd3080009c4ce2c3f0f340949f4f53c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 17:58:03 +0300 Subject: cleanups --- .../src/handlers/introduce_named_lifetime.rs | 4 ++-- .../handlers/replace_impl_trait_with_generic.rs | 24 +++++++++++----------- crates/ide_assists/src/utils/suggest_name.rs | 10 ++++++++- 3 files changed, 23 insertions(+), 15 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/introduce_named_lifetime.rs b/crates/ide_assists/src/handlers/introduce_named_lifetime.rs index 9f4f71d6c..cb71ca8bd 100644 --- a/crates/ide_assists/src/handlers/introduce_named_lifetime.rs +++ b/crates/ide_assists/src/handlers/introduce_named_lifetime.rs @@ -139,12 +139,12 @@ fn generate_unique_lifetime_param_name( fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) { let generic_param = - make::generic_param(format!("'{}", new_lifetime_param), None).clone_for_update(); + make::generic_param(&format!("'{}", new_lifetime_param), None).clone_for_update(); type_params.add_generic_param(generic_param); } fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime { - make::generic_param(format!("'{}", new_lifetime_param), None) + make::generic_param(&format!("'{}", new_lifetime_param), None) .syntax() .descendants() .find_map(ast::Lifetime::cast) diff --git a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs index 8509a5e11..26a0e81f0 100644 --- a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs @@ -1,6 +1,6 @@ use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; // Assist: replace_impl_trait_with_generic // @@ -17,30 +17,30 @@ 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().find_map(ast::Fn::cast)?; + let impl_trait_type = ctx.find_node_at_offset::()?; + let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?; + let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?; - let impl_trait_ty = type_impl_trait.type_bound_list()?; + let type_bound_list = impl_trait_type.type_bound_list()?; - let target = type_fn.syntax().text_range(); + let target = fn_.syntax().text_range(); acc.add( AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite), "Replace impl trait with generic", target, |edit| { - let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string(); + let type_param_name = suggest_name::generic_parameter(&impl_trait_type); - let generic_param_list = type_fn + let generic_param_list = fn_ .generic_param_list() .unwrap_or_else(|| make::generic_param_list(None)) - .append_param(make::generic_param(generic_letter.clone(), Some(impl_trait_ty))); + .append_param(make::generic_param(&type_param_name, Some(type_bound_list))); - let new_type_fn = type_fn - .replace_descendant::(type_impl_trait.into(), make::ty(&generic_letter)) + let new_type_fn = fn_ + .replace_descendant::(impl_trait_type.into(), make::ty(&type_param_name)) .with_generic_param_list(generic_param_list); - edit.replace_ast(type_fn.clone(), new_type_fn); + edit.replace_ast(fn_.clone(), new_type_fn); }, ) } diff --git a/crates/ide_assists/src/utils/suggest_name.rs b/crates/ide_assists/src/utils/suggest_name.rs index deafcd630..c8487846d 100644 --- a/crates/ide_assists/src/utils/suggest_name.rs +++ b/crates/ide_assists/src/utils/suggest_name.rs @@ -6,7 +6,7 @@ use itertools::Itertools; use stdx::to_lower_snake_case; use syntax::{ ast::{self, NameOwner}, - match_ast, AstNode, + match_ast, AstNode, SmolStr, }; /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` @@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[ "iter_mut", ]; +pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { + let c = ty + .type_bound_list() + .and_then(|bounds| bounds.syntax().text().char_at(0.into())) + .unwrap_or('T'); + c.encode_utf8(&mut [0; 4]).into() +} + /// Suggest name of variable for given expression /// /// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name. -- cgit v1.2.3 From 984d20aad8445ecbdc05d1dc3ea2de104c685af0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 17:59:52 +0300 Subject: cleanups --- crates/ide_assists/src/handlers/extract_variable.rs | 2 +- crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs | 2 +- crates/ide_assists/src/utils/suggest_name.rs | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/extract_variable.rs b/crates/ide_assists/src/handlers/extract_variable.rs index 136b9a55b..ae084c86c 100644 --- a/crates/ide_assists/src/handlers/extract_variable.rs +++ b/crates/ide_assists/src/handlers/extract_variable.rs @@ -54,7 +54,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option let var_name = match &field_shorthand { Some(it) => it.to_string(), - None => suggest_name::variable(&to_extract, &ctx.sema), + None => suggest_name::for_variable(&to_extract, &ctx.sema), }; let expr_range = match &field_shorthand { Some(it) => it.syntax().text_range().cover(to_extract.syntax().text_range()), diff --git a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs index 26a0e81f0..899c773df 100644 --- a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs @@ -29,7 +29,7 @@ pub(crate) fn replace_impl_trait_with_generic( "Replace impl trait with generic", target, |edit| { - let type_param_name = suggest_name::generic_parameter(&impl_trait_type); + let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type); let generic_param_list = fn_ .generic_param_list() diff --git a/crates/ide_assists/src/utils/suggest_name.rs b/crates/ide_assists/src/utils/suggest_name.rs index c8487846d..b3aabeab3 100644 --- a/crates/ide_assists/src/utils/suggest_name.rs +++ b/crates/ide_assists/src/utils/suggest_name.rs @@ -57,7 +57,7 @@ const USELESS_METHODS: &[&str] = &[ "iter_mut", ]; -pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { +pub(crate) fn for_generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { let c = ty .type_bound_list() .and_then(|bounds| bounds.syntax().text().char_at(0.into())) @@ -83,7 +83,8 @@ pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { /// It also applies heuristics to filter out less informative names /// /// Currently it sticks to the first name found. -pub(crate) fn variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String { +// FIXME: Microoptimize and return a `SmolStr` here. +pub(crate) fn for_variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String { // `from_param` does not benifit from stripping // it need the largest context possible // so we check firstmost @@ -284,7 +285,7 @@ mod tests { frange.range, "selection is not an expression(yet contained in one)" ); - let name = variable(&expr, &sema); + let name = for_variable(&expr, &sema); assert_eq!(&name, expected); } -- cgit v1.2.3 From 5342800147679a0ded5546322c94aa6339d58fbc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 18:20:37 +0300 Subject: internal: rewrite **Repalce impl Trait** assist to mutable syntax trees --- .../handlers/replace_impl_trait_with_generic.rs | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs index 899c773df..16cae0281 100644 --- a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs @@ -1,4 +1,7 @@ -use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; +use syntax::{ + ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode}, + ted, +}; use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; @@ -29,18 +32,17 @@ pub(crate) fn replace_impl_trait_with_generic( "Replace impl trait with generic", target, |edit| { - let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type); + let impl_trait_type = edit.make_ast_mut(impl_trait_type); + let fn_ = edit.make_ast_mut(fn_); - let generic_param_list = fn_ - .generic_param_list() - .unwrap_or_else(|| make::generic_param_list(None)) - .append_param(make::generic_param(&type_param_name, Some(type_bound_list))); + let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type); - let new_type_fn = fn_ - .replace_descendant::(impl_trait_type.into(), make::ty(&type_param_name)) - .with_generic_param_list(generic_param_list); + let type_param = + make::generic_param(&type_param_name, Some(type_bound_list)).clone_for_update(); + let new_ty = make::ty(&type_param_name).clone_for_update(); - edit.replace_ast(fn_.clone(), new_type_fn); + ted::replace(impl_trait_type.syntax(), new_ty.syntax()); + fn_.get_or_create_generic_param_list().add_generic_param(type_param) }, ) } @@ -127,7 +129,7 @@ fn foo< fn foo< G: Foo, F, - H, B: Bar + H, B: Bar, >(bar: B) {} "#, ); -- cgit v1.2.3