rust/crates/ra_assists/src/fill_match_arms.rs

284 lines
6.9 KiB
Rust
Raw Normal View History

2019-06-25 15:14:54 -05:00
use itertools::Itertools;
use std::fmt::Write;
2019-02-03 12:26:35 -06:00
use hir::{db::HirDatabase, AdtDef, FieldSource, HasSource};
2019-02-03 12:26:35 -06:00
use ra_syntax::ast::{self, AstNode};
use crate::{Assist, AssistCtx, AssistId};
2019-02-03 12:26:35 -06:00
2019-06-23 23:05:50 -05:00
fn is_trivial_arm(arm: &ast::MatchArm) -> bool {
2019-06-25 15:14:54 -05:00
fn single_pattern(arm: &ast::MatchArm) -> Option<ast::PatKind> {
let (pat,) = arm.pats().collect_tuple()?;
Some(pat.kind())
}
match single_pattern(arm) {
Some(ast::PatKind::PlaceholderPat(..)) => true,
_ => false,
2019-06-23 23:05:50 -05:00
}
}
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
2019-02-03 12:26:35 -06:00
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
// We already have some match arms, so we don't provide any assists.
2019-06-23 23:05:50 -05:00
// Unless if there is only one trivial match arm possibly created
// by match postfix complete. Trivial match arm is the catch all arm.
2019-02-03 12:26:35 -06:00
match match_expr.match_arm_list() {
2019-06-23 23:05:50 -05:00
Some(arm_list) => {
2019-06-25 15:14:54 -05:00
let mut arm_iter = arm_list.arms();
let first = arm_iter.next();
match first {
// If there arm list is empty or there is only one trivial arm, then proceed.
Some(arm) if is_trivial_arm(arm) => {
if arm_iter.next() != None {
return None;
}
2019-06-23 23:05:50 -05:00
}
2019-06-25 15:14:54 -05:00
None => {}
2019-06-23 23:05:50 -05:00
2019-06-25 15:14:54 -05:00
_ => {
2019-06-23 23:05:50 -05:00
return None;
}
}
2019-02-03 12:26:35 -06:00
}
_ => {}
}
let expr = match_expr.expr()?;
let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, expr.syntax(), None);
2019-04-10 03:15:55 -05:00
let match_expr_ty = analyzer.type_of(ctx.db, expr)?;
let enum_def = analyzer.autoderef(ctx.db, match_expr_ty).find_map(|ty| match ty.as_adt() {
Some((AdtDef::Enum(e), _)) => Some(e),
_ => None,
})?;
2019-02-03 12:26:35 -06:00
let enum_name = enum_def.name(ctx.db)?;
let db = ctx.db;
2019-02-24 04:53:35 -06:00
ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| {
2019-02-03 12:26:35 -06:00
let mut buf = format!("match {} {{\n", expr.syntax().text().to_string());
let variants = enum_def.variants(db);
for variant in variants {
let name = match variant.name(db) {
Some(it) => it,
None => continue,
};
write!(&mut buf, " {}::{}", enum_name, name.to_string()).unwrap();
let pat = variant
.fields(db)
.into_iter()
.map(|field| {
let name = field.name(db).to_string();
2019-06-11 09:43:36 -05:00
let src = field.source(db);
match src.ast {
2019-02-03 12:26:35 -06:00
FieldSource::Named(_) => name,
FieldSource::Pos(_) => "_".to_string(),
}
})
.collect::<Vec<_>>();
match pat.first().map(|s| s.as_str()) {
Some("_") => write!(&mut buf, "({})", pat.join(", ")).unwrap(),
Some(_) => write!(&mut buf, "{{{}}}", pat.join(", ")).unwrap(),
None => (),
};
buf.push_str(" => (),\n");
}
buf.push_str("}");
2019-02-08 17:34:05 -06:00
edit.target(match_expr.syntax().range());
2019-02-03 12:26:35 -06:00
edit.set_cursor(expr.syntax().range().start());
edit.replace_node_and_indent(match_expr.syntax(), buf);
});
ctx.build()
2019-02-03 12:26:35 -06:00
}
#[cfg(test)]
mod tests {
2019-02-08 17:34:05 -06:00
use crate::helpers::{check_assist, check_assist_target};
2019-02-03 12:26:35 -06:00
use super::fill_match_arms;
#[test]
fn fill_match_arms_empty_body() {
check_assist(
fill_match_arms,
r#"
enum A {
As,
Bs,
Cs(String),
Ds(String, String),
Es{x: usize, y: usize}
}
fn main() {
let a = A::As;
match a<|> {}
}
"#,
r#"
enum A {
As,
Bs,
Cs(String),
Ds(String, String),
Es{x: usize, y: usize}
}
fn main() {
let a = A::As;
match <|>a {
A::As => (),
A::Bs => (),
A::Cs(_) => (),
A::Ds(_, _) => (),
A::Es{x, y} => (),
}
}
"#,
);
}
#[test]
fn test_fill_match_arm_refs() {
check_assist(
fill_match_arms,
r#"
enum A {
As,
}
fn foo(a: &A) {
match a<|> {
}
}
"#,
r#"
enum A {
As,
}
fn foo(a: &A) {
match <|>a {
A::As => (),
}
}
"#,
);
check_assist(
fill_match_arms,
r#"
enum A {
Es{x: usize, y: usize}
}
fn foo(a: &mut A) {
match a<|> {
}
}
"#,
r#"
enum A {
Es{x: usize, y: usize}
}
fn foo(a: &mut A) {
match <|>a {
A::Es{x, y} => (),
}
}
"#,
);
check_assist(
fill_match_arms,
r#"
enum E { X, Y}
fn main() {
match &E::X<|>
}
"#,
r#"
enum E { X, Y}
fn main() {
match <|>&E::X {
E::X => (),
E::Y => (),
}
}
"#,
);
}
2019-02-03 12:26:35 -06:00
#[test]
fn fill_match_arms_no_body() {
check_assist(
fill_match_arms,
r#"
enum E { X, Y}
fn main() {
match E::X<|>
}
"#,
r#"
enum E { X, Y}
fn main() {
match <|>E::X {
E::X => (),
E::Y => (),
}
}
"#,
);
}
2019-02-08 17:34:05 -06:00
#[test]
fn fill_match_arms_target() {
check_assist_target(
fill_match_arms,
r#"
enum E { X, Y}
fn main() {
match E::X<|> {}
}
"#,
"match E::X {}",
);
}
2019-06-23 23:05:50 -05:00
#[test]
fn fill_match_arms_trivial_arm() {
check_assist(
fill_match_arms,
r#"
enum E { X, Y }
fn main() {
match E::X {
<|>_ => {},
}
}
"#,
r#"
enum E { X, Y }
fn main() {
match <|>E::X {
E::X => (),
E::Y => (),
}
}
"#,
);
}
2019-02-03 12:26:35 -06:00
}