Skip to content

Commit 477fa75

Browse files
Fix nitpicks
1 parent 7259cc8 commit 477fa75

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

crates/assists/src/handlers/replace_impl_trait_with_generic.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
55
// Assist: replace_impl_trait_with_generic
66
//
77
// Replaces `impl Trait` function argument with the named generic.
8+
//
9+
// ```
10+
// fn foo<G>(bar: <|>impl Bar) {}
11+
// ```
12+
// ->
13+
// ```
14+
// fn foo<B: Bar>(bar: B) {}
15+
// ```
816
pub(crate) fn replace_impl_trait_with_generic(
917
acc: &mut Assists,
1018
ctx: &AssistContext,
1119
) -> Option<()> {
1220
let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
1321
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
14-
let type_fn = type_param.syntax().ancestors().nth(2).and_then(ast::Fn::cast)?;
22+
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?;
1523

1624
let impl_trait_ty = type_impl_trait
1725
.syntax()
@@ -27,7 +35,7 @@ pub(crate) fn replace_impl_trait_with_generic(
2735
"Replace impl trait with generic",
2836
target,
2937
|edit| {
30-
let generic_letter = impl_trait_ty[..1].to_string();
38+
let generic_letter = impl_trait_ty.chars().next().unwrap().to_string();
3139

3240
let generic_param_list = type_fn
3341
.generic_param_list()
@@ -36,7 +44,7 @@ pub(crate) fn replace_impl_trait_with_generic(
3644

3745
let new_type_fn = type_fn
3846
.replace_descendant::<ast::Type>(type_impl_trait.into(), make::ty(&generic_letter))
39-
.with_generic_params(generic_param_list);
47+
.with_generic_param_list(generic_param_list);
4048

4149
edit.replace_ast(type_fn.clone(), new_type_fn);
4250
},
@@ -103,8 +111,6 @@ mod tests {
103111

104112
#[test]
105113
fn replace_impl_trait_with_empty_multiline_generic_params() {
106-
// FIXME: It would be more correct to place the generic parameter
107-
// on the next line after the left angle.
108114
check_assist(
109115
replace_impl_trait_with_generic,
110116
r#"
@@ -147,8 +153,7 @@ mod tests {
147153
fn foo<
148154
G: Foo,
149155
F,
150-
H,
151-
B: Bar,
156+
H, B: Bar,
152157
>(bar: B) {}
153158
"#,
154159
);

crates/syntax/src/ast/edit.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl ast::Fn {
4848
}
4949

5050
#[must_use]
51-
pub fn with_generic_params(&self, generic_args: ast::GenericParamList) -> ast::Fn {
51+
pub fn with_generic_param_list(&self, generic_args: ast::GenericParamList) -> ast::Fn {
5252
if let Some(old) = self.generic_param_list() {
5353
return self.replace_descendant(old, generic_args);
5454
}
@@ -485,17 +485,7 @@ impl ast::GenericParamList {
485485

486486
#[must_use]
487487
pub fn append_param(&self, item: ast::GenericParam) -> ast::GenericParamList {
488-
let is_multiline = self.syntax().text().contains_char('\n');
489-
let ws;
490-
let space = if is_multiline {
491-
ws = tokens::WsBuilder::new(&format!(
492-
"\n{} ",
493-
leading_indent(self.syntax()).unwrap_or_default()
494-
));
495-
ws.ws()
496-
} else {
497-
tokens::single_space()
498-
};
488+
let space = tokens::single_space();
499489

500490
let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new();
501491
if self.generic_params().next().is_some() {
@@ -529,11 +519,6 @@ impl ast::GenericParamList {
529519
};
530520
};
531521

532-
if !is_multiline {
533-
// don't insert comma before angle
534-
to_insert.pop();
535-
}
536-
537522
let position = match self.generic_params().last() {
538523
Some(it) => after_field!(it),
539524
None => after_l_angle!(),

0 commit comments

Comments
 (0)