2019-09-04 11:48:45 -05:00
|
|
|
use ra_syntax::{
|
2020-03-18 10:41:24 -05:00
|
|
|
ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner},
|
2020-03-18 14:51:47 -05:00
|
|
|
match_ast,
|
2019-09-04 11:48:45 -05:00
|
|
|
SyntaxKind::*,
|
2020-04-10 10:06:57 -05:00
|
|
|
T,
|
2019-09-04 11:48:45 -05:00
|
|
|
};
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2019-09-04 11:48:45 -05:00
|
|
|
|
2019-10-27 03:26:46 -05:00
|
|
|
// Assist: move_bounds_to_where_clause
|
|
|
|
//
|
|
|
|
// Moves inline type bounds to a where clause.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U {
|
|
|
|
// f(x)
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
|
|
|
|
// f(x)
|
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-07-30 08:36:21 -05:00
|
|
|
let type_param_list = ctx.find_node_at_offset::<ast::GenericParamList>()?;
|
2019-09-04 11:48:45 -05:00
|
|
|
|
|
|
|
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()?;
|
2019-10-11 14:55:45 -05:00
|
|
|
if parent.children_with_tokens().any(|it| it.kind() == WHERE_CLAUSE) {
|
2019-09-04 11:48:45 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:51:47 -05:00
|
|
|
let anchor = match_ast! {
|
|
|
|
match parent {
|
2020-07-30 07:51:08 -05:00
|
|
|
ast::Fn(it) => it.body()?.syntax().clone().into(),
|
2020-07-30 04:42:51 -05:00
|
|
|
ast::TraitDef(it) => it.assoc_item_list()?.syntax().clone().into(),
|
|
|
|
ast::ImplDef(it) => it.assoc_item_list()?.syntax().clone().into(),
|
2020-03-18 14:51:47 -05:00
|
|
|
ast::EnumDef(it) => it.variant_list()?.syntax().clone().into(),
|
2020-07-30 10:50:40 -05:00
|
|
|
ast::Struct(it) => {
|
2020-03-18 14:51:47 -05:00
|
|
|
it.syntax().children_with_tokens()
|
2020-07-30 09:49:13 -05:00
|
|
|
.find(|it| it.kind() == RECORD_FIELD_LIST || it.kind() == T![;])?
|
2020-03-18 14:51:47 -05:00
|
|
|
},
|
|
|
|
_ => return None
|
|
|
|
}
|
2019-09-04 11:48:45 -05:00
|
|
|
};
|
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
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 new_params = type_param_list
|
|
|
|
.type_params()
|
|
|
|
.filter(|it| it.type_bound_list().is_some())
|
|
|
|
.map(|type_param| {
|
|
|
|
let without_bounds = type_param.remove_bounds();
|
|
|
|
(type_param, without_bounds)
|
|
|
|
});
|
2019-09-04 11:48:45 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
let new_type_param_list = type_param_list.replace_descendants(new_params);
|
|
|
|
edit.replace_ast(type_param_list.clone(), new_type_param_list);
|
2019-09-04 11:48:45 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
let where_clause = {
|
|
|
|
let predicates = type_param_list.type_params().filter_map(build_predicate);
|
|
|
|
make::where_clause(predicates)
|
|
|
|
};
|
2019-09-04 11:48:45 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
let to_insert = match anchor.prev_sibling_or_token() {
|
|
|
|
Some(ref elem) if elem.kind() == WHITESPACE => {
|
|
|
|
format!("{} ", where_clause.syntax())
|
|
|
|
}
|
|
|
|
_ => format!(" {}", where_clause.syntax()),
|
|
|
|
};
|
|
|
|
edit.insert(anchor.text_range().start(), to_insert);
|
|
|
|
},
|
|
|
|
)
|
2019-09-04 11:48:45 -05:00
|
|
|
}
|
|
|
|
|
2019-09-25 09:57:12 -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(¶m.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());
|
2019-09-04 11:48:45 -05:00
|
|
|
Some(predicate)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::check_assist;
|
2019-09-04 11:48:45 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_bounds_to_where_clause_fn() {
|
|
|
|
check_assist(
|
|
|
|
move_bounds_to_where_clause,
|
|
|
|
r#"
|
|
|
|
fn foo<T: u32, <|>F: FnOnce(T) -> T>() {}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-05-20 15:55:37 -05:00
|
|
|
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
|
2019-09-04 11:48:45 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_bounds_to_where_clause_impl() {
|
|
|
|
check_assist(
|
|
|
|
move_bounds_to_where_clause,
|
|
|
|
r#"
|
|
|
|
impl<U: u32, <|>T> A<U, T> {}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-05-20 15:55:37 -05:00
|
|
|
impl<U, T> A<U, T> where U: u32 {}
|
2019-09-04 11:48:45 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_bounds_to_where_clause_struct() {
|
|
|
|
check_assist(
|
|
|
|
move_bounds_to_where_clause,
|
|
|
|
r#"
|
|
|
|
struct A<<|>T: Iterator<Item = u32>> {}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-05-20 15:55:37 -05:00
|
|
|
struct A<T> where T: Iterator<Item = u32> {}
|
2019-09-04 11:48:45 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_bounds_to_where_clause_tuple_struct() {
|
|
|
|
check_assist(
|
|
|
|
move_bounds_to_where_clause,
|
|
|
|
r#"
|
|
|
|
struct Pair<<|>T: u32>(T, T);
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-05-20 15:55:37 -05:00
|
|
|
struct Pair<T>(T, T) where T: u32;
|
2019-09-04 11:48:45 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|