From 4f2134cc33f07c09fe166cec42971828843bc0ef Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 01:18:19 +0200 Subject: Introduce EffectExpr --- crates/ra_assists/src/handlers/move_guard.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_guard.rs') diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index d5ccdd91c..b084dd9ee 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs @@ -113,9 +113,9 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { "Move condition to match guard", |edit| { edit.target(if_expr.syntax().text_range()); - let then_only_expr = then_block.block().and_then(|it| it.statements().next()).is_none(); + let then_only_expr = then_block.statements().next().is_none(); - match &then_block.block().and_then(|it| it.expr()) { + match &then_block.expr() { Some(then_expr) if then_only_expr => { edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) } -- cgit v1.2.3 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_guard.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/handlers/move_guard.rs') diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index b084dd9ee..f2aa7e594 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs @@ -132,7 +132,7 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { mod tests { use super::*; - use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; #[test] fn move_guard_to_arm_body_target() { -- 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_guard.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_guard.rs') diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index f2aa7e594..29bc9a9ff 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs @@ -40,8 +40,8 @@ pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { let arm_expr = match_arm.expr()?; let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); - ctx.add_assist(AssistId("move_guard_to_arm_body"), "Move guard to arm body", |edit| { - edit.target(guard.syntax().text_range()); + let target = guard.syntax().text_range(); + ctx.add_assist(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { Some(tok) => { if ast::Whitespace::cast(tok.clone()).is_some() { @@ -108,11 +108,12 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { let buf = format!(" if {}", cond.syntax().text()); + let target = if_expr.syntax().text_range(); ctx.add_assist( AssistId("move_arm_cond_to_match_guard"), "Move condition to match guard", + target, |edit| { - edit.target(if_expr.syntax().text_range()); let then_only_expr = then_block.statements().next().is_none(); match &then_block.expr() { -- 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_guard.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_guard.rs') diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index 29bc9a9ff..fc0335b57 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs @@ -4,7 +4,7 @@ use ra_syntax::{ TextSize, }; -use crate::{Assist, AssistCtx, AssistId}; +use crate::{AssistContext, AssistId, Assists}; // Assist: move_guard_to_arm_body // @@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // } // ``` -pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { +pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let match_arm = ctx.find_node_at_offset::()?; let guard = match_arm.guard()?; let space_before_guard = guard.syntax().prev_sibling_or_token(); @@ -41,7 +41,7 @@ pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); let target = guard.syntax().text_range(); - ctx.add_assist(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { + acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { Some(tok) => { if ast::Whitespace::cast(tok.clone()).is_some() { @@ -88,7 +88,7 @@ pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { // } // } // ``` -pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { +pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let match_arm: MatchArm = ctx.find_node_at_offset::()?; let match_pat = match_arm.pat()?; @@ -109,7 +109,7 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { let buf = format!(" if {}", cond.syntax().text()); let target = if_expr.syntax().text_range(); - ctx.add_assist( + acc.add( AssistId("move_arm_cond_to_match_guard"), "Move condition to match guard", target, -- cgit v1.2.3 From 5e13e4eba17fe0d55afa76c8d5ff495228283c4e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 23:14:31 +0200 Subject: More snippets --- crates/ra_assists/src/handlers/move_guard.rs | 33 ++++++++++------------------ 1 file changed, 11 insertions(+), 22 deletions(-) (limited to 'crates/ra_assists/src/handlers/move_guard.rs') diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index fc0335b57..7edcf0748 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs @@ -1,7 +1,6 @@ use ra_syntax::{ - ast, - ast::{AstNode, AstToken, IfExpr, MatchArm}, - TextSize, + ast::{AstNode, IfExpr, MatchArm}, + SyntaxKind::WHITESPACE, }; use crate::{AssistContext, AssistId, Assists}; @@ -42,24 +41,15 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> let target = guard.syntax().text_range(); acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { - let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { - Some(tok) => { - if ast::Whitespace::cast(tok.clone()).is_some() { - let ele = tok.text_range(); - edit.delete(ele); - ele.len() - } else { - TextSize::from(0) - } + match space_before_guard { + Some(element) if element.kind() == WHITESPACE => { + edit.delete(element.text_range()); } - _ => TextSize::from(0), + _ => (), }; edit.delete(guard.syntax().text_range()); edit.replace_node_and_indent(arm_expr.syntax(), buf); - edit.set_cursor( - arm_expr.syntax().text_range().start() + TextSize::from(3) - offseting_amount, - ); }) } @@ -124,7 +114,6 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex } edit.insert(match_pat.syntax().text_range().end(), buf); - edit.set_cursor(match_pat.syntax().text_range().end() + TextSize::from(1)); }, ) } @@ -172,7 +161,7 @@ mod tests { let t = 'a'; let chars = "abcd"; match t { - '\r' => if chars.clone().next() == Some('\n') { <|>false }, + '\r' => if chars.clone().next() == Some('\n') { false }, _ => true } } @@ -195,7 +184,7 @@ mod tests { r#" fn f() { match x { - y @ 4 | y @ 5 => if y > 5 { <|>true }, + y @ 4 | y @ 5 => if y > 5 { true }, _ => false } } @@ -222,7 +211,7 @@ mod tests { let t = 'a'; let chars = "abcd"; match t { - '\r' <|>if chars.clone().next() == Some('\n') => false, + '\r' if chars.clone().next() == Some('\n') => false, _ => true } } @@ -266,7 +255,7 @@ mod tests { let t = 'a'; let chars = "abcd"; match t { - '\r' <|>if chars.clone().next().is_some() => { }, + '\r' if chars.clone().next().is_some() => { }, _ => true } } @@ -296,7 +285,7 @@ mod tests { let mut t = 'a'; let chars = "abcd"; match t { - '\r' <|>if chars.clone().next().is_some() => { + '\r' if chars.clone().next().is_some() => { t = 'e'; false }, -- cgit v1.2.3