2019-09-04 11:48:45 -05:00
|
|
|
use hir::db::HirDatabase;
|
|
|
|
use ra_syntax::{
|
2019-09-26 04:18:26 -05:00
|
|
|
ast::{self, make, AstNode, NameOwner, TypeBoundsOwner},
|
2019-09-04 11:48:45 -05:00
|
|
|
SyntaxElement,
|
|
|
|
SyntaxKind::*,
|
|
|
|
};
|
|
|
|
|
2019-09-26 04:18:26 -05:00
|
|
|
use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
2019-09-04 11:48:45 -05:00
|
|
|
|
|
|
|
pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
|
|
|
let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
|
|
|
|
|
|
|
|
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()?;
|
|
|
|
if parent.children_with_tokens().find(|it| it.kind() == WHERE_CLAUSE).is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let anchor: SyntaxElement = match parent.kind() {
|
|
|
|
FN_DEF => ast::FnDef::cast(parent)?.body()?.syntax().clone().into(),
|
|
|
|
TRAIT_DEF => ast::TraitDef::cast(parent)?.item_list()?.syntax().clone().into(),
|
|
|
|
IMPL_BLOCK => ast::ImplBlock::cast(parent)?.item_list()?.syntax().clone().into(),
|
|
|
|
ENUM_DEF => ast::EnumDef::cast(parent)?.variant_list()?.syntax().clone().into(),
|
|
|
|
STRUCT_DEF => parent
|
|
|
|
.children_with_tokens()
|
|
|
|
.find(|it| it.kind() == RECORD_FIELD_DEF_LIST || it.kind() == SEMI)?,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.add_action(
|
|
|
|
AssistId("move_bounds_to_where_clause"),
|
|
|
|
"move_bounds_to_where_clause",
|
|
|
|
|edit| {
|
2019-09-25 09:57:12 -05:00
|
|
|
let new_params = type_param_list
|
|
|
|
.type_params()
|
|
|
|
.filter(|it| it.type_bound_list().is_some())
|
|
|
|
.map(|type_param| {
|
2019-09-30 01:56:20 -05:00
|
|
|
let without_bounds = type_param.remove_bounds();
|
2019-09-25 09:57:12 -05:00
|
|
|
(type_param, without_bounds)
|
|
|
|
});
|
2019-09-04 11:48:45 -05:00
|
|
|
|
2019-09-25 09:57:12 -05:00
|
|
|
let mut ast_editor = AstEditor::new(type_param_list.clone());
|
|
|
|
ast_editor.replace_descendants(new_params);
|
|
|
|
ast_editor.into_text_edit(edit.text_edit_builder());
|
2019-09-04 11:48:45 -05:00
|
|
|
|
2019-09-25 09:57:12 -05:00
|
|
|
let where_clause = {
|
|
|
|
let predicates = type_param_list.type_params().filter_map(build_predicate);
|
2019-09-26 04:18:26 -05:00
|
|
|
make::where_clause(predicates)
|
2019-09-25 09:57:12 -05:00
|
|
|
};
|
2019-09-04 11:48:45 -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);
|
|
|
|
edit.target(type_param_list.syntax().text_range());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.build()
|
|
|
|
}
|
|
|
|
|
2019-09-25 09:57:12 -05:00
|
|
|
fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
|
2019-09-26 04:18:26 -05:00
|
|
|
let path = make::path_from_name_ref(make::name_ref(¶m.name()?.syntax().to_string()));
|
|
|
|
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::*;
|
|
|
|
|
|
|
|
use crate::helpers::check_assist;
|
|
|
|
|
|
|
|
#[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#"
|
|
|
|
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,
|
|
|
|
r#"
|
|
|
|
impl<U: u32, <|>T> 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,
|
|
|
|
r#"
|
|
|
|
struct A<<|>T: 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,
|
|
|
|
r#"
|
|
|
|
struct Pair<<|>T: u32>(T, T);
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Pair<<|>T>(T, T) where T: u32;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|