aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/generate_getter_mut.rs
blob: bf0d99881fae931798af9819ff7cc6678cce2dc9 (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
use stdx::{format_to, to_lower_snake_case};
use syntax::ast::VisibilityOwner;
use syntax::ast::{self, AstNode, NameOwner};

use crate::{
    utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
    AssistContext, AssistId, AssistKind, Assists,
};

// Assist: generate_getter_mut
//
// Generate a mut getter method.
//
// ```
// struct Person {
//     nam$0e: String,
// }
// ```
// ->
// ```
// struct Person {
//     name: String,
// }
//
// impl Person {
//     /// Get a mutable reference to the person's name.
//     fn name_mut(&mut self) -> &mut String {
//         &mut self.name
//     }
// }
// ```
pub(crate) fn generate_getter_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
    let field = ctx.find_node_at_offset::<ast::RecordField>()?;

    let strukt_name = strukt.name()?;
    let field_name = field.name()?;
    let field_ty = field.ty()?;

    // Return early if we've found an existing fn
    let fn_name = to_lower_snake_case(&field_name.to_string());
    let impl_def = find_struct_impl(
        &ctx,
        &ast::Adt::Struct(strukt.clone()),
        format!("{}_mut", fn_name).as_str(),
    )?;

    let target = field.syntax().text_range();
    acc.add(
        AssistId("generate_getter_mut", AssistKind::Generate),
        "Generate a mut getter method",
        target,
        |builder| {
            let mut buf = String::with_capacity(512);
            let fn_name_spaced = fn_name.replace('_', " ");
            let strukt_name_spaced =
                to_lower_snake_case(&strukt_name.to_string()).replace('_', " ");

            if impl_def.is_some() {
                buf.push('\n');
            }

            let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
            format_to!(
                buf,
                "    /// Get a mutable reference to the {}'s {}.
    {}fn {}_mut(&mut self) -> &mut {} {{
        &mut self.{}
    }}",
                strukt_name_spaced,
                fn_name_spaced,
                vis,
                fn_name,
                field_ty,
                fn_name,
            );

            let start_offset = impl_def
                .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
                .unwrap_or_else(|| {
                    buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf);
                    strukt.syntax().text_range().end()
                });

            builder.insert(start_offset, buf);
        },
    )
}

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

    use super::*;

    fn check_not_applicable(ra_fixture: &str) {
        check_assist_not_applicable(generate_getter_mut, ra_fixture)
    }

    #[test]
    fn test_generate_getter_mut_from_field() {
        check_assist(
            generate_getter_mut,
            r#"
struct Context<T: Clone> {
    dat$0a: T,
}"#,
            r#"
struct Context<T: Clone> {
    data: T,
}

impl<T: Clone> Context<T> {
    /// Get a mutable reference to the context's data.
    fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }
}"#,
        );
    }

    #[test]
    fn test_generate_getter_mut_already_implemented() {
        check_not_applicable(
            r#"
struct Context<T: Clone> {
    dat$0a: T,
}

impl<T: Clone> Context<T> {
    fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }
}"#,
        );
    }

    #[test]
    fn test_generate_getter_mut_from_field_with_visibility_marker() {
        check_assist(
            generate_getter_mut,
            r#"
pub(crate) struct Context<T: Clone> {
    dat$0a: T,
}"#,
            r#"
pub(crate) struct Context<T: Clone> {
    data: T,
}

impl<T: Clone> Context<T> {
    /// Get a mutable reference to the context's data.
    pub(crate) fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }
}"#,
        );
    }

    #[test]
    fn test_multiple_generate_getter_mut() {
        check_assist(
            generate_getter_mut,
            r#"
struct Context<T: Clone> {
    data: T,
    cou$0nt: usize,
}

impl<T: Clone> Context<T> {
    /// Get a mutable reference to the context's data.
    fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }
}"#,
            r#"
struct Context<T: Clone> {
    data: T,
    count: usize,
}

impl<T: Clone> Context<T> {
    /// Get a mutable reference to the context's data.
    fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }

    /// Get a mutable reference to the context's count.
    fn count_mut(&mut self) -> &mut usize {
        &mut self.count
    }
}"#,
        );
    }
}