4 edit_in_place::{GenericParamsOwnerEdit, Removable},
5 make, AstNode, HasName, HasTypeBounds,
10 use crate::{AssistContext, AssistId, AssistKind, Assists};
12 // Assist: move_bounds_to_where_clause
14 // Moves inline type bounds to a where clause.
17 // fn apply<T, U, $0F: FnOnce(T) -> U>(f: F, x: T) -> U {
23 // fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
27 pub(crate) fn move_bounds_to_where_clause(
29 ctx: &AssistContext<'_>,
31 let type_param_list = ctx.find_node_at_offset::<ast::GenericParamList>()?;
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,
41 let parent = type_param_list.syntax().parent()?;
43 let target = type_param_list.syntax().text_range();
45 AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite),
46 "Move to where clause",
49 let type_param_list = edit.make_mut(type_param_list);
50 let parent = edit.make_syntax_mut(parent);
52 let where_clause: ast::WhereClause = match_ast! {
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(),
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,
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)
79 fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
80 let path = make::ext::ident_path(¶m.name()?.syntax().to_string());
81 let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
82 Some(predicate.clone_for_update())
89 use crate::tests::check_assist;
92 fn move_bounds_to_where_clause_fn() {
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 {}"#,
101 fn move_bounds_to_where_clause_impl() {
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 {}"#,
110 fn move_bounds_to_where_clause_struct() {
112 move_bounds_to_where_clause,
113 r#"struct A<$0T: Iterator<Item = u32>> {}"#,
114 r#"struct A<T> where T: Iterator<Item = u32> {}"#,
119 fn move_bounds_to_where_clause_tuple_struct() {
121 move_bounds_to_where_clause,
122 r#"struct Pair<$0T: u32>(T, T);"#,
123 r#"struct Pair<T>(T, T) where T: u32;"#,