aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/fill_struct_fields.rs
blob: 54b70e17dd54c38c3fd5059470680ceba2a385f8 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use hir::{AdtDef, db::HirDatabase};

use ra_syntax::ast::{self, AstNode};

use crate::{AssistCtx, Assist, AssistId, ast_editor::{AstEditor, AstBuilder}};

pub(crate) fn fill_struct_fields(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
    let struct_lit = ctx.node_at_offset::<ast::StructLit>()?;
    let named_field_list = struct_lit.named_field_list()?;

    // Collect all fields from struct definition
    let mut fields = {
        let analyzer =
            hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, struct_lit.syntax(), None);
        let struct_lit_ty = analyzer.type_of(ctx.db, struct_lit.into())?;
        let struct_def = match struct_lit_ty.as_adt() {
            Some((AdtDef::Struct(s), _)) => s,
            _ => return None,
        };
        struct_def.fields(ctx.db)
    };

    // Filter out existing fields
    for ast_field in named_field_list.fields() {
        let name_from_ast = ast_field.name_ref()?.text().to_string();
        fields.retain(|field| field.name(ctx.db).to_string() != name_from_ast);
    }
    if fields.is_empty() {
        return None;
    }

    let db = ctx.db;
    ctx.add_action(AssistId("fill_struct_fields"), "fill struct fields", |edit| {
        let mut ast_editor = AstEditor::new(named_field_list);
        if named_field_list.fields().count() == 0 && fields.len() > 2 {
            ast_editor.make_multiline();
        };

        for field in fields {
            let field = AstBuilder::<ast::NamedField>::from_pieces(
                &AstBuilder::<ast::NameRef>::new(&field.name(db).to_string()),
                Some(&AstBuilder::<ast::Expr>::unit()),
            );
            ast_editor.append_field(&field);
        }

        edit.target(struct_lit.syntax().range());
        edit.set_cursor(struct_lit.syntax().range().start());

        ast_editor.into_text_edit(edit.text_edit_builder());
    });
    ctx.build()
}

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

    use super::fill_struct_fields;

    #[test]
    fn fill_struct_fields_empty_body() {
        check_assist(
            fill_struct_fields,
            r#"
            struct S<'a, D> {
                a: u32,
                b: String,
                c: (i32, i32),
                d: D,
                e: &'a str,
            }

            fn main() {
                let s = S<|> {}
            }
            "#,
            r#"
            struct S<'a, D> {
                a: u32,
                b: String,
                c: (i32, i32),
                d: D,
                e: &'a str,
            }

            fn main() {
                let s = <|>S {
                    a: (),
                    b: (),
                    c: (),
                    d: (),
                    e: (),
                }
            }
            "#,
        );
    }

    #[test]
    fn fill_struct_fields_target() {
        check_assist_target(
            fill_struct_fields,
            r#"
            struct S<'a, D> {
                a: u32,
                b: String,
                c: (i32, i32),
                d: D,
                e: &'a str,
            }

            fn main() {
                let s = S<|> {}
            }
            "#,
            "S {}",
        );
    }

    #[test]
    fn fill_struct_fields_preserve_self() {
        check_assist(
            fill_struct_fields,
            r#"
            struct Foo {
                foo: u8,
                bar: String,
                baz: i128,
            }

            impl Foo {
                pub fn new() -> Self {
                    Self <|>{}
                }
            }
            "#,
            r#"
            struct Foo {
                foo: u8,
                bar: String,
                baz: i128,
            }

            impl Foo {
                pub fn new() -> Self {
                    <|>Self {
                        foo: (),
                        bar: (),
                        baz: (),
                    }
                }
            }
            "#,
        );
    }

    #[test]
    fn fill_struct_fields_partial() {
        check_assist(
            fill_struct_fields,
            r#"
            struct S<'a, D> {
                a: u32,
                b: String,
                c: (i32, i32),
                d: D,
                e: &'a str,
            }

            fn main() {
                let s = S {
                    c: (1, 2),
                    e: "foo",<|>
                }
            }
            "#,
            r#"
            struct S<'a, D> {
                a: u32,
                b: String,
                c: (i32, i32),
                d: D,
                e: &'a str,
            }

            fn main() {
                let s = <|>S {
                    c: (1, 2),
                    e: "foo",
                    a: (),
                    b: (),
                    d: (),
                }
            }
            "#,
        );
    }

    #[test]
    fn fill_struct_short() {
        check_assist(
            fill_struct_fields,
            r#"
            struct S {
                foo: u32,
                bar: String,
            }

            fn main() {
                let s = S {<|> };
            }
            "#,
            r#"
            struct S {
                foo: u32,
                bar: String,
            }

            fn main() {
                let s = <|>S { foo: (), bar: () };
            }
            "#,
        );
    }
}