Skip to content

Commit a70beea

Browse files
committed
Trigger only when cursor is on else
1 parent 59cdb31 commit a70beea

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

crates/ide-assists/src/handlers/convert_let_else_to_match.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use syntax::ast::{edit::AstNodeEdit, AstNode, HasName, LetStmt, Pat};
2+
use syntax::T;
23

34
use crate::{AssistContext, AssistId, AssistKind, Assists};
45

@@ -95,7 +96,7 @@ fn binders_to_str(binders: &[(String, bool)], addmut: bool) -> String {
9596
//
9697
// ```
9798
// fn main() {
98-
// let Ok(mut x) = f() else {$0 return };
99+
// let Ok(mut x) = f() else$0 { return };
99100
// }
100101
// ```
101102
// ->
@@ -108,7 +109,9 @@ fn binders_to_str(binders: &[(String, bool)], addmut: bool) -> String {
108109
// }
109110
// ```
110111
pub(crate) fn convert_let_else_to_match(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
111-
let let_stmt: LetStmt = ctx.find_node_at_offset()?;
112+
// should focus on else token to trigger
113+
let else_token = ctx.find_token_syntax_at_offset(T![else])?;
114+
let let_stmt = LetStmt::cast(else_token.parent()?.parent()?)?;
112115
let let_else_block = let_stmt.let_else()?.block_expr()?;
113116
let let_init = let_stmt.initializer()?;
114117
if let_stmt.ty().is_some() {
@@ -177,18 +180,30 @@ mod tests {
177180
convert_let_else_to_match,
178181
r#"
179182
fn main() {
180-
let 1: u32 = v.iter().sum() else {$0 return };
183+
let 1: u32 = v.iter().sum() else$0 { return };
181184
}"#,
182185
);
183186
}
184187

188+
#[test]
189+
fn convert_let_else_to_match_on_else() {
190+
check_assist_not_applicable(
191+
convert_let_else_to_match,
192+
r#"
193+
fn main() {
194+
let Ok(x) = f() else {$0 return };
195+
}
196+
"#,
197+
);
198+
}
199+
185200
#[test]
186201
fn convert_let_else_to_match_no_macropat() {
187202
check_assist_not_applicable(
188203
convert_let_else_to_match,
189204
r#"
190205
fn main() {
191-
let m!() = g() else {$0 return };
206+
let m!() = g() else$0 { return };
192207
}
193208
"#,
194209
);
@@ -200,7 +215,7 @@ fn main() {
200215
convert_let_else_to_match,
201216
r"
202217
fn main() {
203-
let Ok(x) = f() else {$0 continue };
218+
let Ok(x) = f() else$0 { continue };
204219
}",
205220
"let Ok(x) = f() else { continue };",
206221
);
@@ -212,7 +227,7 @@ fn main() {
212227
convert_let_else_to_match,
213228
r"
214229
fn main() {
215-
let Ok(x) = f() else {$0 continue };
230+
let Ok(x) = f() else$0 { continue };
216231
}",
217232
r"
218233
fn main() {
@@ -230,7 +245,7 @@ fn main() {
230245
convert_let_else_to_match,
231246
r"
232247
fn main() {
233-
let Ok(mut x) = f() else {$0 continue };
248+
let Ok(mut x) = f() el$0se { continue };
234249
}",
235250
r"
236251
fn main() {
@@ -248,7 +263,7 @@ fn main() {
248263
convert_let_else_to_match,
249264
r#"
250265
fn main() {
251-
let ControlFlow::Break((x, "tag", y, ..)) = f() else {$0 g(); return };
266+
let ControlFlow::Break((x, "tag", y, ..)) = f() else$0 { g(); return };
252267
}"#,
253268
r#"
254269
fn main() {
@@ -266,7 +281,7 @@ fn main() {
266281
convert_let_else_to_match,
267282
r#"
268283
fn main() {
269-
let [one, 1001, other] = f() else {$0 break };
284+
let [one, 1001, other] = f() else$0 { break };
270285
}"#,
271286
r#"
272287
fn main() {
@@ -284,7 +299,7 @@ fn main() {
284299
convert_let_else_to_match,
285300
r#"
286301
fn main() {
287-
let [Struct { inner: Some(it) }, 1001, other] = f() else {$0 break };
302+
let [Struct { inner: Some(it) }, 1001, other] = f() else$0 { break };
288303
}"#,
289304
r#"
290305
fn main() {
@@ -302,7 +317,7 @@ fn main() {
302317
convert_let_else_to_match,
303318
r#"
304319
fn main() {
305-
let [Struct { inner }, 1001, other] = f() else {$0 break };
320+
let [Struct { inner }, 1001, other] = f() else$0 { break };
306321
}"#,
307322
r#"
308323
fn main() {
@@ -320,7 +335,7 @@ fn main() {
320335
convert_let_else_to_match,
321336
r#"
322337
fn main() {
323-
let (8 | 9) = f() else {$0 panic!() };
338+
let (8 | 9) = f() else$0 { panic!() };
324339
}"#,
325340
r#"
326341
fn main() {
@@ -338,7 +353,7 @@ fn main() {
338353
convert_let_else_to_match,
339354
r#"
340355
fn main() {
341-
let 1.. = f() else {$0 return };
356+
let 1.. = f() e$0lse { return };
342357
}"#,
343358
r#"
344359
fn main() {
@@ -356,7 +371,7 @@ fn main() {
356371
convert_let_else_to_match,
357372
r#"
358373
fn main() {
359-
let Ok(&mut x) = f(&mut 0) else {$0 return };
374+
let Ok(&mut x) = f(&mut 0) else$0 { return };
360375
}"#,
361376
r#"
362377
fn main() {
@@ -374,7 +389,7 @@ fn main() {
374389
convert_let_else_to_match,
375390
r#"
376391
fn main() {
377-
let Ok(ref mut x) = f() else {$0 return };
392+
let Ok(ref mut x) = f() else$0 { return };
378393
}"#,
379394
r#"
380395
fn main() {
@@ -392,7 +407,7 @@ fn main() {
392407
convert_let_else_to_match,
393408
r#"
394409
fn main() {
395-
let out @ Ok(ins) = f() else {$0 return };
410+
let out @ Ok(ins) = f() else$0 { return };
396411
}"#,
397412
r#"
398413
fn main() {
@@ -411,7 +426,7 @@ fn main() {
411426
r#"
412427
fn main() {
413428
let v = vec![1, 2, 3];
414-
let &[mut x, y, ..] = &v.iter().collect::<Vec<_>>()[..]$0 else { return };
429+
let &[mut x, y, ..] = &v.iter().collect::<Vec<_>>()[..] else$0 { return };
415430
}"#,
416431
r#"
417432
fn main() {

crates/ide-assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ fn doctest_convert_let_else_to_match() {
370370
"convert_let_else_to_match",
371371
r#####"
372372
fn main() {
373-
let Ok(mut x) = f() else {$0 return };
373+
let Ok(mut x) = f() else$0 { return };
374374
}
375375
"#####,
376376
r#####"

0 commit comments

Comments
 (0)