More snippets

This commit is contained in:
Aleksey Kladov 2020-05-20 23:14:31 +02:00
parent fd77170718
commit 5e13e4eba1
6 changed files with 45 additions and 97 deletions

View file

@ -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)?; 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(); let target = if_expr.syntax().text_range();
acc.add(AssistId("convert_to_guarded_return"), "Convert to guarded return", target, |edit| { 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.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap());
edit.set_cursor(cursor_position);
fn replace( fn replace(
new_expr: &SyntaxNode, new_expr: &SyntaxNode,
@ -207,7 +205,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
bar(); bar();
if<|> !true { if !true {
return; return;
} }
foo(); foo();
@ -237,7 +235,7 @@ mod tests {
r#" r#"
fn main(n: Option<String>) { fn main(n: Option<String>) {
bar(); bar();
le<|>t n = match n { let n = match n {
Some(it) => it, Some(it) => it,
_ => return, _ => return,
}; };
@ -263,7 +261,7 @@ mod tests {
"#, "#,
r#" r#"
fn main() { fn main() {
le<|>t x = match Err(92) { let x = match Err(92) {
Ok(it) => it, Ok(it) => it,
_ => return, _ => return,
}; };
@ -291,7 +289,7 @@ mod tests {
r#" r#"
fn main(n: Option<String>) { fn main(n: Option<String>) {
bar(); bar();
le<|>t n = match n { let n = match n {
Ok(it) => it, Ok(it) => it,
_ => return, _ => return,
}; };
@ -321,7 +319,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
while true { while true {
if<|> !true { if !true {
continue; continue;
} }
foo(); foo();
@ -349,7 +347,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
while true { while true {
le<|>t n = match n { let n = match n {
Some(it) => it, Some(it) => it,
_ => continue, _ => continue,
}; };
@ -378,7 +376,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
loop { loop {
if<|> !true { if !true {
continue; continue;
} }
foo(); foo();
@ -406,7 +404,7 @@ mod tests {
r#" r#"
fn main() { fn main() {
loop { loop {
le<|>t n = match n { let n = match n {
Some(it) => it, Some(it) => it,
_ => continue, _ => continue,
}; };

View file

@ -58,8 +58,6 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()
let target = tree.syntax().text_range(); let target = tree.syntax().text_range();
acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| { acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| {
builder.rewrite(rewriter); 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; use std::fmt::Display;
", ",
r" r"
use std::fmt<|>::{Debug, Display}; use std::fmt::{Debug, Display};
", ",
) )
} }
@ -156,7 +154,7 @@ use std::fmt::Debug;
use std::fmt<|>::Display; use std::fmt<|>::Display;
", ",
r" 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}; use std::{fmt<|>::Debug, fmt::Display};
", ",
r" r"
use std::{fmt<|>::{Debug, Display}}; use std::{fmt::{Debug, Display}};
", ",
); );
check_assist( check_assist(
@ -178,7 +176,7 @@ use std::{fmt<|>::{Debug, Display}};
use std::{fmt::Debug, fmt<|>::Display}; use std::{fmt::Debug, fmt<|>::Display};
", ",
r" r"
use std::{fmt::<|>{Display, Debug}}; use std::{fmt::{Display, Debug}};
", ",
); );
} }
@ -192,7 +190,7 @@ use std<|>::cell::*;
use std::str; use std::str;
", ",
r" r"
use std<|>::{cell::*, str}; use std::{cell::*, str};
", ",
) )
} }
@ -206,7 +204,7 @@ use std<|>::cell::*;
use std::str::*; use std::str::*;
", ",
r" r"
use std<|>::{cell::*, str::*}; use std::{cell::*, str::*};
", ",
) )
} }
@ -222,7 +220,7 @@ use foo::baz;
/// Doc comment /// Doc comment
", ",
r" r"
use foo<|>::{bar, baz}; use foo::{bar, baz};
/// Doc comment /// Doc comment
", ",
@ -241,7 +239,7 @@ use {
", ",
r" r"
use { use {
foo<|>::{bar, baz}, foo::{bar, baz},
}; };
", ",
); );
@ -255,7 +253,7 @@ use {
", ",
r" r"
use { use {
foo::{bar<|>, baz}, foo::{bar, baz},
}; };
", ",
); );
@ -272,7 +270,7 @@ use foo::<|>{
}; };
", ",
r" r"
use foo::{<|> use foo::{
FooBar, FooBar,
bar::baz}; bar::baz};
", ",

View file

@ -3,7 +3,7 @@ use std::iter::successors;
use ra_syntax::{ use ra_syntax::{
algo::neighbor, algo::neighbor,
ast::{self, AstNode}, ast::{self, AstNode},
Direction, TextSize, Direction,
}; };
use crate::{AssistContext, AssistId, Assists, TextRange}; 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_expr = current_arm.expr()?;
let current_text_range = current_arm.syntax().text_range(); 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, // We check if the following match arms match this one. We could, but don't,
// compare to the previous match arm as well. // compare to the previous match arm as well.
let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) 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 start = arms_to_merge.first().unwrap().syntax().text_range().start();
let end = arms_to_merge.last().unwrap().syntax().text_range().end(); 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); edit.replace(TextRange::new(start, end), arm);
}) })
} }
@ -132,7 +117,7 @@ mod tests {
fn main() { fn main() {
let x = X::A; let x = X::A;
let y = match x { let y = match x {
X::A | X::B => { 1i32<|> } X::A | X::B => { 1i32 }
X::C => { 2i32 } X::C => { 2i32 }
} }
} }
@ -164,7 +149,7 @@ mod tests {
fn main() { fn main() {
let x = X::A; let x = X::A;
let y = match x { 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 }, X::E => { 2i32 },
} }
} }
@ -197,7 +182,7 @@ mod tests {
let x = X::A; let x = X::A;
let y = match x { let y = match x {
X::A => { 1i32 }, X::A => { 1i32 },
_ => { 2i<|>32 } _ => { 2i32 }
} }
} }
"#, "#,
@ -226,7 +211,7 @@ mod tests {
fn main() { fn main() {
match X::A { match X::A {
X::A<|> | X::B | X::C => 92, X::A | X::B | X::C => 92,
X::D => 62, X::D => 62,
_ => panic!(), _ => panic!(),
} }

View file

@ -1,7 +1,6 @@
use ra_syntax::{ use ra_syntax::{
ast, ast::{AstNode, IfExpr, MatchArm},
ast::{AstNode, AstToken, IfExpr, MatchArm}, SyntaxKind::WHITESPACE,
TextSize,
}; };
use crate::{AssistContext, AssistId, Assists}; 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(); let target = guard.syntax().text_range();
acc.add(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()) { match space_before_guard {
Some(tok) => { Some(element) if element.kind() == WHITESPACE => {
if ast::Whitespace::cast(tok.clone()).is_some() { edit.delete(element.text_range());
let ele = tok.text_range();
edit.delete(ele);
ele.len()
} else {
TextSize::from(0)
}
} }
_ => TextSize::from(0), _ => (),
}; };
edit.delete(guard.syntax().text_range()); edit.delete(guard.syntax().text_range());
edit.replace_node_and_indent(arm_expr.syntax(), buf); 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.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 t = 'a';
let chars = "abcd"; let chars = "abcd";
match t { match t {
'\r' => if chars.clone().next() == Some('\n') { <|>false }, '\r' => if chars.clone().next() == Some('\n') { false },
_ => true _ => true
} }
} }
@ -195,7 +184,7 @@ mod tests {
r#" r#"
fn f() { fn f() {
match x { match x {
y @ 4 | y @ 5 => if y > 5 { <|>true }, y @ 4 | y @ 5 => if y > 5 { true },
_ => false _ => false
} }
} }
@ -222,7 +211,7 @@ mod tests {
let t = 'a'; let t = 'a';
let chars = "abcd"; let chars = "abcd";
match t { match t {
'\r' <|>if chars.clone().next() == Some('\n') => false, '\r' if chars.clone().next() == Some('\n') => false,
_ => true _ => true
} }
} }
@ -266,7 +255,7 @@ mod tests {
let t = 'a'; let t = 'a';
let chars = "abcd"; let chars = "abcd";
match t { match t {
'\r' <|>if chars.clone().next().is_some() => { }, '\r' if chars.clone().next().is_some() => { },
_ => true _ => true
} }
} }
@ -296,7 +285,7 @@ mod tests {
let mut t = 'a'; let mut t = 'a';
let chars = "abcd"; let chars = "abcd";
match t { match t {
'\r' <|>if chars.clone().next().is_some() => { '\r' if chars.clone().next().is_some() => {
t = 'e'; t = 'e';
false false
}, },

View file

@ -29,26 +29,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let macro_range = macro_call.syntax().text_range(); 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_content = {
let macro_args = macro_call.token_tree()?.syntax().clone(); 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(); let target = macro_call.syntax().text_range();
acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |edit| { acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |builder| {
edit.replace(macro_range, macro_content); builder.replace(macro_range, macro_content);
edit.set_cursor(cursor_pos);
}) })
} }
@ -94,13 +73,13 @@ mod tests {
#[test] #[test]
fn test_remove_dbg() { 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( check_assist(
remove_dbg, remove_dbg,
@ -113,7 +92,7 @@ fn foo(n: usize) {
", ",
" "
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] #[test]
fn test_remove_dbg_with_brackets_and_braces() { 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] #[test]

View file

@ -26,8 +26,7 @@ pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
}; };
let target = mut_token.text_range(); let target = mut_token.text_range();
acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |edit| { acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |builder| {
edit.set_cursor(delete_from); builder.delete(TextRange::new(delete_from, delete_to));
edit.delete(TextRange::new(delete_from, delete_to));
}) })
} }