1 use stdx::{format_to, to_lower_snake_case};
2 use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
5 utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
6 AssistContext, AssistId, AssistKind, Assists, GroupLabel,
9 // Assist: generate_getter
11 // Generate a getter method.
25 // /// Get a reference to the person's name.
26 // fn name(&self) -> &String {
31 pub(crate) fn generate_getter(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
32 generate_getter_impl(acc, ctx, false)
35 // Assist: generate_getter_mut
37 // Generate a mut getter method.
51 // /// Get a mutable reference to the person's name.
52 // fn name_mut(&mut self) -> &mut String {
57 pub(crate) fn generate_getter_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
58 generate_getter_impl(acc, ctx, true)
61 pub(crate) fn generate_getter_impl(
66 let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
67 let field = ctx.find_node_at_offset::<ast::RecordField>()?;
69 let strukt_name = strukt.name()?;
70 let field_name = field.name()?;
71 let field_ty = field.ty()?;
73 // Return early if we've found an existing fn
74 let mut fn_name = to_lower_snake_case(&field_name.to_string());
76 format_to!(fn_name, "_mut");
78 let impl_def = find_struct_impl(&ctx, &ast::Adt::Struct(strukt.clone()), fn_name.as_str())?;
80 let (id, label) = if mutable {
81 ("generate_getter_mut", "Generate a mut getter method")
83 ("generate_getter", "Generate a getter method")
85 let target = field.syntax().text_range();
87 &GroupLabel("Generate getter/setter".to_owned()),
88 AssistId(id, AssistKind::Generate),
92 let mut buf = String::with_capacity(512);
94 if impl_def.is_some() {
98 let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
101 " /// Get a {}reference to the {}'s {}.
102 {}fn {}(&{mut_}self) -> &{mut_}{} {{
105 mutable.then(|| "mutable ").unwrap_or_default(),
106 to_lower_snake_case(&strukt_name.to_string()).replace('_', " "),
107 fn_name.trim_end_matches("_mut").replace('_', " "),
112 mut_ = mutable.then(|| "mut ").unwrap_or_default(),
115 let start_offset = impl_def
116 .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
118 buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf);
119 strukt.syntax().text_range().end()
122 builder.insert(start_offset, buf);
129 use crate::tests::{check_assist, check_assist_not_applicable};
133 fn check_not_applicable(ra_fixture: &str) {
134 check_assist_not_applicable(generate_getter, ra_fixture)
138 fn test_generate_getter_from_field() {
142 struct Context<T: Clone> {
146 struct Context<T: Clone> {
150 impl<T: Clone> Context<T> {
151 /// Get a reference to the context's data.
152 fn data(&self) -> &T {
160 fn test_generate_getter_already_implemented() {
161 check_not_applicable(
163 struct Context<T: Clone> {
167 impl<T: Clone> Context<T> {
168 fn data(&self) -> &T {
176 fn test_generate_getter_from_field_with_visibility_marker() {
180 pub(crate) struct Context<T: Clone> {
184 pub(crate) struct Context<T: Clone> {
188 impl<T: Clone> Context<T> {
189 /// Get a reference to the context's data.
190 pub(crate) fn data(&self) -> &T {
198 fn test_multiple_generate_getter() {
202 struct Context<T: Clone> {
207 impl<T: Clone> Context<T> {
208 /// Get a reference to the context's data.
209 fn data(&self) -> &T {
214 struct Context<T: Clone> {
219 impl<T: Clone> Context<T> {
220 /// Get a reference to the context's data.
221 fn data(&self) -> &T {
225 /// Get a reference to the context's count.
226 fn count(&self) -> &usize {
236 use crate::tests::{check_assist, check_assist_not_applicable};
240 fn check_not_applicable(ra_fixture: &str) {
241 check_assist_not_applicable(generate_getter_mut, ra_fixture)
245 fn test_generate_getter_mut_from_field() {
249 struct Context<T: Clone> {
253 struct Context<T: Clone> {
257 impl<T: Clone> Context<T> {
258 /// Get a mutable reference to the context's data.
259 fn data_mut(&mut self) -> &mut T {
267 fn test_generate_getter_mut_already_implemented() {
268 check_not_applicable(
270 struct Context<T: Clone> {
274 impl<T: Clone> Context<T> {
275 fn data_mut(&mut self) -> &mut T {
283 fn test_generate_getter_mut_from_field_with_visibility_marker() {
287 pub(crate) struct Context<T: Clone> {
291 pub(crate) struct Context<T: Clone> {
295 impl<T: Clone> Context<T> {
296 /// Get a mutable reference to the context's data.
297 pub(crate) fn data_mut(&mut self) -> &mut T {
305 fn test_multiple_generate_getter_mut() {
309 struct Context<T: Clone> {
314 impl<T: Clone> Context<T> {
315 /// Get a mutable reference to the context's data.
316 fn data_mut(&mut self) -> &mut T {
321 struct Context<T: Clone> {
326 impl<T: Clone> Context<T> {
327 /// Get a mutable reference to the context's data.
328 fn data_mut(&mut self) -> &mut T {
332 /// Get a mutable reference to the context's count.
333 fn count_mut(&mut self) -> &mut usize {