aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/move_bounds.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/move_bounds.rs')
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs39
1 files changed, 19 insertions, 20 deletions
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs
index 6fd2fb72b..671826013 100644
--- a/crates/ra_assists/src/assists/move_bounds.rs
+++ b/crates/ra_assists/src/assists/move_bounds.rs
@@ -3,10 +3,9 @@ use ra_syntax::{
3 ast::{self, AstNode, NameOwner, TypeBoundsOwner}, 3 ast::{self, AstNode, NameOwner, TypeBoundsOwner},
4 SyntaxElement, 4 SyntaxElement,
5 SyntaxKind::*, 5 SyntaxKind::*,
6 TextRange,
7}; 6};
8 7
9use crate::{ast_builder::Make, Assist, AssistCtx, AssistId}; 8use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
10 9
11pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; 11 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
@@ -36,23 +35,23 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
36 AssistId("move_bounds_to_where_clause"), 35 AssistId("move_bounds_to_where_clause"),
37 "move_bounds_to_where_clause", 36 "move_bounds_to_where_clause",
38 |edit| { 37 |edit| {
39 let type_params = type_param_list.type_params().collect::<Vec<_>>(); 38 let new_params = type_param_list
40 39 .type_params()
41 for param in &type_params { 40 .filter(|it| it.type_bound_list().is_some())
42 if let Some(bounds) = param.type_bound_list() { 41 .map(|type_param| {
43 let colon = param 42 let without_bounds =
44 .syntax() 43 AstEditor::new(type_param.clone()).remove_bounds().ast().clone();
45 .children_with_tokens() 44 (type_param, without_bounds)
46 .find(|it| it.kind() == COLON) 45 });
47 .unwrap(); 46
48 let start = colon.text_range().start(); 47 let mut ast_editor = AstEditor::new(type_param_list.clone());
49 let end = bounds.syntax().text_range().end(); 48 ast_editor.replace_descendants(new_params);
50 edit.delete(TextRange::from_to(start, end)); 49 ast_editor.into_text_edit(edit.text_edit_builder());
51 } 50
52 } 51 let where_clause = {
53 52 let predicates = type_param_list.type_params().filter_map(build_predicate);
54 let predicates = type_params.iter().filter_map(build_predicate); 53 Make::<ast::WhereClause>::from_predicates(predicates)
55 let where_clause = Make::<ast::WhereClause>::from_predicates(predicates); 54 };
56 55
57 let to_insert = match anchor.prev_sibling_or_token() { 56 let to_insert = match anchor.prev_sibling_or_token() {
58 Some(ref elem) if elem.kind() == WHITESPACE => { 57 Some(ref elem) if elem.kind() == WHITESPACE => {
@@ -68,7 +67,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
68 ctx.build() 67 ctx.build()
69} 68}
70 69
71fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> { 70fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
72 let path = Make::<ast::Path>::from_name(param.name()?); 71 let path = Make::<ast::Path>::from_name(param.name()?);
73 let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds()); 72 let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds());
74 Some(predicate) 73 Some(predicate)