]> git.lizzy.rs Git - rust.git/blob - crates/ra_assists/src/handlers/move_bounds.rs
Centralize fixture parsing for assists
[rust.git] / crates / ra_assists / src / handlers / move_bounds.rs
1 use ra_syntax::{
2     ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner},
3     match_ast,
4     SyntaxKind::*,
5     T,
6 };
7
8 use crate::{AssistContext, AssistId, Assists};
9
10 // Assist: move_bounds_to_where_clause
11 //
12 // Moves inline type bounds to a where clause.
13 //
14 // ```
15 // fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U {
16 //     f(x)
17 // }
18 // ```
19 // ->
20 // ```
21 // fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
22 //     f(x)
23 // }
24 // ```
25 pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
26     let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?;
27
28     let mut type_params = type_param_list.type_params();
29     if type_params.all(|p| p.type_bound_list().is_none()) {
30         return None;
31     }
32
33     let parent = type_param_list.syntax().parent()?;
34     if parent.children_with_tokens().any(|it| it.kind() == WHERE_CLAUSE) {
35         return None;
36     }
37
38     let anchor = match_ast! {
39         match parent {
40             ast::FnDef(it) => it.body()?.syntax().clone().into(),
41             ast::TraitDef(it) => it.item_list()?.syntax().clone().into(),
42             ast::ImplDef(it) => it.item_list()?.syntax().clone().into(),
43             ast::EnumDef(it) => it.variant_list()?.syntax().clone().into(),
44             ast::StructDef(it) => {
45                 it.syntax().children_with_tokens()
46                     .find(|it| it.kind() == RECORD_FIELD_DEF_LIST || it.kind() == T![;])?
47             },
48             _ => return None
49         }
50     };
51
52     let target = type_param_list.syntax().text_range();
53     acc.add(AssistId("move_bounds_to_where_clause"), "Move to where clause", target, |edit| {
54         let new_params = type_param_list
55             .type_params()
56             .filter(|it| it.type_bound_list().is_some())
57             .map(|type_param| {
58                 let without_bounds = type_param.remove_bounds();
59                 (type_param, without_bounds)
60             });
61
62         let new_type_param_list = type_param_list.replace_descendants(new_params);
63         edit.replace_ast(type_param_list.clone(), new_type_param_list);
64
65         let where_clause = {
66             let predicates = type_param_list.type_params().filter_map(build_predicate);
67             make::where_clause(predicates)
68         };
69
70         let to_insert = match anchor.prev_sibling_or_token() {
71             Some(ref elem) if elem.kind() == WHITESPACE => format!("{} ", where_clause.syntax()),
72             _ => format!(" {}", where_clause.syntax()),
73         };
74         edit.insert(anchor.text_range().start(), to_insert);
75     })
76 }
77
78 fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
79     let path = {
80         let name_ref = make::name_ref(&param.name()?.syntax().to_string());
81         let segment = make::path_segment(name_ref);
82         make::path_unqualified(segment)
83     };
84     let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
85     Some(predicate)
86 }
87
88 #[cfg(test)]
89 mod tests {
90     use super::*;
91
92     use crate::tests::check_assist;
93
94     #[test]
95     fn move_bounds_to_where_clause_fn() {
96         check_assist(
97             move_bounds_to_where_clause,
98             r#"
99             fn foo<T: u32, <|>F: FnOnce(T) -> T>() {}
100             "#,
101             r#"
102             fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
103             "#,
104         );
105     }
106
107     #[test]
108     fn move_bounds_to_where_clause_impl() {
109         check_assist(
110             move_bounds_to_where_clause,
111             r#"
112             impl<U: u32, <|>T> A<U, T> {}
113             "#,
114             r#"
115             impl<U, T> A<U, T> where U: u32 {}
116             "#,
117         );
118     }
119
120     #[test]
121     fn move_bounds_to_where_clause_struct() {
122         check_assist(
123             move_bounds_to_where_clause,
124             r#"
125             struct A<<|>T: Iterator<Item = u32>> {}
126             "#,
127             r#"
128             struct A<T> where T: Iterator<Item = u32> {}
129             "#,
130         );
131     }
132
133     #[test]
134     fn move_bounds_to_where_clause_tuple_struct() {
135         check_assist(
136             move_bounds_to_where_clause,
137             r#"
138             struct Pair<<|>T: u32>(T, T);
139             "#,
140             r#"
141             struct Pair<T>(T, T) where T: u32;
142             "#,
143         );
144     }
145 }