Skip to content

Commit

Permalink
Add getter/setter assists
Browse files Browse the repository at this point in the history
Finish implementing `generate_setter` assists

Make `generate_impl_text` util generic

generate getter methods

Fix getter / setter naming

It's now in-line with the Rust API naming guidelines: https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter

apply clippy

Improve examples
  • Loading branch information
yoshuawuyts committed Feb 9, 2021
1 parent 876c451 commit 7b3c89e
Show file tree
Hide file tree
Showing 9 changed files with 600 additions and 69 deletions.
17 changes: 4 additions & 13 deletions crates/assists/src/handlers/generate_enum_match_method.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use ast::Adt;
use stdx::{format_to, to_lower_snake_case};
use syntax::ast::VisibilityOwner;
use syntax::ast::{self, AstNode, NameOwner};
use test_utils::mark;

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

Expand Down Expand Up @@ -49,7 +50,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
// Return early if we've found an existing new fn
let impl_def = find_struct_impl(
&ctx,
&ast::Adt::Enum(parent_enum.clone()),
&Adt::Enum(parent_enum.clone()),
format!("is_{}", fn_name).as_str(),
)?;

Expand Down Expand Up @@ -82,7 +83,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
let start_offset = impl_def
.and_then(|impl_def| find_impl_block(impl_def, &mut buf))
.unwrap_or_else(|| {
buf = generate_impl_text(&parent_enum, &buf);
buf = generate_impl_text(&Adt::Enum(parent_enum.clone()), &buf);
parent_enum.syntax().text_range().end()
});

Expand All @@ -91,16 +92,6 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
)
}

// Generates the surrounding `impl Type { <code> }` including type and lifetime
// parameters
fn generate_impl_text(strukt: &ast::Enum, code: &str) -> String {
let mut buf = String::with_capacity(code.len());
buf.push_str("\n\nimpl ");
buf.push_str(strukt.name().unwrap().text());
format_to!(buf, " {{\n{}\n}}", code);
buf
}

#[cfg(test)]
mod tests {
use test_utils::mark;
Expand Down
156 changes: 156 additions & 0 deletions crates/assists/src/handlers/generate_getter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use ast::Adt;
use stdx::{format_to, to_lower_snake_case};
use syntax::ast::VisibilityOwner;
use syntax::ast::{self, AstNode, NameOwner};

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

// Assist: generate_getter
//
// Generate a getter method.
//
// ```
// struct Person {
// nam$0e: String,
// }
// ```
// ->
// ```
// struct Person {
// name: String,
// }
//
// impl Person {
// /// Get a reference to the person's name.
// fn name(&self) -> &String {
// &self.name
// }
// }
// ```
pub(crate) fn generate_getter(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()?;

let fn_name = to_lower_snake_case(&field_name.to_string());
let fn_name_spaced = fn_name.replace('_', " ");
let strukt_name_spaced = to_lower_snake_case(&strukt_name.to_string()).replace('_', " ");

// Return early if we've found an existing fn
let impl_def = find_struct_impl(&ctx, &Adt::Struct(strukt.clone()), fn_name.as_str())?;

let target = field.syntax().text_range();
acc.add(
AssistId("generate_getter", AssistKind::Generate),
"Generate a getter method",
target,
|builder| {
let mut buf = String::with_capacity(512);

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

let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
format_to!(
buf,
" /// Get a reference to the {}'s {}.
{}fn {}(&self) -> &{} {{
&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(impl_def, &mut buf))
.unwrap_or_else(|| {
buf = generate_impl_text(&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, ra_fixture)
}

#[test]
fn test_generate_getter_from_field() {
check_assist(
generate_getter,
r#"
struct Context<T: Clone> {
dat$0a: T,
}"#,
r#"
struct Context<T: Clone> {
data: T,
}
impl<T: Clone> Context<T> {
/// Get a reference to the context's data.
fn data(&self) -> &T {
&self.data
}
}"#,
);
}

#[test]
fn test_generate_getter_already_implemented() {
check_not_applicable(
r#"
struct Context<T: Clone> {
dat$0a: T,
}
impl<T: Clone> Context<T> {
fn data(&self) -> &T {
&self.data
}
}"#,
);
}

#[test]
fn test_generate_getter_from_field_with_visibility_marker() {
check_assist(
generate_getter,
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 reference to the context's data.
pub(crate) fn data(&self) -> &T {
&self.data
}
}"#,
);
}
}
157 changes: 157 additions & 0 deletions crates/assists/src/handlers/generate_getter_mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use ast::Adt;
use stdx::{format_to, to_lower_snake_case};
use syntax::ast::VisibilityOwner;
use syntax::ast::{self, AstNode, NameOwner};

use crate::{
utils::{find_impl_block, 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()?;

let fn_name = to_lower_snake_case(&field_name.to_string());
let fn_name_spaced = fn_name.replace('_', " ");
let strukt_name_spaced = to_lower_snake_case(&strukt_name.to_string()).replace('_', " ");

// Return early if we've found an existing fn
let impl_def =
find_struct_impl(&ctx, &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);

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(impl_def, &mut buf))
.unwrap_or_else(|| {
buf = generate_impl_text(&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
}
}"#,
);
}
}
Loading

0 comments on commit 7b3c89e

Please sign in to comment.