From fd771707187a505c826096fc62ced6ba9b65460e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 23:07:17 +0200 Subject: Snippetify introduce/inline var --- .../src/handlers/inline_local_variable.rs | 47 +++++---- .../ra_assists/src/handlers/introduce_variable.rs | 112 ++++++++++++--------- crates/ra_assists/src/tests/generated.rs | 2 +- 3 files changed, 87 insertions(+), 74 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index 46d675a4e..d26e68847 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs @@ -116,7 +116,6 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() }; builder.replace(desc.file_range.range, replacement) } - builder.set_cursor(delete_range.start()) }) } @@ -149,7 +148,7 @@ fn foo() { r" fn bar(a: usize) {} fn foo() { - <|>1 + 1; + 1 + 1; if 1 > 10 { } @@ -183,7 +182,7 @@ fn foo() { r" fn bar(a: usize) {} fn foo() { - <|>(1 + 1) + 1; + (1 + 1) + 1; if (1 + 1) > 10 { } @@ -217,7 +216,7 @@ fn foo() { r" fn bar(a: usize) {} fn foo() { - <|>bar(1) + 1; + bar(1) + 1; if bar(1) > 10 { } @@ -251,7 +250,7 @@ fn foo() { r" fn bar(a: usize): usize { a } fn foo() { - <|>(bar(1) as u64) + 1; + (bar(1) as u64) + 1; if (bar(1) as u64) > 10 { } @@ -283,7 +282,7 @@ fn foo() { }", r" fn foo() { - <|>{ 10 + 1 } + 1; + { 10 + 1 } + 1; if { 10 + 1 } > 10 { } @@ -315,7 +314,7 @@ fn foo() { }", r" fn foo() { - <|>( 10 + 1 ) + 1; + ( 10 + 1 ) + 1; if ( 10 + 1 ) > 10 { } @@ -353,7 +352,7 @@ fn foo() { }", r" fn foo() { - <|>let b = bar(10 + 1) * 10; + let b = bar(10 + 1) * 10; let c = bar(10 + 1) as usize; }", ); @@ -373,7 +372,7 @@ fn foo() { r" fn foo() { let x = vec![1, 2, 3]; - <|>let b = x[0] * 10; + let b = x[0] * 10; let c = x[0] as usize; }", ); @@ -393,7 +392,7 @@ fn foo() { r" fn foo() { let bar = vec![1]; - <|>let b = bar.len() * 10; + let b = bar.len() * 10; let c = bar.len() as usize; }", ); @@ -421,7 +420,7 @@ struct Bar { fn foo() { let bar = Bar { foo: 1 }; - <|>let b = bar.foo * 10; + let b = bar.foo * 10; let c = bar.foo as usize; }", ); @@ -442,7 +441,7 @@ fn foo() -> Option { r" fn foo() -> Option { let bar = Some(1); - <|>let b = bar? * 10; + let b = bar? * 10; let c = bar? as usize; None }", @@ -462,7 +461,7 @@ fn foo() { r" fn foo() { let bar = 10; - <|>let b = &bar * 10; + let b = &bar * 10; }", ); } @@ -478,7 +477,7 @@ fn foo() { }", r" fn foo() { - <|>let b = (10, 20)[0]; + let b = (10, 20)[0]; }", ); } @@ -494,7 +493,7 @@ fn foo() { }", r" fn foo() { - <|>let b = [1, 2, 3].len(); + let b = [1, 2, 3].len(); }", ); } @@ -511,7 +510,7 @@ fn foo() { }", r" fn foo() { - <|>let b = (10 + 20) * 10; + let b = (10 + 20) * 10; let c = (10 + 20) as usize; }", ); @@ -531,7 +530,7 @@ fn foo() { r" fn foo() { let d = 10; - <|>let b = d * 10; + let b = d * 10; let c = d as usize; }", ); @@ -549,7 +548,7 @@ fn foo() { }", r" fn foo() { - <|>let b = { 10 } * 10; + let b = { 10 } * 10; let c = { 10 } as usize; }", ); @@ -569,7 +568,7 @@ fn foo() { }", r" fn foo() { - <|>let b = (10 + 20) * 10; + let b = (10 + 20) * 10; let c = (10 + 20, 20); let d = [10 + 20, 10]; let e = (10 + 20); @@ -588,7 +587,7 @@ fn foo() { }", r" fn foo() { - <|>for i in vec![10, 20] {} + for i in vec![10, 20] {} }", ); } @@ -604,7 +603,7 @@ fn foo() { }", r" fn foo() { - <|>while 1 > 0 {} + while 1 > 0 {} }", ); } @@ -622,7 +621,7 @@ fn foo() { }", r" fn foo() { - <|>loop { + loop { break 1 + 1; } }", @@ -640,7 +639,7 @@ fn foo() { }", r" fn foo() { - <|>return 1 > 0; + return 1 > 0; }", ); } @@ -656,7 +655,7 @@ fn foo() { }", r" fn foo() { - <|>match 1 > 0 {} + match 1 > 0 {} }", ); } diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index 56c610fed..31d6539f7 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs @@ -4,7 +4,7 @@ use ra_syntax::{ BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, WHITESPACE, }, - SyntaxNode, TextSize, + SyntaxNode, }; use stdx::format_to; use test_utils::mark; @@ -23,7 +23,7 @@ use crate::{AssistContext, AssistId, Assists}; // -> // ``` // fn main() { -// let var_name = (1 + 2); +// let $0var_name = (1 + 2); // var_name * 4; // } // ``` @@ -46,14 +46,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| { let mut buf = String::new(); - let cursor_offset = if wrap_in_block { + if wrap_in_block { buf.push_str("{ let var_name = "); - TextSize::of("{ let ") } else { buf.push_str("let var_name = "); - TextSize::of("let ") }; format_to!(buf, "{}", expr.syntax()); + let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); let is_full_stmt = if let Some(expr_stmt) = &full_stmt { Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) @@ -65,28 +64,43 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti if full_stmt.unwrap().semicolon_token().is_none() { buf.push_str(";"); } - edit.replace(expr.syntax().text_range(), buf); - } else { - buf.push_str(";"); - - // We want to maintain the indent level, - // but we do not want to duplicate possible - // extra newlines in the indent block - let text = indent.text(); - if text.starts_with('\n') { - buf.push_str("\n"); - buf.push_str(text.trim_start_matches('\n')); - } else { - buf.push_str(text); + let offset = expr.syntax().text_range(); + match ctx.config.snippet_cap { + Some(cap) => { + let snip = buf.replace("let var_name", "let $0var_name"); + edit.replace_snippet(cap, offset, snip) + } + None => edit.replace(offset, buf), } + return; + } - edit.replace(expr.syntax().text_range(), "var_name".to_string()); - edit.insert(anchor_stmt.text_range().start(), buf); - if wrap_in_block { - edit.insert(anchor_stmt.text_range().end(), " }"); + buf.push_str(";"); + + // We want to maintain the indent level, + // but we do not want to duplicate possible + // extra newlines in the indent block + let text = indent.text(); + if text.starts_with('\n') { + buf.push_str("\n"); + buf.push_str(text.trim_start_matches('\n')); + } else { + buf.push_str(text); + } + + edit.replace(expr.syntax().text_range(), "var_name".to_string()); + let offset = anchor_stmt.text_range().start(); + match ctx.config.snippet_cap { + Some(cap) => { + let snip = buf.replace("let var_name", "let $0var_name"); + edit.insert_snippet(cap, offset, snip) } + None => edit.insert(offset, buf), + } + + if wrap_in_block { + edit.insert(anchor_stmt.text_range().end(), " }"); } - edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); }) } @@ -144,15 +158,15 @@ mod tests { fn test_introduce_var_simple() { check_assist( introduce_variable, - " + r#" fn foo() { foo(<|>1 + 1<|>); -}", - " +}"#, + r#" fn foo() { - let <|>var_name = 1 + 1; + let $0var_name = 1 + 1; foo(var_name); -}", +}"#, ); } @@ -167,14 +181,14 @@ fn foo() { mark::check!(test_introduce_var_expr_stmt); check_assist( introduce_variable, - " + r#" fn foo() { <|>1 + 1<|>; -}", - " +}"#, + r#" fn foo() { - let <|>var_name = 1 + 1; -}", + let $0var_name = 1 + 1; +}"#, ); check_assist( introduce_variable, @@ -185,7 +199,7 @@ fn foo() { }", " fn foo() { - let <|>var_name = { let x = 0; x }; + let $0var_name = { let x = 0; x }; something_else(); }", ); @@ -201,7 +215,7 @@ fn foo() { }", " fn foo() { - let <|>var_name = 1; + let $0var_name = 1; var_name + 1; }", ); @@ -218,7 +232,7 @@ fn foo() { }", " fn foo() { - let <|>var_name = 1 + 1; + let $0var_name = 1 + 1; bar(var_name) }", ); @@ -230,7 +244,7 @@ fn foo() { }", " fn foo() { - let <|>var_name = bar(1 + 1); + let $0var_name = bar(1 + 1); var_name }", ) @@ -253,7 +267,7 @@ fn main() { fn main() { let x = true; let tuple = match x { - true => { let <|>var_name = 2 + 2; (var_name, true) } + true => { let $0var_name = 2 + 2; (var_name, true) } _ => (0, false) }; } @@ -283,7 +297,7 @@ fn main() { let tuple = match x { true => { let y = 1; - let <|>var_name = 2 + y; + let $0var_name = 2 + y; (var_name, true) } _ => (0, false) @@ -304,7 +318,7 @@ fn main() { ", " fn main() { - let lambda = |x: u32| { let <|>var_name = x * 2; var_name }; + let lambda = |x: u32| { let $0var_name = x * 2; var_name }; } ", ); @@ -321,7 +335,7 @@ fn main() { ", " fn main() { - let lambda = |x: u32| { let <|>var_name = x * 2; var_name }; + let lambda = |x: u32| { let $0var_name = x * 2; var_name }; } ", ); @@ -338,7 +352,7 @@ fn main() { ", " fn main() { - let <|>var_name = Some(true); + let $0var_name = Some(true); let o = var_name; } ", @@ -356,7 +370,7 @@ fn main() { ", " fn main() { - let <|>var_name = bar.foo(); + let $0var_name = bar.foo(); let v = var_name; } ", @@ -374,7 +388,7 @@ fn foo() -> u32 { ", " fn foo() -> u32 { - let <|>var_name = 2 + 2; + let $0var_name = 2 + 2; return var_name; } ", @@ -396,7 +410,7 @@ fn foo() -> u32 { fn foo() -> u32 { - let <|>var_name = 2 + 2; + let $0var_name = 2 + 2; return var_name; } ", @@ -413,7 +427,7 @@ fn foo() -> u32 { " fn foo() -> u32 { - let <|>var_name = 2 + 2; + let $0var_name = 2 + 2; return var_name; } ", @@ -438,7 +452,7 @@ fn foo() -> u32 { // bar - let <|>var_name = 2 + 2; + let $0var_name = 2 + 2; return var_name; } ", @@ -459,7 +473,7 @@ fn main() { " fn main() { let result = loop { - let <|>var_name = 2 + 2; + let $0var_name = 2 + 2; break var_name; }; } @@ -478,7 +492,7 @@ fn main() { ", " fn main() { - let <|>var_name = 0f32 as u32; + let $0var_name = 0f32 as u32; let v = var_name; } ", diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 3e6654c17..0eeb5c199 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -443,7 +443,7 @@ fn main() { "#####, r#####" fn main() { - let var_name = (1 + 2); + let $0var_name = (1 + 2); var_name * 4; } "#####, -- 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/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