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
|
use std::fmt::Write;
use hir::{AdtDef, Ty, source_binder};
use hir::db::HirDatabase;
use ra_syntax::ast::{self, AstNode};
use crate::{AssistCtx, Assist, AssistId};
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()?;
// If we already have existing struct fields, don't provide the assist.
if named_field_list.fields().count() > 0 {
return None;
}
let function =
source_binder::function_from_child_node(ctx.db, ctx.frange.file_id, struct_lit.syntax())?;
let infer_result = function.infer(ctx.db);
let source_map = function.body_source_map(ctx.db);
let node_expr = source_map.node_expr(struct_lit.into())?;
let struct_lit_ty = infer_result[node_expr].clone();
let struct_def = match struct_lit_ty {
Ty::Adt { def_id: AdtDef::Struct(s), .. } => s,
_ => return None,
};
let db = ctx.db;
ctx.add_action(AssistId("fill_struct_fields"), "fill struct fields", |edit| {
let mut buf = String::from("{\n");
let struct_fields = struct_def.fields(db);
for field in struct_fields {
let field_name = field.name(db).to_string();
write!(&mut buf, " {}: (),\n", field_name).unwrap();
}
buf.push_str("}");
edit.target(struct_lit.syntax().range());
edit.set_cursor(struct_lit.syntax().range().start());
edit.replace_node_and_indent(named_field_list.syntax(), buf);
});
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,
r: &'a str,
}
fn main() {
let s = S<|> {}
}
"#,
r#"
struct S<'a, D> {
a: u32,
b: String,
c: (i32, i32),
d: D,
r: &'a str,
}
fn main() {
let s = <|>S {
a: (),
b: (),
c: (),
d: (),
r: (),
}
}
"#,
);
}
#[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,
r: &'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: (),
}
}
}
"#,
);
}
}
|