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/early_return.rs | 18 +++++----- crates/ra_assists/src/handlers/merge_imports.rs | 22 ++++++------ crates/ra_assists/src/handlers/merge_match_arms.rs | 25 +++----------- crates/ra_assists/src/handlers/move_guard.rs | 33 ++++++------------ crates/ra_assists/src/handlers/remove_dbg.rs | 39 +++++----------------- crates/ra_assists/src/handlers/remove_mut.rs | 5 ++- 6 files changed, 45 insertions(+), 97 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs index 66b296081..4cc75a7ce 100644 --- a/crates/ra_assists/src/handlers/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs @@ -97,7 +97,6 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) } then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; - let cursor_position = ctx.offset(); let target = if_expr.syntax().text_range(); acc.add(AssistId("convert_to_guarded_return"), "Convert to guarded return", target, |edit| { @@ -148,7 +147,6 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) } }; edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap()); - edit.set_cursor(cursor_position); fn replace( new_expr: &SyntaxNode, @@ -207,7 +205,7 @@ mod tests { r#" fn main() { bar(); - if<|> !true { + if !true { return; } foo(); @@ -237,7 +235,7 @@ mod tests { r#" fn main(n: Option) { bar(); - le<|>t n = match n { + let n = match n { Some(it) => it, _ => return, }; @@ -263,7 +261,7 @@ mod tests { "#, r#" fn main() { - le<|>t x = match Err(92) { + let x = match Err(92) { Ok(it) => it, _ => return, }; @@ -291,7 +289,7 @@ mod tests { r#" fn main(n: Option) { bar(); - le<|>t n = match n { + let n = match n { Ok(it) => it, _ => return, }; @@ -321,7 +319,7 @@ mod tests { r#" fn main() { while true { - if<|> !true { + if !true { continue; } foo(); @@ -349,7 +347,7 @@ mod tests { r#" fn main() { while true { - le<|>t n = match n { + let n = match n { Some(it) => it, _ => continue, }; @@ -378,7 +376,7 @@ mod tests { r#" fn main() { loop { - if<|> !true { + if !true { continue; } foo(); @@ -406,7 +404,7 @@ mod tests { r#" fn main() { loop { - le<|>t n = match n { + let n = match n { Some(it) => it, _ => continue, }; diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index ac3e53c27..972d16241 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -58,8 +58,6 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<() let target = tree.syntax().text_range(); acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| { builder.rewrite(rewriter); - // FIXME: we only need because our diff is imprecise - builder.set_cursor(offset); }) } @@ -142,7 +140,7 @@ use std::fmt<|>::Debug; use std::fmt::Display; ", r" -use std::fmt<|>::{Debug, Display}; +use std::fmt::{Debug, Display}; ", ) } @@ -156,7 +154,7 @@ use std::fmt::Debug; use std::fmt<|>::Display; ", r" -use std::fmt:<|>:{Display, Debug}; +use std::fmt::{Display, Debug}; ", ); } @@ -169,7 +167,7 @@ use std::fmt:<|>:{Display, Debug}; use std::{fmt<|>::Debug, fmt::Display}; ", r" -use std::{fmt<|>::{Debug, Display}}; +use std::{fmt::{Debug, Display}}; ", ); check_assist( @@ -178,7 +176,7 @@ use std::{fmt<|>::{Debug, Display}}; use std::{fmt::Debug, fmt<|>::Display}; ", r" -use std::{fmt::<|>{Display, Debug}}; +use std::{fmt::{Display, Debug}}; ", ); } @@ -192,7 +190,7 @@ use std<|>::cell::*; use std::str; ", r" -use std<|>::{cell::*, str}; +use std::{cell::*, str}; ", ) } @@ -206,7 +204,7 @@ use std<|>::cell::*; use std::str::*; ", r" -use std<|>::{cell::*, str::*}; +use std::{cell::*, str::*}; ", ) } @@ -222,7 +220,7 @@ use foo::baz; /// Doc comment ", r" -use foo<|>::{bar, baz}; +use foo::{bar, baz}; /// Doc comment ", @@ -241,7 +239,7 @@ use { ", r" use { - foo<|>::{bar, baz}, + foo::{bar, baz}, }; ", ); @@ -255,7 +253,7 @@ use { ", r" use { - foo::{bar<|>, baz}, + foo::{bar, baz}, }; ", ); @@ -272,7 +270,7 @@ use foo::<|>{ }; ", r" -use foo::{<|> +use foo::{ FooBar, bar::baz}; ", diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index d4e38aa6a..ca04ec671 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs @@ -3,7 +3,7 @@ use std::iter::successors; use ra_syntax::{ algo::neighbor, ast::{self, AstNode}, - Direction, TextSize, + Direction, }; use crate::{AssistContext, AssistId, Assists, TextRange}; @@ -41,17 +41,6 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option let current_expr = current_arm.expr()?; let current_text_range = current_arm.syntax().text_range(); - enum CursorPos { - InExpr(TextSize), - InPat(TextSize), - } - let cursor_pos = ctx.offset(); - let cursor_pos = if current_expr.syntax().text_range().contains(cursor_pos) { - CursorPos::InExpr(current_text_range.end() - cursor_pos) - } else { - CursorPos::InPat(cursor_pos) - }; - // We check if the following match arms match this one. We could, but don't, // compare to the previous match arm as well. let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) @@ -87,10 +76,6 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option let start = arms_to_merge.first().unwrap().syntax().text_range().start(); let end = arms_to_merge.last().unwrap().syntax().text_range().end(); - edit.set_cursor(match cursor_pos { - CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset, - CursorPos::InPat(offset) => offset, - }); edit.replace(TextRange::new(start, end), arm); }) } @@ -132,7 +117,7 @@ mod tests { fn main() { let x = X::A; let y = match x { - X::A | X::B => { 1i32<|> } + X::A | X::B => { 1i32 } X::C => { 2i32 } } } @@ -164,7 +149,7 @@ mod tests { fn main() { let x = X::A; let y = match x { - X::A | X::B | X::C | X::D => {<|> 1i32 }, + X::A | X::B | X::C | X::D => { 1i32 }, X::E => { 2i32 }, } } @@ -197,7 +182,7 @@ mod tests { let x = X::A; let y = match x { X::A => { 1i32 }, - _ => { 2i<|>32 } + _ => { 2i32 } } } "#, @@ -226,7 +211,7 @@ mod tests { fn main() { match X::A { - X::A<|> | X::B | X::C => 92, + X::A | X::B | X::C => 92, X::D => 62, _ => panic!(), } 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 }, diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index 8eef578cf..961ee1731 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs @@ -29,26 +29,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let macro_range = macro_call.syntax().text_range(); - // If the cursor is inside the macro call, we'll try to maintain the cursor - // position by subtracting the length of dbg!( from the start of the file - // range, otherwise we'll default to using the start of the macro call - let cursor_pos = { - let file_range = ctx.frange.range; - - let offset_start = file_range - .start() - .checked_sub(macro_range.start()) - .unwrap_or_else(|| TextSize::from(0)); - - let dbg_size = TextSize::of("dbg!("); - - if offset_start > dbg_size { - file_range.start() - dbg_size - } else { - macro_range.start() - } - }; - let macro_content = { let macro_args = macro_call.token_tree()?.syntax().clone(); @@ -58,9 +38,8 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { }; let target = macro_call.syntax().text_range(); - acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |edit| { - edit.replace(macro_range, macro_content); - edit.set_cursor(cursor_pos); + acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |builder| { + builder.replace(macro_range, macro_content); }) } @@ -94,13 +73,13 @@ mod tests { #[test] fn test_remove_dbg() { - check_assist(remove_dbg, "<|>dbg!(1 + 1)", "<|>1 + 1"); + check_assist(remove_dbg, "<|>dbg!(1 + 1)", "1 + 1"); - check_assist(remove_dbg, "dbg!<|>((1 + 1))", "<|>(1 + 1)"); + check_assist(remove_dbg, "dbg!<|>((1 + 1))", "(1 + 1)"); - check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 <|>+ 1"); + check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 + 1"); - check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = <|>1 + 1"); + check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = 1 + 1"); check_assist( remove_dbg, @@ -113,7 +92,7 @@ fn foo(n: usize) { ", " fn foo(n: usize) { - if let Some(_) = n.<|>checked_sub(4) { + if let Some(_) = n.checked_sub(4) { // ... } } @@ -122,8 +101,8 @@ fn foo(n: usize) { } #[test] fn test_remove_dbg_with_brackets_and_braces() { - check_assist(remove_dbg, "dbg![<|>1 + 1]", "<|>1 + 1"); - check_assist(remove_dbg, "dbg!{<|>1 + 1}", "<|>1 + 1"); + check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); + check_assist(remove_dbg, "dbg!{<|>1 + 1}", "1 + 1"); } #[test] diff --git a/crates/ra_assists/src/handlers/remove_mut.rs b/crates/ra_assists/src/handlers/remove_mut.rs index dce546db7..fe4eada03 100644 --- a/crates/ra_assists/src/handlers/remove_mut.rs +++ b/crates/ra_assists/src/handlers/remove_mut.rs @@ -26,8 +26,7 @@ pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { }; let target = mut_token.text_range(); - acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |edit| { - edit.set_cursor(delete_from); - edit.delete(TextRange::new(delete_from, delete_to)); + acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |builder| { + builder.delete(TextRange::new(delete_from, delete_to)); }) } -- cgit v1.2.3