1 //! FIXME: write short doc here
5 use hir::{Adt, HasSource, Semantics};
6 use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner};
8 use crate::{Assist, AssistCtx, AssistId};
9 use ra_ide_db::RootDatabase;
11 // Assist: fill_match_arms
13 // Adds missing clauses to a `match` expression.
16 // enum Action { Move { distance: u32 }, Stop }
18 // fn handle(action: Action) {
26 // enum Action { Move { distance: u32 }, Stop }
28 // fn handle(action: Action) {
30 // Action::Move { distance } => (),
31 // Action::Stop => (),
35 pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
36 let match_expr = ctx.find_node_at_offset::<ast::MatchExpr>()?;
37 let match_arm_list = match_expr.match_arm_list()?;
39 // We already have some match arms, so we don't provide any assists.
40 // Unless if there is only one trivial match arm possibly created
41 // by match postfix complete. Trivial match arm is the catch all arm.
42 let mut existing_arms = match_arm_list.arms();
43 if let Some(arm) = existing_arms.next() {
44 if !is_trivial(&arm) || existing_arms.next().is_some() {
49 let expr = match_expr.expr()?;
50 let enum_def = resolve_enum_def(&ctx.sema, &expr)?;
51 let module = ctx.sema.scope(expr.syntax()).module()?;
53 let variants = enum_def.variants(ctx.db);
54 if variants.is_empty() {
60 ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| {
61 let indent_level = IndentLevel::from_node(match_arm_list.syntax());
66 .filter_map(|variant| build_pat(db, module, variant))
67 .map(|pat| make::match_arm(iter::once(pat), make::expr_unit()));
68 indent_level.increase_indent(make::match_arm_list(arms))
71 edit.target(match_expr.syntax().text_range());
72 edit.set_cursor(expr.syntax().text_range().start());
73 edit.replace_ast(match_arm_list, new_arm_list);
77 fn is_trivial(arm: &ast::MatchArm) -> bool {
79 Some(ast::Pat::PlaceholderPat(..)) => true,
84 fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> {
85 sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() {
86 Some(Adt::Enum(e)) => Some(e),
91 fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> {
92 let path = crate::ast_transform::path_to_ast(module.find_use_path(db, var.into())?);
94 // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
95 let pat: ast::Pat = match var.source(db).value.kind() {
96 ast::StructKind::Tuple(field_list) => {
98 iter::repeat(make::placeholder_pat().into()).take(field_list.fields().count());
99 make::tuple_struct_pat(path, pats).into()
101 ast::StructKind::Record(field_list) => {
102 let pats = field_list.fields().map(|f| make::bind_pat(f.name().unwrap()).into());
103 make::record_pat(path, pats).into()
105 ast::StructKind::Unit => make::path_pat(path),
113 use crate::helpers::{check_assist, check_assist_target};
115 use super::fill_match_arms;
118 fn fill_match_arms_empty_body() {
127 Es{ x: usize, y: usize }
141 Es{ x: usize, y: usize }
151 A::Es { x, y } => (),
159 fn test_fill_match_arm_refs() {
189 Es{ x: usize, y: usize }
199 Es{ x: usize, y: usize }
204 A::Es { x, y } => (),
212 fn fill_match_arms_target() {
227 fn fill_match_arms_trivial_arm() {
253 fn fill_match_arms_qualifies_path() {
257 mod foo { pub enum E { X, Y } }
267 mod foo { pub enum E { X, Y } }