2 ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasName, HasTypeBounds},
6 use crate::{AssistContext, AssistId, AssistKind, Assists};
8 // Assist: move_bounds_to_where_clause
10 // Moves inline type bounds to a where clause.
13 // fn apply<T, U, $0F: FnOnce(T) -> U>(f: F, x: T) -> U {
19 // fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
23 pub(crate) fn move_bounds_to_where_clause(
25 ctx: &AssistContext<'_>,
27 let type_param_list = ctx.find_node_at_offset::<ast::GenericParamList>()?;
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,
37 let parent = type_param_list.syntax().parent()?;
39 let target = type_param_list.syntax().text_range();
41 AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite),
42 "Move to where clause",
45 let type_param_list = edit.make_mut(type_param_list);
46 let parent = edit.make_syntax_mut(parent);
48 let where_clause: ast::WhereClause = match_ast! {
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(),
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,
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)
75 fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
76 let path = make::ext::ident_path(¶m.name()?.syntax().to_string());
77 let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
78 Some(predicate.clone_for_update())
85 use crate::tests::check_assist;
88 fn move_bounds_to_where_clause_fn() {
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 {}"#,
97 fn move_bounds_to_where_clause_impl() {
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 {}"#,
106 fn move_bounds_to_where_clause_struct() {
108 move_bounds_to_where_clause,
109 r#"struct A<$0T: Iterator<Item = u32>> {}"#,
110 r#"struct A<T> where T: Iterator<Item = u32> {}"#,
115 fn move_bounds_to_where_clause_tuple_struct() {
117 move_bounds_to_where_clause,
118 r#"struct Pair<$0T: u32>(T, T);"#,
119 r#"struct Pair<T>(T, T) where T: u32;"#,