aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/move_bounds.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/move_bounds.rs')
-rw-r--r--crates/ra_assists/src/handlers/move_bounds.rs57
1 files changed, 25 insertions, 32 deletions
diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs
index 44e50cb6e..a41aacfc3 100644
--- a/crates/ra_assists/src/handlers/move_bounds.rs
+++ b/crates/ra_assists/src/handlers/move_bounds.rs
@@ -5,7 +5,7 @@ use ra_syntax::{
5 T, 5 T,
6}; 6};
7 7
8use crate::{Assist, AssistCtx, AssistId}; 8use crate::{AssistContext, AssistId, Assists};
9 9
10// Assist: move_bounds_to_where_clause 10// Assist: move_bounds_to_where_clause
11// 11//
@@ -22,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId};
22// f(x) 22// f(x)
23// } 23// }
24// ``` 24// ```
25pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> { 25pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
26 let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?; 26 let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?;
27 27
28 let mut type_params = type_param_list.type_params(); 28 let mut type_params = type_param_list.type_params();
@@ -50,36 +50,29 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> {
50 }; 50 };
51 51
52 let target = type_param_list.syntax().text_range(); 52 let target = type_param_list.syntax().text_range();
53 ctx.add_assist( 53 acc.add(AssistId("move_bounds_to_where_clause"), "Move to where clause", target, |edit| {
54 AssistId("move_bounds_to_where_clause"), 54 let new_params = type_param_list
55 "Move to where clause", 55 .type_params()
56 target, 56 .filter(|it| it.type_bound_list().is_some())
57 |edit| { 57 .map(|type_param| {
58 let new_params = type_param_list 58 let without_bounds = type_param.remove_bounds();
59 .type_params() 59 (type_param, without_bounds)
60 .filter(|it| it.type_bound_list().is_some()) 60 });
61 .map(|type_param| { 61
62 let without_bounds = type_param.remove_bounds(); 62 let new_type_param_list = type_param_list.replace_descendants(new_params);
63 (type_param, without_bounds) 63 edit.replace_ast(type_param_list.clone(), new_type_param_list);
64 }); 64
65 65 let where_clause = {
66 let new_type_param_list = type_param_list.replace_descendants(new_params); 66 let predicates = type_param_list.type_params().filter_map(build_predicate);
67 edit.replace_ast(type_param_list.clone(), new_type_param_list); 67 make::where_clause(predicates)
68 68 };
69 let where_clause = { 69
70 let predicates = type_param_list.type_params().filter_map(build_predicate); 70 let to_insert = match anchor.prev_sibling_or_token() {
71 make::where_clause(predicates) 71 Some(ref elem) if elem.kind() == WHITESPACE => format!("{} ", where_clause.syntax()),
72 }; 72 _ => format!(" {}", where_clause.syntax()),
73 73 };
74 let to_insert = match anchor.prev_sibling_or_token() { 74 edit.insert(anchor.text_range().start(), to_insert);
75 Some(ref elem) if elem.kind() == WHITESPACE => { 75 })
76 format!("{} ", where_clause.syntax())
77 }
78 _ => format!(" {}", where_clause.syntax()),
79 };
80 edit.insert(anchor.text_range().start(), to_insert);
81 },
82 )
83} 76}
84 77
85fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { 78fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {