rust/crates/ide-assists/src/handlers/move_bounds.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

123 lines
3.7 KiB
Rust
Raw Normal View History

2020-08-12 11:26:51 -05:00
use syntax::{
ast::{self, edit_in_place::{GenericParamsOwnerEdit, Removable}, make, AstNode, HasName, HasTypeBounds},
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)
// }
// ```
2022-07-20 08:06:15 -05:00
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>()?;
2021-12-29 07:35:59 -06:00
let mut type_params = type_param_list.type_or_const_params();
if type_params.all(|p| match p {
ast::TypeOrConstParam::Type(t) => t.type_bound_list().is_none(),
ast::TypeOrConstParam::Const(_) => true,
}) {
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| {
2021-05-16 06:18:49 -05:00
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(),
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
};
2021-12-29 07:35:59 -06:00
for toc_param in type_param_list.type_or_const_params() {
let type_param = match toc_param {
ast::TypeOrConstParam::Type(x) => x,
ast::TypeOrConstParam::Const(_) => continue,
};
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> {
let path = make::ext::ident_path(&param.name()?.syntax().to_string());
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;"#,
);
}
}