Skip to content

Commit 8300132

Browse files
committed
More snippets
1 parent 4677cea commit 8300132

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

crates/ra_assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
6868
.indent(IndentLevel::from_node(if_expr.syntax()))
6969
};
7070

71-
edit.set_cursor(if_expr.syntax().text_range().start());
7271
edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr);
7372
})
7473
}
@@ -83,7 +82,7 @@ mod tests {
8382
fn test_replace_if_let_with_match_unwraps_simple_expressions() {
8483
check_assist(
8584
replace_if_let_with_match,
86-
"
85+
r#"
8786
impl VariantData {
8887
pub fn is_struct(&self) -> bool {
8988
if <|>let VariantData::Struct(..) = *self {
@@ -92,24 +91,24 @@ impl VariantData {
9291
false
9392
}
9493
}
95-
} ",
96-
"
94+
} "#,
95+
r#"
9796
impl VariantData {
9897
pub fn is_struct(&self) -> bool {
99-
<|>match *self {
98+
match *self {
10099
VariantData::Struct(..) => true,
101100
_ => false,
102101
}
103102
}
104-
} ",
103+
} "#,
105104
)
106105
}
107106

108107
#[test]
109108
fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
110109
check_assist(
111110
replace_if_let_with_match,
112-
"
111+
r#"
113112
fn foo() {
114113
if <|>let VariantData::Struct(..) = a {
115114
bar(
@@ -118,26 +117,26 @@ fn foo() {
118117
} else {
119118
false
120119
}
121-
} ",
122-
"
120+
} "#,
121+
r#"
123122
fn foo() {
124-
<|>match a {
123+
match a {
125124
VariantData::Struct(..) => {
126125
bar(
127126
123
128127
)
129128
}
130129
_ => false,
131130
}
132-
} ",
131+
} "#,
133132
)
134133
}
135134

136135
#[test]
137136
fn replace_if_let_with_match_target() {
138137
check_assist_target(
139138
replace_if_let_with_match,
140-
"
139+
r#"
141140
impl VariantData {
142141
pub fn is_struct(&self) -> bool {
143142
if <|>let VariantData::Struct(..) = *self {
@@ -146,7 +145,7 @@ impl VariantData {
146145
false
147146
}
148147
}
149-
} ",
148+
} "#,
150149
"if let VariantData::Struct(..) = *self {
151150
true
152151
} else {
@@ -176,7 +175,7 @@ enum Option<T> { Some(T), None }
176175
use Option::*;
177176
178177
fn foo(x: Option<i32>) {
179-
<|>match x {
178+
match x {
180179
Some(x) => println!("{}", x),
181180
None => println!("none"),
182181
}
@@ -206,7 +205,7 @@ enum Result<T, E> { Ok(T), Err(E) }
206205
use Result::*;
207206
208207
fn foo(x: Result<i32, ()>) {
209-
<|>match x {
208+
match x {
210209
Ok(x) => println!("{}", x),
211210
Err(_) => println!("none"),
212211
}

crates/ra_assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
5858
let stmt = make::expr_stmt(if_);
5959

6060
let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap();
61-
let target_offset =
62-
let_stmt.syntax().text_range().start() + placeholder.syntax().text_range().start();
6361
let stmt = stmt.replace_descendant(placeholder.into(), original_pat);
6462

6563
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
66-
edit.set_cursor(target_offset);
6764
})
6865
}
6966

@@ -88,7 +85,7 @@ fn main() {
8885
enum E<T> { X(T), Y(T) }
8986
9087
fn main() {
91-
if let <|>x = E::X(92) {
88+
if let x = E::X(92) {
9289
}
9390
}
9491
",

crates/ra_assists/src/handlers/split_import.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
2626
if new_tree == use_tree {
2727
return None;
2828
}
29-
let cursor = ctx.offset();
3029

3130
let target = colon_colon.text_range();
3231
acc.add(AssistId("split_import"), "Split import", target, |edit| {
3332
edit.replace_ast(use_tree, new_tree);
34-
edit.set_cursor(cursor);
3533
})
3634
}
3735

@@ -46,7 +44,7 @@ mod tests {
4644
check_assist(
4745
split_import,
4846
"use crate::<|>db::RootDatabase;",
49-
"use crate::<|>{db::RootDatabase};",
47+
"use crate::{db::RootDatabase};",
5048
)
5149
}
5250

@@ -55,7 +53,7 @@ mod tests {
5553
check_assist(
5654
split_import,
5755
"use crate:<|>:db::{RootDatabase, FileSymbol}",
58-
"use crate:<|>:{db::{RootDatabase, FileSymbol}}",
56+
"use crate::{db::{RootDatabase, FileSymbol}}",
5957
)
6058
}
6159

0 commit comments

Comments
 (0)