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/replace_unwrap_with_match.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index 62d4ea522..dcb471edb 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -63,7 +63,7 @@ pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_target}; + use crate::tests::{check_assist, check_assist_target}; #[test] fn test_replace_result_unwrap_with_match() { -- 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. --- .../src/handlers/replace_unwrap_with_match.rs | 38 +++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index dcb471edb..c6b73da67 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -38,26 +38,32 @@ pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option { let caller = method_call.expr()?; let ty = ctx.sema.type_of_expr(&caller)?; let happy_variant = TryEnum::from_ty(ctx.sema, &ty)?.happy_case(); + let target = method_call.syntax().text_range(); + ctx.add_assist( + AssistId("replace_unwrap_with_match"), + "Replace unwrap with match", + target, + |edit| { + let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); + let it = make::bind_pat(make::name("a")).into(); + let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); - ctx.add_assist(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", |edit| { - let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); - let it = make::bind_pat(make::name("a")).into(); - let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); + let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); + let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); - let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); - let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); + let unreachable_call = make::unreachable_macro_call().into(); + let err_arm = + make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); - let unreachable_call = make::unreachable_macro_call().into(); - let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); + let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); + let match_expr = make::expr_match(caller.clone(), match_arm_list); + let match_expr = + IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); - let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); - let match_expr = make::expr_match(caller.clone(), match_arm_list); - let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); - - edit.target(method_call.syntax().text_range()); - edit.set_cursor(caller.syntax().text_range().start()); - edit.replace_ast::(method_call.into(), match_expr); - }) + edit.set_cursor(caller.syntax().text_range().start()); + edit.replace_ast::(method_call.into(), match_expr); + }, + ) } #[cfg(test)] -- 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. --- .../src/handlers/replace_unwrap_with_match.rs | 41 +++++++++------------- 1 file changed, 17 insertions(+), 24 deletions(-) (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index c6b73da67..a46998b8e 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -5,7 +5,7 @@ use ra_syntax::{ AstNode, }; -use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; +use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; // Assist: replace_unwrap_with_match // @@ -29,7 +29,7 @@ use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; // }; // } // ``` -pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option { +pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let method_call: ast::MethodCallExpr = ctx.find_node_at_offset()?; let name = method_call.name_ref()?; if name.text() != "unwrap" { @@ -37,33 +37,26 @@ pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option { } let caller = method_call.expr()?; let ty = ctx.sema.type_of_expr(&caller)?; - let happy_variant = TryEnum::from_ty(ctx.sema, &ty)?.happy_case(); + let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); let target = method_call.syntax().text_range(); - ctx.add_assist( - AssistId("replace_unwrap_with_match"), - "Replace unwrap with match", - target, - |edit| { - let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); - let it = make::bind_pat(make::name("a")).into(); - let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); + acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| { + let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); + let it = make::bind_pat(make::name("a")).into(); + let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); - let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); - let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); + let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); + let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); - let unreachable_call = make::unreachable_macro_call().into(); - let err_arm = - make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); + let unreachable_call = make::unreachable_macro_call().into(); + let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); - let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); - let match_expr = make::expr_match(caller.clone(), match_arm_list); - let match_expr = - IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); + let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); + let match_expr = make::expr_match(caller.clone(), match_arm_list); + let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); - edit.set_cursor(caller.syntax().text_range().start()); - edit.replace_ast::(method_call.into(), match_expr); - }, - ) + edit.set_cursor(caller.syntax().text_range().start()); + edit.replace_ast::(method_call.into(), match_expr); + }) } #[cfg(test)] -- cgit v1.2.3 From 231fddab5420ffe5edf7b93609ea21155653254a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 14:40:11 +0200 Subject: More fluent indent API --- crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index a46998b8e..c4b56f6e9 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -1,7 +1,11 @@ use std::iter; use ra_syntax::{ - ast::{self, edit::IndentLevel, make}, + ast::{ + self, + edit::{AstNodeEdit, IndentLevel}, + make, + }, AstNode, }; @@ -51,8 +55,8 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); - let match_expr = make::expr_match(caller.clone(), match_arm_list); - let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); + let match_expr = make::expr_match(caller.clone(), match_arm_list) + .indent(IndentLevel::from_node(method_call.syntax())); edit.set_cursor(caller.syntax().text_range().start()); edit.replace_ast::(method_call.into(), match_expr); -- cgit v1.2.3 From 4de2749db8281c00aba37270fa9ae8d4bd2572d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 01:28:11 +0200 Subject: Explain the purpose of `ast::make` module more clearly --- crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index c4b56f6e9..b379b55a8 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -51,7 +51,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); - let unreachable_call = make::unreachable_macro_call().into(); + let unreachable_call = make::expr_unreachable(); let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); -- cgit v1.2.3 From 4ac0abd2960acf1b3a357c681e64b3cddba6fc8e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 May 2020 00:01:08 +0200 Subject: Snippetify unwrap -> match --- .../src/handlers/replace_unwrap_with_match.rs | 41 +++++++++++++++------- 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index b379b55a8..cff7dfb81 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -9,7 +9,10 @@ use ra_syntax::{ AstNode, }; -use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; +use crate::{ + utils::{render_snippet, Cursor, TryEnum}, + AssistContext, AssistId, Assists, +}; // Assist: replace_unwrap_with_match // @@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; // let x: Result = Result::Ok(92); // let y = match x { // Ok(a) => a, -// _ => unreachable!(), +// $0_ => unreachable!(), // }; // } // ``` @@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) let ty = ctx.sema.type_of_expr(&caller)?; let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); let target = method_call.syntax().text_range(); - acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| { + acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |builder| { let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); let it = make::bind_pat(make::name("a")).into(); let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); @@ -58,16 +61,30 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) let match_expr = make::expr_match(caller.clone(), match_arm_list) .indent(IndentLevel::from_node(method_call.syntax())); - edit.set_cursor(caller.syntax().text_range().start()); - edit.replace_ast::(method_call.into(), match_expr); + let range = method_call.syntax().text_range(); + match ctx.config.snippet_cap { + Some(cap) => { + let err_arm = match_expr + .syntax() + .descendants() + .filter_map(ast::MatchArm::cast) + .last() + .unwrap(); + let snippet = + render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax())); + builder.replace_snippet(cap, range, snippet) + } + None => builder.replace(range, match_expr.to_string()), + } }) } #[cfg(test)] mod tests { - use super::*; use crate::tests::{check_assist, check_assist_target}; + use super::*; + #[test] fn test_replace_result_unwrap_with_match() { check_assist( @@ -85,9 +102,9 @@ enum Result { Ok(T), Err(E) } fn i(a: T) -> T { a } fn main() { let x: Result = Result::Ok(92); - let y = <|>match i(x) { + let y = match i(x) { Ok(a) => a, - _ => unreachable!(), + $0_ => unreachable!(), }; } ", @@ -111,9 +128,9 @@ enum Option { Some(T), None } fn i(a: T) -> T { a } fn main() { let x = Option::Some(92); - let y = <|>match i(x) { + let y = match i(x) { Some(a) => a, - _ => unreachable!(), + $0_ => unreachable!(), }; } ", @@ -137,9 +154,9 @@ enum Result { Ok(T), Err(E) } fn i(a: T) -> T { a } fn main() { let x: Result = Result::Ok(92); - let y = <|>match i(x) { + let y = match i(x) { Ok(a) => a, - _ => unreachable!(), + $0_ => unreachable!(), }.count_zeroes(); } ", -- cgit v1.2.3