2 ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner},
8 use crate::{AssistContext, AssistId, Assists};
10 // Assist: move_bounds_to_where_clause
12 // Moves inline type bounds to a where clause.
15 // fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U {
21 // fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
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>()?;
28 let mut type_params = type_param_list.type_params();
29 if type_params.all(|p| p.type_bound_list().is_none()) {
33 let parent = type_param_list.syntax().parent()?;
34 if parent.children_with_tokens().any(|it| it.kind() == WHERE_CLAUSE) {
38 let anchor = match_ast! {
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![;])?
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
56 .filter(|it| it.type_bound_list().is_some())
58 let without_bounds = type_param.remove_bounds();
59 (type_param, without_bounds)
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);
66 let predicates = type_param_list.type_params().filter_map(build_predicate);
67 make::where_clause(predicates)
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()),
74 edit.insert(anchor.text_range().start(), to_insert);
78 fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
80 let name_ref = make::name_ref(¶m.name()?.syntax().to_string());
81 let segment = make::path_segment(name_ref);
82 make::path_unqualified(segment)
84 let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
92 use crate::tests::check_assist;
95 fn move_bounds_to_where_clause_fn() {
97 move_bounds_to_where_clause,
99 fn foo<T: u32, <|>F: FnOnce(T) -> T>() {}
102 fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
108 fn move_bounds_to_where_clause_impl() {
110 move_bounds_to_where_clause,
112 impl<U: u32, <|>T> A<U, T> {}
115 impl<U, T> A<U, T> where U: u32 {}
121 fn move_bounds_to_where_clause_struct() {
123 move_bounds_to_where_clause,
125 struct A<<|>T: Iterator<Item = u32>> {}
128 struct A<T> where T: Iterator<Item = u32> {}
134 fn move_bounds_to_where_clause_tuple_struct() {
136 move_bounds_to_where_clause,
138 struct Pair<<|>T: u32>(T, T);
141 struct Pair<T>(T, T) where T: u32;