Skip to content

Commit 4ac0abd

Browse files
committed
Snippetify unwrap -> match
1 parent 8300132 commit 4ac0abd

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

crates/ra_assists/src/handlers/replace_unwrap_with_match.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use ra_syntax::{
99
AstNode,
1010
};
1111

12-
use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
12+
use crate::{
13+
utils::{render_snippet, Cursor, TryEnum},
14+
AssistContext, AssistId, Assists,
15+
};
1316

1417
// Assist: replace_unwrap_with_match
1518
//
@@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
2932
// let x: Result<i32, i32> = Result::Ok(92);
3033
// let y = match x {
3134
// Ok(a) => a,
32-
// _ => unreachable!(),
35+
// $0_ => unreachable!(),
3336
// };
3437
// }
3538
// ```
@@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
4346
let ty = ctx.sema.type_of_expr(&caller)?;
4447
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
4548
let target = method_call.syntax().text_range();
46-
acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| {
49+
acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |builder| {
4750
let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
4851
let it = make::bind_pat(make::name("a")).into();
4952
let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
@@ -58,16 +61,30 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
5861
let match_expr = make::expr_match(caller.clone(), match_arm_list)
5962
.indent(IndentLevel::from_node(method_call.syntax()));
6063

61-
edit.set_cursor(caller.syntax().text_range().start());
62-
edit.replace_ast::<ast::Expr>(method_call.into(), match_expr);
64+
let range = method_call.syntax().text_range();
65+
match ctx.config.snippet_cap {
66+
Some(cap) => {
67+
let err_arm = match_expr
68+
.syntax()
69+
.descendants()
70+
.filter_map(ast::MatchArm::cast)
71+
.last()
72+
.unwrap();
73+
let snippet =
74+
render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax()));
75+
builder.replace_snippet(cap, range, snippet)
76+
}
77+
None => builder.replace(range, match_expr.to_string()),
78+
}
6379
})
6480
}
6581

6682
#[cfg(test)]
6783
mod tests {
68-
use super::*;
6984
use crate::tests::{check_assist, check_assist_target};
7085

86+
use super::*;
87+
7188
#[test]
7289
fn test_replace_result_unwrap_with_match() {
7390
check_assist(
@@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) }
85102
fn i<T>(a: T) -> T { a }
86103
fn main() {
87104
let x: Result<i32, i32> = Result::Ok(92);
88-
let y = <|>match i(x) {
105+
let y = match i(x) {
89106
Ok(a) => a,
90-
_ => unreachable!(),
107+
$0_ => unreachable!(),
91108
};
92109
}
93110
",
@@ -111,9 +128,9 @@ enum Option<T> { Some(T), None }
111128
fn i<T>(a: T) -> T { a }
112129
fn main() {
113130
let x = Option::Some(92);
114-
let y = <|>match i(x) {
131+
let y = match i(x) {
115132
Some(a) => a,
116-
_ => unreachable!(),
133+
$0_ => unreachable!(),
117134
};
118135
}
119136
",
@@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) }
137154
fn i<T>(a: T) -> T { a }
138155
fn main() {
139156
let x: Result<i32, i32> = Result::Ok(92);
140-
let y = <|>match i(x) {
157+
let y = match i(x) {
141158
Ok(a) => a,
142-
_ => unreachable!(),
159+
$0_ => unreachable!(),
143160
}.count_zeroes();
144161
}
145162
",

crates/ra_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ fn main() {
764764
let x: Result<i32, i32> = Result::Ok(92);
765765
let y = match x {
766766
Ok(a) => a,
767-
_ => unreachable!(),
767+
$0_ => unreachable!(),
768768
};
769769
}
770770
"#####,

docs/user/assists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ fn main() {
733733
let x: Result<i32, i32> = Result::Ok(92);
734734
let y = match x {
735735
Ok(a) => a,
736-
_ => unreachable!(),
736+
$0_ => unreachable!(),
737737
};
738738
}
739739
```

0 commit comments

Comments
 (0)