Added support for const generics in impl generation

This commit is contained in:
ivan770 2021-03-27 11:37:39 +02:00
parent 4cb3ecce3f
commit 5a2ef8d0ca
No known key found for this signature in database
GPG Key ID: D8C4BD5AE4D9CC4D
2 changed files with 21 additions and 5 deletions

View File

@ -69,6 +69,17 @@ fn test_add_impl() {
"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<T, const S: usize> {}$0"#,
r#"
struct MyOwnArray<T, const S: usize> {}
impl<T, const S: usize> MyOwnArray<T, S> {
$0
}"#,
);
check_assist(
generate_impl,
r#"
@ -114,11 +125,11 @@ impl<T> Defaulted<T> {
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
}"#,
);

View File

@ -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() {