use syntax::{ ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasName, HasTypeBounds}, match_ast, }; use crate::{AssistContext, AssistId, AssistKind, Assists}; // Assist: move_bounds_to_where_clause // // Moves inline type bounds to a where clause. // // ``` // fn apply U>(f: F, x: T) -> U { // f(x) // } // ``` // -> // ``` // fn apply(f: F, x: T) -> U where F: FnOnce(T) -> U { // f(x) // } // ``` pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let type_param_list = ctx.find_node_at_offset::()?; let mut type_params = type_param_list.type_params(); if type_params.all(|p| p.type_bound_list().is_none()) { return None; } let parent = type_param_list.syntax().parent()?; let target = type_param_list.syntax().text_range(); acc.add( AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite), "Move to where clause", target, |edit| { let type_param_list = edit.make_mut(type_param_list); let parent = edit.make_syntax_mut(parent); let where_clause: ast::WhereClause = match_ast! { match parent { ast::Fn(it) => it.get_or_create_where_clause(), ast::Trait(it) => it.get_or_create_where_clause(), ast::Impl(it) => it.get_or_create_where_clause(), ast::Enum(it) => it.get_or_create_where_clause(), ast::Struct(it) => it.get_or_create_where_clause(), _ => return, } }; for type_param in type_param_list.type_params() { if let Some(tbl) = type_param.type_bound_list() { if let Some(predicate) = build_predicate(type_param) { where_clause.add_predicate(predicate) } tbl.remove() } } }, ) } fn build_predicate(param: ast::TypeParam) -> Option { let path = make::ext::ident_path(¶m.name()?.syntax().to_string()); let predicate = make::where_pred(path, param.type_bound_list()?.bounds()); Some(predicate.clone_for_update()) } #[cfg(test)] mod tests { use super::*; use crate::tests::check_assist; #[test] fn move_bounds_to_where_clause_fn() { check_assist( move_bounds_to_where_clause, r#"fn foo T>() {}"#, r#"fn foo() where T: u32, F: FnOnce(T) -> T {}"#, ); } #[test] fn move_bounds_to_where_clause_impl() { check_assist( move_bounds_to_where_clause, r#"impl A {}"#, r#"impl A where U: u32 {}"#, ); } #[test] fn move_bounds_to_where_clause_struct() { check_assist( move_bounds_to_where_clause, r#"struct A<$0T: Iterator> {}"#, r#"struct A where T: Iterator {}"#, ); } #[test] fn move_bounds_to_where_clause_tuple_struct() { check_assist( move_bounds_to_where_clause, r#"struct Pair<$0T: u32>(T, T);"#, r#"struct Pair(T, T) where T: u32;"#, ); } }