From 8300132ed0676496ec4adda49eb682ac24bdb5b9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 23:50:29 +0200 Subject: More snippets --- .../src/handlers/replace_if_let_with_match.rs | 29 +++++++++++----------- .../src/handlers/replace_let_with_if_let.rs | 5 +--- crates/ra_assists/src/handlers/split_import.rs | 6 ++--- 3 files changed, 17 insertions(+), 23 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs index 65f5fc6ab..e016f51c3 100644 --- a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs @@ -68,7 +68,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) .indent(IndentLevel::from_node(if_expr.syntax())) }; - edit.set_cursor(if_expr.syntax().text_range().start()); edit.replace_ast::(if_expr.into(), match_expr); }) } @@ -83,7 +82,7 @@ mod tests { fn test_replace_if_let_with_match_unwraps_simple_expressions() { check_assist( replace_if_let_with_match, - " + r#" impl VariantData { pub fn is_struct(&self) -> bool { if <|>let VariantData::Struct(..) = *self { @@ -92,16 +91,16 @@ impl VariantData { false } } -} ", - " +} "#, + r#" impl VariantData { pub fn is_struct(&self) -> bool { - <|>match *self { + match *self { VariantData::Struct(..) => true, _ => false, } } -} ", +} "#, ) } @@ -109,7 +108,7 @@ impl VariantData { fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() { check_assist( replace_if_let_with_match, - " + r#" fn foo() { if <|>let VariantData::Struct(..) = a { bar( @@ -118,10 +117,10 @@ fn foo() { } else { false } -} ", - " +} "#, + r#" fn foo() { - <|>match a { + match a { VariantData::Struct(..) => { bar( 123 @@ -129,7 +128,7 @@ fn foo() { } _ => false, } -} ", +} "#, ) } @@ -137,7 +136,7 @@ fn foo() { fn replace_if_let_with_match_target() { check_assist_target( replace_if_let_with_match, - " + r#" impl VariantData { pub fn is_struct(&self) -> bool { if <|>let VariantData::Struct(..) = *self { @@ -146,7 +145,7 @@ impl VariantData { false } } -} ", +} "#, "if let VariantData::Struct(..) = *self { true } else { @@ -176,7 +175,7 @@ enum Option { Some(T), None } use Option::*; fn foo(x: Option) { - <|>match x { + match x { Some(x) => println!("{}", x), None => println!("none"), } @@ -206,7 +205,7 @@ enum Result { Ok(T), Err(E) } use Result::*; fn foo(x: Result) { - <|>match x { + match x { Ok(x) => println!("{}", x), Err(_) => println!("none"), } diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs index 482957dc6..761557ac0 100644 --- a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs @@ -58,12 +58,9 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> let stmt = make::expr_stmt(if_); let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap(); - let target_offset = - let_stmt.syntax().text_range().start() + placeholder.syntax().text_range().start(); let stmt = stmt.replace_descendant(placeholder.into(), original_pat); edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); - edit.set_cursor(target_offset); }) } @@ -88,7 +85,7 @@ fn main() { enum E { X(T), Y(T) } fn main() { - if let <|>x = E::X(92) { + if let x = E::X(92) { } } ", diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index b2757e50c..c7a874480 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs @@ -26,12 +26,10 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> if new_tree == use_tree { return None; } - let cursor = ctx.offset(); let target = colon_colon.text_range(); acc.add(AssistId("split_import"), "Split import", target, |edit| { edit.replace_ast(use_tree, new_tree); - edit.set_cursor(cursor); }) } @@ -46,7 +44,7 @@ mod tests { check_assist( split_import, "use crate::<|>db::RootDatabase;", - "use crate::<|>{db::RootDatabase};", + "use crate::{db::RootDatabase};", ) } @@ -55,7 +53,7 @@ mod tests { check_assist( split_import, "use crate:<|>:db::{RootDatabase, FileSymbol}", - "use crate:<|>:{db::{RootDatabase, FileSymbol}}", + "use crate::{db::{RootDatabase, FileSymbol}}", ) } -- cgit v1.2.3