]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_bounds.rs
Rollup merge of #103996 - SUPERCILEX:docs, r=RalfJung
[rust.git] / src / tools / rust-analyzer / crates / ide-assists / src / handlers / move_bounds.rs
1 use syntax::{
2     ast::{
3         self,
4         edit_in_place::{GenericParamsOwnerEdit, Removable},
5         make, AstNode, HasName, HasTypeBounds,
6     },
7     match_ast,
8 };
9
10 use crate::{AssistContext, AssistId, AssistKind, Assists};
11
12 // Assist: move_bounds_to_where_clause
13 //
14 // Moves inline type bounds to a where clause.
15 //
16 // ```
17 // fn apply<T, U, $0F: FnOnce(T) -> U>(f: F, x: T) -> U {
18 //     f(x)
19 // }
20 // ```
21 // ->
22 // ```
23 // fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
24 //     f(x)
25 // }
26 // ```
27 pub(crate) fn move_bounds_to_where_clause(
28     acc: &mut Assists,
29     ctx: &AssistContext<'_>,
30 ) -> Option<()> {
31     let type_param_list = ctx.find_node_at_offset::<ast::GenericParamList>()?;
32
33     let mut type_params = type_param_list.type_or_const_params();
34     if type_params.all(|p| match p {
35         ast::TypeOrConstParam::Type(t) => t.type_bound_list().is_none(),
36         ast::TypeOrConstParam::Const(_) => true,
37     }) {
38         return None;
39     }
40
41     let parent = type_param_list.syntax().parent()?;
42
43     let target = type_param_list.syntax().text_range();
44     acc.add(
45         AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite),
46         "Move to where clause",
47         target,
48         |edit| {
49             let type_param_list = edit.make_mut(type_param_list);
50             let parent = edit.make_syntax_mut(parent);
51
52             let where_clause: ast::WhereClause = match_ast! {
53                 match parent {
54                     ast::Fn(it) => it.get_or_create_where_clause(),
55                     ast::Trait(it) => it.get_or_create_where_clause(),
56                     ast::Impl(it) => it.get_or_create_where_clause(),
57                     ast::Enum(it) => it.get_or_create_where_clause(),
58                     ast::Struct(it) => it.get_or_create_where_clause(),
59                     _ => return,
60                 }
61             };
62
63             for toc_param in type_param_list.type_or_const_params() {
64                 let type_param = match toc_param {
65                     ast::TypeOrConstParam::Type(x) => x,
66                     ast::TypeOrConstParam::Const(_) => continue,
67                 };
68                 if let Some(tbl) = type_param.type_bound_list() {
69                     if let Some(predicate) = build_predicate(type_param) {
70                         where_clause.add_predicate(predicate)
71                     }
72                     tbl.remove()
73                 }
74             }
75         },
76     )
77 }
78
79 fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
80     let path = make::ext::ident_path(&param.name()?.syntax().to_string());
81     let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
82     Some(predicate.clone_for_update())
83 }
84
85 #[cfg(test)]
86 mod tests {
87     use super::*;
88
89     use crate::tests::check_assist;
90
91     #[test]
92     fn move_bounds_to_where_clause_fn() {
93         check_assist(
94             move_bounds_to_where_clause,
95             r#"fn foo<T: u32, $0F: FnOnce(T) -> T>() {}"#,
96             r#"fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}"#,
97         );
98     }
99
100     #[test]
101     fn move_bounds_to_where_clause_impl() {
102         check_assist(
103             move_bounds_to_where_clause,
104             r#"impl<U: u32, $0T> A<U, T> {}"#,
105             r#"impl<U, T> A<U, T> where U: u32 {}"#,
106         );
107     }
108
109     #[test]
110     fn move_bounds_to_where_clause_struct() {
111         check_assist(
112             move_bounds_to_where_clause,
113             r#"struct A<$0T: Iterator<Item = u32>> {}"#,
114             r#"struct A<T> where T: Iterator<Item = u32> {}"#,
115         );
116     }
117
118     #[test]
119     fn move_bounds_to_where_clause_tuple_struct() {
120         check_assist(
121             move_bounds_to_where_clause,
122             r#"struct Pair<$0T: u32>(T, T);"#,
123             r#"struct Pair<T>(T, T) where T: u32;"#,
124         );
125     }
126 }