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