From 150ed80c511f7be6390af34e4f2c74c54c274b32 Mon Sep 17 00:00:00 2001 From: Domantas Jadenkus Date: Sat, 13 Feb 2021 23:59:51 +0200 Subject: strip type parameter defaults when generating impl generics --- crates/assists/src/handlers/generate_impl.rs | 24 +++++++++++++++++++++ crates/assists/src/utils.rs | 32 ++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/generate_impl.rs b/crates/assists/src/handlers/generate_impl.rs index 050bcd4e2..16a600e6f 100644 --- a/crates/assists/src/handlers/generate_impl.rs +++ b/crates/assists/src/handlers/generate_impl.rs @@ -98,6 +98,30 @@ mod tests { $0 }"#, ); + + check_assist( + generate_impl, + r#" + struct Defaulted {}$0"#, + r#" + struct Defaulted {} + + impl Defaulted { + $0 + }"#, + ); + + check_assist( + generate_impl, + r#" + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}$0"#, + r#" + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {} + + impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b> Defaulted<'a, 'b, T> { + $0 + }"#, + ); } #[test] diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 98c4462bb..8418e6e12 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -2,6 +2,7 @@ use std::ops; +use ast::TypeBoundsOwner; use hir::{Adt, HasSource}; use ide_db::{helpers::SnippetCap, RootDatabase}; use itertools::Itertools; @@ -377,15 +378,30 @@ pub(crate) fn generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: & } fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str) -> String { - let type_params = adt.generic_param_list(); + let generic_params = adt.generic_param_list(); let mut buf = String::with_capacity(code.len()); buf.push_str("\n\n"); adt.attrs() .filter(|attr| attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false)) .for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str())); buf.push_str("impl"); - if let Some(type_params) = &type_params { - format_to!(buf, "{}", type_params.syntax()); + if let Some(generic_params) = &generic_params { + let lifetimes = generic_params.lifetime_params().map(|lt| format!("{}", lt.syntax())); + let type_params = generic_params.type_params().map(|type_param| { + let mut buf = String::new(); + if let Some(it) = type_param.name() { + format_to!(buf, "{}", it.syntax()); + } + if let Some(it) = type_param.colon_token() { + format_to!(buf, "{} ", it); + } + if let Some(it) = type_param.type_bound_list() { + format_to!(buf, "{}", it.syntax()); + } + buf + }); + let generics = lifetimes.chain(type_params).format(", "); + format_to!(buf, "<{}>", generics); } buf.push(' '); if let Some(trait_text) = trait_text { @@ -393,13 +409,15 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str buf.push_str(" for "); } buf.push_str(adt.name().unwrap().text()); - if let Some(type_params) = type_params { - let lifetime_params = type_params + if let Some(generic_params) = generic_params { + let lifetime_params = generic_params .lifetime_params() .filter_map(|it| it.lifetime()) .map(|it| SmolStr::from(it.text())); - let type_params = - type_params.type_params().filter_map(|it| it.name()).map(|it| SmolStr::from(it.text())); + let type_params = generic_params + .type_params() + .filter_map(|it| it.name()) + .map(|it| SmolStr::from(it.text())); format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", ")) } -- cgit v1.2.3