rust/crates/assists/src/handlers/replace_unwrap_with_match.rs

189 lines
4.9 KiB
Rust
Raw Normal View History

2020-03-26 04:16:10 -05:00
use std::iter;
2020-08-12 11:26:51 -05:00
use syntax::{
2020-05-09 07:40:11 -05:00
ast::{
self,
edit::{AstNodeEdit, IndentLevel},
make,
},
2020-03-26 04:16:10 -05:00
AstNode,
};
2020-05-20 17:01:08 -05:00
use crate::{
utils::{render_snippet, Cursor},
2020-06-28 17:36:05 -05:00
AssistContext, AssistId, AssistKind, Assists,
2020-05-20 17:01:08 -05:00
};
use ide_db::ty_filter::TryEnum;
2020-03-26 04:16:10 -05:00
// Assist: replace_unwrap_with_match
//
// Replaces `unwrap` a `match` expression. Works for Result and Option.
//
// ```
// enum Result<T, E> { Ok(T), Err(E) }
// fn main() {
// let x: Result<i32, i32> = Result::Ok(92);
2021-01-06 14:15:48 -06:00
// let y = x.$0unwrap();
2020-03-26 04:16:10 -05:00
// }
// ```
// ->
// ```
// enum Result<T, E> { Ok(T), Err(E) }
// fn main() {
// let x: Result<i32, i32> = Result::Ok(92);
// let y = match x {
// Ok(a) => a,
2020-05-20 17:01:08 -05:00
// $0_ => unreachable!(),
2020-03-26 04:16:10 -05:00
// };
// }
// ```
pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2020-03-26 04:16:10 -05:00
let method_call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
let name = method_call.name_ref()?;
if name.text() != "unwrap" {
return None;
}
2020-08-21 12:12:38 -05:00
let caller = method_call.receiver()?;
2020-03-26 04:16:10 -05:00
let ty = ctx.sema.type_of_expr(&caller)?;
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
let target = method_call.syntax().text_range();
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-02 16:48:35 -05:00
AssistId("replace_unwrap_with_match", AssistKind::RefactorRewrite),
2020-06-28 17:36:05 -05:00
"Replace unwrap with match",
target,
|builder| {
let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
2020-08-05 12:29:24 -05:00
let it = make::ident_pat(make::name("a")).into();
2020-06-28 17:36:05 -05:00
let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
2020-03-26 04:16:10 -05:00
2020-06-28 17:36:05 -05:00
let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a")));
let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path));
2020-03-26 04:16:10 -05:00
2020-06-28 17:36:05 -05:00
let unreachable_call = make::expr_unreachable();
let err_arm =
2020-08-05 12:29:24 -05:00
make::match_arm(iter::once(make::wildcard_pat().into()), unreachable_call);
2020-03-26 04:16:10 -05:00
2020-06-28 17:36:05 -05:00
let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]);
let match_expr = make::expr_match(caller.clone(), match_arm_list)
.indent(IndentLevel::from_node(method_call.syntax()));
2020-03-26 04:16:10 -05:00
2020-06-28 17:36:05 -05:00
let range = method_call.syntax().text_range();
match ctx.config.snippet_cap {
Some(cap) => {
let err_arm = match_expr
.syntax()
.descendants()
.filter_map(ast::MatchArm::cast)
.last()
.unwrap();
let snippet =
render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax()));
builder.replace_snippet(cap, range, snippet)
}
None => builder.replace(range, match_expr.to_string()),
2020-05-20 17:01:08 -05:00
}
2020-06-28 17:36:05 -05:00
},
)
2020-03-26 04:16:10 -05:00
}
#[cfg(test)]
mod tests {
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_target};
2020-03-26 04:16:10 -05:00
2020-05-20 17:01:08 -05:00
use super::*;
2020-03-26 04:16:10 -05:00
#[test]
fn test_replace_result_unwrap_with_match() {
check_assist(
replace_unwrap_with_match,
r"
enum Result<T, E> { Ok(T), Err(E) }
fn i<T>(a: T) -> T { a }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
2021-01-06 14:15:48 -06:00
let y = i(x).$0unwrap();
2020-03-26 04:16:10 -05:00
}
",
r"
enum Result<T, E> { Ok(T), Err(E) }
fn i<T>(a: T) -> T { a }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
2020-05-20 17:01:08 -05:00
let y = match i(x) {
2020-03-26 04:16:10 -05:00
Ok(a) => a,
2020-05-20 17:01:08 -05:00
$0_ => unreachable!(),
2020-03-26 04:16:10 -05:00
};
}
",
)
}
#[test]
fn test_replace_option_unwrap_with_match() {
check_assist(
replace_unwrap_with_match,
r"
enum Option<T> { Some(T), None }
fn i<T>(a: T) -> T { a }
fn main() {
let x = Option::Some(92);
2021-01-06 14:15:48 -06:00
let y = i(x).$0unwrap();
2020-03-26 04:16:10 -05:00
}
",
r"
enum Option<T> { Some(T), None }
fn i<T>(a: T) -> T { a }
fn main() {
let x = Option::Some(92);
2020-05-20 17:01:08 -05:00
let y = match i(x) {
2020-03-26 04:16:10 -05:00
Some(a) => a,
2020-05-20 17:01:08 -05:00
$0_ => unreachable!(),
2020-03-26 04:16:10 -05:00
};
}
",
);
}
#[test]
fn test_replace_result_unwrap_with_match_chaining() {
check_assist(
replace_unwrap_with_match,
r"
enum Result<T, E> { Ok(T), Err(E) }
fn i<T>(a: T) -> T { a }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
2021-01-06 14:15:48 -06:00
let y = i(x).$0unwrap().count_zeroes();
2020-03-26 04:16:10 -05:00
}
",
r"
enum Result<T, E> { Ok(T), Err(E) }
fn i<T>(a: T) -> T { a }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
2020-05-20 17:01:08 -05:00
let y = match i(x) {
2020-03-26 04:16:10 -05:00
Ok(a) => a,
2020-05-20 17:01:08 -05:00
$0_ => unreachable!(),
2020-03-26 04:16:10 -05:00
}.count_zeroes();
}
",
)
}
#[test]
fn replace_unwrap_with_match_target() {
check_assist_target(
replace_unwrap_with_match,
r"
enum Option<T> { Some(T), None }
fn i<T>(a: T) -> T { a }
fn main() {
let x = Option::Some(92);
2021-01-06 14:15:48 -06:00
let y = i(x).$0unwrap();
2020-03-26 04:16:10 -05:00
}
",
r"i(x).unwrap()",
);
}
}