aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs
blob: 03806724a3ace8b32bfe16af0090cc50db3e56ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use ra_syntax::{
    ast::{self, AstNode, NameOwner},
    TextSize,
};
use stdx::format_to;

use crate::{Assist, AssistCtx, AssistId};
use ra_ide_db::RootDatabase;

// Assist add_from_impl_for_enum
//
// Adds a From impl for an enum variant with one tuple field
//
// ```
// enum A { <|>One(u32) }
// ```
// ->
// ```
// enum A { One(u32) }
//
// impl From<u32> for A {
//     fn from(v: u32) -> Self {
//         A::One(v)
//     }
// }
// ```
pub(crate) fn add_from_impl_for_enum(ctx: AssistCtx) -> Option<Assist> {
    let variant = ctx.find_node_at_offset::<ast::EnumVariant>()?;
    let variant_name = variant.name()?;
    let enum_name = variant.parent_enum().name()?;
    let field_list = match variant.kind() {
        ast::StructKind::Tuple(field_list) => field_list,
        _ => return None,
    };
    if field_list.fields().count() != 1 {
        return None;
    }
    let field_type = field_list.fields().next()?.type_ref()?;
    let path = match field_type {
        ast::TypeRef::PathType(p) => p,
        _ => return None,
    };

    if already_has_from_impl(ctx.sema, &variant) {
        return None;
    }

    ctx.add_assist(
        AssistId("add_from_impl_for_enum"),
        "Add From impl for this enum variant",
        |edit| {
            let start_offset = variant.parent_enum().syntax().text_range().end();
            let mut buf = String::new();
            format_to!(
                buf,
                r#"

impl From<{0}> for {1} {{
    fn from(v: {0}) -> Self {{
        {1}::{2}(v)
    }}
}}"#,
                path.syntax(),
                enum_name,
                variant_name
            );
            edit.insert(start_offset, buf);
            edit.set_cursor(start_offset + TextSize::of("\n\n"));
        },
    )
}

fn already_has_from_impl(
    sema: &'_ hir::Semantics<'_, RootDatabase>,
    variant: &ast::EnumVariant,
) -> bool {
    let scope = sema.scope(&variant.syntax());

    let from_path = ast::make::path_from_text("From");
    let from_hir_path = match hir::Path::from_ast(from_path) {
        Some(p) => p,
        None => return false,
    };
    let from_trait = match scope.resolve_hir_path(&from_hir_path) {
        Some(hir::PathResolution::Def(hir::ModuleDef::Trait(t))) => t,
        _ => return false,
    };

    let e: hir::Enum = match sema.to_def(&variant.parent_enum()) {
        Some(e) => e,
        None => return false,
    };
    let e_ty = e.ty(sema.db);

    let hir_enum_var: hir::EnumVariant = match sema.to_def(variant) {
        Some(ev) => ev,
        None => return false,
    };
    let var_ty = hir_enum_var.fields(sema.db)[0].signature_ty(sema.db);

    e_ty.impls_trait(sema.db, from_trait, &[var_ty])
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::helpers::{check_assist, check_assist_not_applicable};

    #[test]
    fn test_add_from_impl_for_enum() {
        check_assist(
            add_from_impl_for_enum,
            "enum A { <|>One(u32) }",
            r#"enum A { One(u32) }

<|>impl From<u32> for A {
    fn from(v: u32) -> Self {
        A::One(v)
    }
}"#,
        );
    }

    #[test]
    fn test_add_from_impl_for_enum_complicated_path() {
        check_assist(
            add_from_impl_for_enum,
            "enum A { <|>One(foo::bar::baz::Boo) }",
            r#"enum A { One(foo::bar::baz::Boo) }

<|>impl From<foo::bar::baz::Boo> for A {
    fn from(v: foo::bar::baz::Boo) -> Self {
        A::One(v)
    }
}"#,
        );
    }

    #[test]
    fn test_add_from_impl_no_element() {
        check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One }");
    }

    #[test]
    fn test_add_from_impl_more_than_one_element_in_tuple() {
        check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One(u32, String) }");
    }

    #[test]
    fn test_add_from_impl_struct_variant() {
        check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One { x: u32 } }");
    }

    #[test]
    fn test_add_from_impl_already_exists() {
        check_assist_not_applicable(
            add_from_impl_for_enum,
            r#"enum A { <|>One(u32), }

impl From<u32> for A {
    fn from(v: u32) -> Self {
        A::One(v)
    }
}

pub trait From<T> {
    fn from(T) -> Self;
}"#,
        );
    }

    #[test]
    fn test_add_from_impl_different_variant_impl_exists() {
        check_assist(
            add_from_impl_for_enum,
            r#"enum A { <|>One(u32), Two(String), }

impl From<String> for A {
    fn from(v: String) -> Self {
        A::Two(v)
    }
}

pub trait From<T> {
    fn from(T) -> Self;
}"#,
            r#"enum A { One(u32), Two(String), }

<|>impl From<u32> for A {
    fn from(v: u32) -> Self {
        A::One(v)
    }
}

impl From<String> for A {
    fn from(v: String) -> Self {
        A::Two(v)
    }
}

pub trait From<T> {
    fn from(T) -> Self;
}"#,
        );
    }
}