From 5a2ef8d0cad71cf51da548b80bcfb4ba615061d1 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Sat, 27 Mar 2021 11:37:39 +0200 Subject: Added support for const generics in impl generation --- crates/ide_assists/src/handlers/generate_impl.rs | 17 ++++++++++++++--- crates/ide_assists/src/utils.rs | 9 +++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/generate_impl.rs b/crates/ide_assists/src/handlers/generate_impl.rs index a8e3c4fc2..fd2e250bc 100644 --- a/crates/ide_assists/src/handlers/generate_impl.rs +++ b/crates/ide_assists/src/handlers/generate_impl.rs @@ -69,6 +69,17 @@ mod tests { "struct Foo<'a, T: Foo<'a>> {$0}", "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}", ); + check_assist( + generate_impl, + r#" + struct MyOwnArray {}$0"#, + r#" + struct MyOwnArray {} + + impl MyOwnArray { + $0 + }"#, + ); check_assist( generate_impl, r#" @@ -114,11 +125,11 @@ mod tests { check_assist( generate_impl, r#" - struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}$0"#, + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}$0"#, r#" - struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {} + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {} - impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b> Defaulted<'a, 'b, T> { + impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> { $0 }"#, ); diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 62f959082..728d174db 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -434,7 +434,8 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str } buf }); - let generics = lifetimes.chain(type_params).format(", "); + let const_params = generic_params.const_params().map(|t| t.syntax().to_string()); + let generics = lifetimes.chain(type_params).chain(const_params).format(", "); format_to!(buf, "<{}>", generics); } buf.push(' '); @@ -452,7 +453,11 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str .type_params() .filter_map(|it| it.name()) .map(|it| SmolStr::from(it.text())); - format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", ")) + let const_params = generic_params + .const_params() + .filter_map(|it| it.name()) + .map(|it| SmolStr::from(it.text())); + format_to!(buf, "<{}>", lifetime_params.chain(type_params).chain(const_params).format(", ")) } match adt.where_clause() { -- cgit v1.2.3