Skip to content

Commit 8638bcb

Browse files
bors[bot]jDomantas
andauthored
Merge #7667
7667: strip type parameter defaults when generating impl generics r=Veykril a=jDomantas Fixes #5666 Co-authored-by: Domantas Jadenkus <[email protected]>
2 parents 84c9970 + 150ed80 commit 8638bcb

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

crates/assists/src/handlers/generate_impl.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ mod tests {
9898
$0
9999
}"#,
100100
);
101+
102+
check_assist(
103+
generate_impl,
104+
r#"
105+
struct Defaulted<T = i32> {}$0"#,
106+
r#"
107+
struct Defaulted<T = i32> {}
108+
109+
impl<T> Defaulted<T> {
110+
$0
111+
}"#,
112+
);
113+
114+
check_assist(
115+
generate_impl,
116+
r#"
117+
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}$0"#,
118+
r#"
119+
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}
120+
121+
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b> Defaulted<'a, 'b, T> {
122+
$0
123+
}"#,
124+
);
101125
}
102126

103127
#[test]

crates/assists/src/utils.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::ops;
44

5+
use ast::TypeBoundsOwner;
56
use hir::{Adt, HasSource};
67
use ide_db::{helpers::SnippetCap, RootDatabase};
78
use itertools::Itertools;
@@ -377,29 +378,46 @@ pub(crate) fn generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: &
377378
}
378379

379380
fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str) -> String {
380-
let type_params = adt.generic_param_list();
381+
let generic_params = adt.generic_param_list();
381382
let mut buf = String::with_capacity(code.len());
382383
buf.push_str("\n\n");
383384
adt.attrs()
384385
.filter(|attr| attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false))
385386
.for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str()));
386387
buf.push_str("impl");
387-
if let Some(type_params) = &type_params {
388-
format_to!(buf, "{}", type_params.syntax());
388+
if let Some(generic_params) = &generic_params {
389+
let lifetimes = generic_params.lifetime_params().map(|lt| format!("{}", lt.syntax()));
390+
let type_params = generic_params.type_params().map(|type_param| {
391+
let mut buf = String::new();
392+
if let Some(it) = type_param.name() {
393+
format_to!(buf, "{}", it.syntax());
394+
}
395+
if let Some(it) = type_param.colon_token() {
396+
format_to!(buf, "{} ", it);
397+
}
398+
if let Some(it) = type_param.type_bound_list() {
399+
format_to!(buf, "{}", it.syntax());
400+
}
401+
buf
402+
});
403+
let generics = lifetimes.chain(type_params).format(", ");
404+
format_to!(buf, "<{}>", generics);
389405
}
390406
buf.push(' ');
391407
if let Some(trait_text) = trait_text {
392408
buf.push_str(trait_text);
393409
buf.push_str(" for ");
394410
}
395411
buf.push_str(adt.name().unwrap().text());
396-
if let Some(type_params) = type_params {
397-
let lifetime_params = type_params
412+
if let Some(generic_params) = generic_params {
413+
let lifetime_params = generic_params
398414
.lifetime_params()
399415
.filter_map(|it| it.lifetime())
400416
.map(|it| SmolStr::from(it.text()));
401-
let type_params =
402-
type_params.type_params().filter_map(|it| it.name()).map(|it| SmolStr::from(it.text()));
417+
let type_params = generic_params
418+
.type_params()
419+
.filter_map(|it| it.name())
420+
.map(|it| SmolStr::from(it.text()));
403421
format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", "))
404422
}
405423

0 commit comments

Comments
 (0)