rust/crates/ide_assists/src/handlers/move_bounds.rs

117 lines
3.5 KiB
Rust
Raw Normal View History

2020-08-12 11:26:51 -05:00
use syntax::{
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, NameOwner, TypeBoundsOwner},
2020-03-18 14:51:47 -05:00
match_ast,
};
2020-06-28 17:36:05 -05:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-10-27 03:26:46 -05:00
// Assist: move_bounds_to_where_clause
//
// Moves inline type bounds to a where clause.
//
// ```
2021-01-06 14:15:48 -06:00
// fn apply<T, U, $0F: FnOnce(T) -> U>(f: F, x: T) -> U {
2019-10-27 03:26:46 -05:00
// f(x)
// }
// ```
// ->
// ```
// fn apply<T, U, F>(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::<ast::GenericParamList>()?;
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();
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-02 16:48:35 -05:00
AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite),
2020-06-28 17:36:05 -05:00
"Move to where clause",
target,
|edit| {
let type_param_list = edit.make_ast_mut(type_param_list);
let parent = edit.make_mut(parent);
let where_clause: ast::WhereClause = match_ast! {
match parent {
ast::Fn(it) => it.get_or_create_where_clause(),
2021-03-16 14:28:04 -05:00
ast::Trait(it) => it.get_or_create_where_clause(),
ast::Impl(it) => it.get_or_create_where_clause(),
2021-03-16 14:28:04 -05:00
ast::Enum(it) => it.get_or_create_where_clause(),
ast::Struct(it) => it.get_or_create_where_clause(),
_ => return,
}
2020-06-28 17:36:05 -05:00
};
for type_param in type_param_list.type_params() {
if let Some(tbl) = type_param.type_bound_list() {
2021-03-19 13:00:20 -05:00
if let Some(predicate) = build_predicate(type_param) {
where_clause.add_predicate(predicate)
}
tbl.remove()
2020-06-28 17:36:05 -05:00
}
}
2020-06-28 17:36:05 -05:00
},
)
}
fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
2020-02-29 04:55:36 -06:00
let path = {
let name_ref = make::name_ref(&param.name()?.syntax().to_string());
let segment = make::path_segment(name_ref);
2020-02-29 06:50:47 -06:00
make::path_unqualified(segment)
2020-02-29 04:55:36 -06:00
};
2019-09-26 04:18:26 -05:00
let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
2021-03-19 13:00:20 -05:00
Some(predicate.clone_for_update())
}
#[cfg(test)]
mod tests {
use super::*;
2020-05-06 03:16:55 -05:00
use crate::tests::check_assist;
#[test]
fn move_bounds_to_where_clause_fn() {
check_assist(
move_bounds_to_where_clause,
2021-03-16 14:28:04 -05:00
r#"fn foo<T: u32, $0F: FnOnce(T) -> T>() {}"#,
r#"fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}"#,
);
}
#[test]
fn move_bounds_to_where_clause_impl() {
check_assist(
move_bounds_to_where_clause,
2021-03-16 14:28:04 -05:00
r#"impl<U: u32, $0T> A<U, T> {}"#,
r#"impl<U, T> A<U, T> where U: u32 {}"#,
);
}
#[test]
fn move_bounds_to_where_clause_struct() {
check_assist(
move_bounds_to_where_clause,
2021-03-16 14:28:04 -05:00
r#"struct A<$0T: Iterator<Item = u32>> {}"#,
r#"struct A<T> where T: Iterator<Item = u32> {}"#,
);
}
#[test]
fn move_bounds_to_where_clause_tuple_struct() {
check_assist(
move_bounds_to_where_clause,
2021-03-16 14:28:04 -05:00
r#"struct Pair<$0T: u32>(T, T);"#,
r#"struct Pair<T>(T, T) where T: u32;"#,
);
}
}