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.rs55
1 files changed, 31 insertions, 24 deletions
diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs
index 89956aea9..44e50cb6e 100644
--- a/crates/ra_assists/src/handlers/move_bounds.rs
+++ b/crates/ra_assists/src/handlers/move_bounds.rs
@@ -49,30 +49,37 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> {
49 } 49 }
50 }; 50 };
51 51
52 ctx.add_assist(AssistId("move_bounds_to_where_clause"), "Move to where clause", |edit| { 52 let target = type_param_list.syntax().text_range();
53 let new_params = type_param_list 53 ctx.add_assist(
54 .type_params() 54 AssistId("move_bounds_to_where_clause"),
55 .filter(|it| it.type_bound_list().is_some()) 55 "Move to where clause",
56 .map(|type_param| { 56 target,
57 let without_bounds = type_param.remove_bounds(); 57 |edit| {
58 (type_param, without_bounds) 58 let new_params = type_param_list
59 }); 59 .type_params()
60 60 .filter(|it| it.type_bound_list().is_some())
61 let new_type_param_list = type_param_list.replace_descendants(new_params); 61 .map(|type_param| {
62 edit.replace_ast(type_param_list.clone(), new_type_param_list); 62 let without_bounds = type_param.remove_bounds();
63 63 (type_param, without_bounds)
64 let where_clause = { 64 });
65 let predicates = type_param_list.type_params().filter_map(build_predicate); 65
66 make::where_clause(predicates) 66 let new_type_param_list = type_param_list.replace_descendants(new_params);
67 }; 67 edit.replace_ast(type_param_list.clone(), new_type_param_list);
68 68
69 let to_insert = match anchor.prev_sibling_or_token() { 69 let where_clause = {
70 Some(ref elem) if elem.kind() == WHITESPACE => format!("{} ", where_clause.syntax()), 70 let predicates = type_param_list.type_params().filter_map(build_predicate);
71 _ => format!(" {}", where_clause.syntax()), 71 make::where_clause(predicates)
72 }; 72 };
73 edit.insert(anchor.text_range().start(), to_insert); 73
74 edit.target(type_param_list.syntax().text_range()); 74 let to_insert = match anchor.prev_sibling_or_token() {
75 }) 75 Some(ref elem) if elem.kind() == WHITESPACE => {
76 format!("{} ", where_clause.syntax())
77 }
78 _ => format!(" {}", where_clause.syntax()),
79 };
80 edit.insert(anchor.text_range().start(), to_insert);
81 },
82 )
76} 83}
77 84
78fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { 85fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {