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') 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 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 +++++++++++++++------- crates/ra_assists/src/tests/generated.rs | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) (limited to 'crates') 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(); } ", diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 0eeb5c199..250e56a69 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -764,7 +764,7 @@ fn main() { let x: Result = Result::Ok(92); let y = match x { Ok(a) => a, - _ => unreachable!(), + $0_ => unreachable!(), }; } "#####, -- cgit v1.2.3 From 70930d3bb2ba1d4a7a7d4a489da714096294acca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 May 2020 00:03:42 +0200 Subject: Remove set_cursor --- crates/ra_assists/src/assist_context.rs | 20 +++----------------- crates/ra_assists/src/handlers/unwrap_block.rs | 24 ++++++++++-------------- 2 files changed, 13 insertions(+), 31 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index 005c17776..9f6ca449b 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs @@ -171,19 +171,13 @@ impl Assists { pub(crate) struct AssistBuilder { edit: TextEditBuilder, - cursor_position: Option, file: FileId, is_snippet: bool, } impl AssistBuilder { pub(crate) fn new(file: FileId) -> AssistBuilder { - AssistBuilder { - edit: TextEditBuilder::default(), - cursor_position: None, - file, - is_snippet: false, - } + AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false } } /// Remove specified `range` of text. @@ -241,10 +235,6 @@ impl AssistBuilder { algo::diff(&node, &new).into_text_edit(&mut self.edit) } - /// Specify desired position of the cursor after the assist is applied. - pub(crate) fn set_cursor(&mut self, offset: TextSize) { - self.cursor_position = Some(offset) - } // FIXME: better API pub(crate) fn set_file(&mut self, assist_file: FileId) { self.file = assist_file; @@ -258,12 +248,8 @@ impl AssistBuilder { fn finish(self, change_label: String) -> SourceChange { let edit = self.edit.finish(); - if edit.is_empty() && self.cursor_position.is_none() { - panic!("Only call `add_assist` if the assist can be applied") - } - let mut res = - SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } - .into_source_change(self.file); + let mut res = SingleFileChange { label: change_label, edit, cursor_position: None } + .into_source_change(self.file); if self.is_snippet { res.is_snippet = true; } diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index b76182d79..8440c7d0f 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -62,7 +62,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end()); - edit.set_cursor(ancestor_then_branch.syntax().text_range().end()); edit.delete(range_to_del_rest); edit.delete(range_to_del_else_if); edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); @@ -79,7 +78,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> return acc.add(assist_id, assist_label, target, |edit| { let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); - edit.set_cursor(then_branch.syntax().text_range().end()); edit.delete(range_to_del); edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); }); @@ -97,8 +95,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let target = expr_to_unwrap.syntax().text_range(); acc.add(assist_id, assist_label, target, |edit| { - edit.set_cursor(expr.syntax().text_range().start()); - edit.replace( expr.syntax().text_range(), update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), @@ -154,7 +150,7 @@ mod tests { r#" fn main() { bar(); - <|>foo(); + foo(); //comment bar(); @@ -188,7 +184,7 @@ mod tests { //comment bar(); - }<|> + } println!("bar"); } "#, @@ -222,7 +218,7 @@ mod tests { //comment //bar(); - }<|> + } println!("bar"); } "#, @@ -258,7 +254,7 @@ mod tests { //bar(); } else if false { println!("bar"); - }<|> + } println!("foo"); } "#, @@ -298,7 +294,7 @@ mod tests { println!("bar"); } else if true { println!("foo"); - }<|> + } println!("else"); } "#, @@ -336,7 +332,7 @@ mod tests { //bar(); } else if false { println!("bar"); - }<|> + } println!("foo"); } "#, @@ -383,7 +379,7 @@ mod tests { "#, r#" fn main() { - <|>if true { + if true { foo(); //comment @@ -417,7 +413,7 @@ mod tests { r#" fn main() { for i in 0..5 { - <|>foo(); + foo(); //comment bar(); @@ -447,7 +443,7 @@ mod tests { "#, r#" fn main() { - <|>if true { + if true { foo(); //comment @@ -480,7 +476,7 @@ mod tests { "#, r#" fn main() { - <|>if true { + if true { foo(); //comment -- cgit v1.2.3