2 algo::non_trivia_sibling,
7 use crate::{AssistContext, AssistId, Assists};
9 // Assist: flip_trait_bound
11 // Flips two trait bounds.
14 // fn foo<T: Clone +<|> Copy>() { }
18 // fn foo<T: Copy + Clone>() { }
20 pub(crate) fn flip_trait_bound(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
21 // We want to replicate the behavior of `flip_binexpr` by only suggesting
22 // the assist when the cursor is on a `+`
23 let plus = ctx.find_token_at_offset(T![+])?;
25 // Make sure we're in a `TypeBoundList`
26 if ast::TypeBoundList::cast(plus.parent()).is_none() {
30 let (before, after) = (
31 non_trivia_sibling(plus.clone().into(), Direction::Prev)?,
32 non_trivia_sibling(plus.clone().into(), Direction::Next)?,
35 let target = plus.text_range();
36 acc.add(AssistId("flip_trait_bound"), "Flip trait bounds", target, |edit| {
37 edit.replace(before.text_range(), after.to_string());
38 edit.replace(after.text_range(), before.to_string());
46 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
49 fn flip_trait_bound_assist_available() {
50 check_assist_target(flip_trait_bound, "struct S<T> where T: A <|>+ B + C { }", "+")
54 fn flip_trait_bound_not_applicable_for_single_trait_bound() {
55 check_assist_not_applicable(flip_trait_bound, "struct S<T> where T: <|>A { }")
59 fn flip_trait_bound_works_for_struct() {
62 "struct S<T> where T: A <|>+ B { }",
63 "struct S<T> where T: B <|>+ A { }",
68 fn flip_trait_bound_works_for_trait_impl() {
71 "impl X for S<T> where T: A +<|> B { }",
72 "impl X for S<T> where T: B +<|> A { }",
77 fn flip_trait_bound_works_for_fn() {
78 check_assist(flip_trait_bound, "fn f<T: A <|>+ B>(t: T) { }", "fn f<T: B <|>+ A>(t: T) { }")
82 fn flip_trait_bound_works_for_fn_where_clause() {
85 "fn f<T>(t: T) where T: A +<|> B { }",
86 "fn f<T>(t: T) where T: B +<|> A { }",
91 fn flip_trait_bound_works_for_lifetime() {
94 "fn f<T>(t: T) where T: A <|>+ 'static { }",
95 "fn f<T>(t: T) where T: 'static <|>+ A { }",
100 fn flip_trait_bound_works_for_complex_bounds() {
103 "struct S<T> where T: A<T> <|>+ b_mod::B<T> + C<T> { }",
104 "struct S<T> where T: b_mod::B<T> <|>+ A<T> + C<T> { }",
109 fn flip_trait_bound_works_for_long_bounds() {
112 "struct S<T> where T: A + B + C + D + E + F +<|> G + H + I + J { }",
113 "struct S<T> where T: A + B + C + D + E + G +<|> F + H + I + J { }",