From 25e6bbde01d4a9cd08fa79db5b8b7da6bbf1a623 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 10:16:55 +0200 Subject: Merge assits::test_helpers and tests --- crates/ra_assists/src/handlers/move_bounds.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/handlers/move_bounds.rs') diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs index 0f26884dc..89956aea9 100644 --- a/crates/ra_assists/src/handlers/move_bounds.rs +++ b/crates/ra_assists/src/handlers/move_bounds.rs @@ -89,7 +89,7 @@ fn build_predicate(param: ast::TypeParam) -> Option { mod tests { use super::*; - use crate::helpers::check_assist; + use crate::tests::check_assist; #[test] fn move_bounds_to_where_clause_fn() { -- cgit v1.2.3 From 233f01c9ba555e5d06f336cb0ff64e7a83e4a23a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 12:51:28 +0200 Subject: Move target to AssistLabel Target is used for assists sorting, so we need it before we compute the action. --- crates/ra_assists/src/handlers/move_bounds.rs | 55 +++++++++++++++------------ 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_bounds.rs') 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 { } }; - ctx.add_assist(AssistId("move_bounds_to_where_clause"), "Move to where clause", |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) - }); - - let new_type_param_list = type_param_list.replace_descendants(new_params); - edit.replace_ast(type_param_list.clone(), new_type_param_list); - - let where_clause = { - let predicates = type_param_list.type_params().filter_map(build_predicate); - make::where_clause(predicates) - }; - - 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()); - }) + let target = type_param_list.syntax().text_range(); + ctx.add_assist( + AssistId("move_bounds_to_where_clause"), + "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) + }); + + let new_type_param_list = type_param_list.replace_descendants(new_params); + edit.replace_ast(type_param_list.clone(), new_type_param_list); + + let where_clause = { + let predicates = type_param_list.type_params().filter_map(build_predicate); + make::where_clause(predicates) + }; + + 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); + }, + ) } fn build_predicate(param: ast::TypeParam) -> Option { -- cgit v1.2.3 From 4867968d22899395e6551f22641b3617e995140c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 18:45:35 +0200 Subject: Refactor assists API to be more convenient for adding new assists It now duplicates completion API in its shape. --- crates/ra_assists/src/handlers/move_bounds.rs | 57 ++++++++++++--------------- 1 file changed, 25 insertions(+), 32 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_bounds.rs') 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::{ T, }; -use crate::{Assist, AssistCtx, AssistId}; +use crate::{AssistContext, AssistId, Assists}; // Assist: move_bounds_to_where_clause // @@ -22,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; // f(x) // } // ``` -pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option { +pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let type_param_list = ctx.find_node_at_offset::()?; let mut type_params = type_param_list.type_params(); @@ -50,36 +50,29 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option { }; let target = type_param_list.syntax().text_range(); - ctx.add_assist( - AssistId("move_bounds_to_where_clause"), - "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) - }); - - let new_type_param_list = type_param_list.replace_descendants(new_params); - edit.replace_ast(type_param_list.clone(), new_type_param_list); - - let where_clause = { - let predicates = type_param_list.type_params().filter_map(build_predicate); - make::where_clause(predicates) - }; - - 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); - }, - ) + acc.add(AssistId("move_bounds_to_where_clause"), "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) + }); + + let new_type_param_list = type_param_list.replace_descendants(new_params); + edit.replace_ast(type_param_list.clone(), new_type_param_list); + + let where_clause = { + let predicates = type_param_list.type_params().filter_map(build_predicate); + make::where_clause(predicates) + }; + + 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); + }) } fn build_predicate(param: ast::TypeParam) -> Option { -- cgit v1.2.3 From 65fa5864105280267e6ccdaa61957cd9953e444e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 22:55:37 +0200 Subject: Relax cursor position tests in assists Those will be replaced with snippets anyway --- crates/ra_assists/src/handlers/move_bounds.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_bounds.rs') diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs index a41aacfc3..be2a7eddc 100644 --- a/crates/ra_assists/src/handlers/move_bounds.rs +++ b/crates/ra_assists/src/handlers/move_bounds.rs @@ -99,7 +99,7 @@ mod tests { fn fooF: FnOnce(T) -> T>() {} "#, r#" - fn fooF>() where T: u32, F: FnOnce(T) -> T {} + fn foo() where T: u32, F: FnOnce(T) -> T {} "#, ); } @@ -112,7 +112,7 @@ mod tests { implT> A {} "#, r#" - implT> A where U: u32 {} + impl A where U: u32 {} "#, ); } @@ -125,7 +125,7 @@ mod tests { struct A<<|>T: Iterator> {} "#, r#" - struct A<<|>T> where T: Iterator {} + struct A where T: Iterator {} "#, ); } @@ -138,7 +138,7 @@ mod tests { struct Pair<<|>T: u32>(T, T); "#, r#" - struct Pair<<|>T>(T, T) where T: u32; + struct Pair(T, T) where T: u32; "#, ); } -- cgit v1.2.3