More snippets

This commit is contained in:
Aleksey Kladov 2020-05-20 23:50:29 +02:00
parent 4677cea719
commit 8300132ed0
3 changed files with 17 additions and 23 deletions

View file

@ -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::<ast::Expr>(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<T> { Some(T), None }
use Option::*;
fn foo(x: Option<i32>) {
<|>match x {
match x {
Some(x) => println!("{}", x),
None => println!("none"),
}
@ -206,7 +205,7 @@ enum Result<T, E> { Ok(T), Err(E) }
use Result::*;
fn foo(x: Result<i32, ()>) {
<|>match x {
match x {
Ok(x) => println!("{}", x),
Err(_) => println!("none"),
}

View file

@ -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<T> { X(T), Y(T) }
fn main() {
if let <|>x = E::X(92) {
if let x = E::X(92) {
}
}
",

View file

@ -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}}",
)
}