2021-10-20 14:35:35 -05:00
|
|
|
use ide_db::helpers::FamousDefs;
|
2021-02-09 05:30:13 -06:00
|
|
|
use stdx::{format_to, to_lower_snake_case};
|
2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::{self, AstNode, HasName, HasVisibility};
|
2021-02-09 05:30:13 -06:00
|
|
|
|
|
|
|
use crate::{
|
2021-09-29 07:04:32 -05:00
|
|
|
utils::{convert_reference_type, find_impl_block_end, find_struct_impl, generate_impl_text},
|
2021-02-13 12:58:58 -06:00
|
|
|
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
|
2021-02-09 05:30:13 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Assist: generate_getter
|
|
|
|
//
|
|
|
|
// Generate a getter method.
|
|
|
|
//
|
|
|
|
// ```
|
2021-09-29 07:04:32 -05:00
|
|
|
// # //- minicore: as_ref
|
|
|
|
// # pub struct String;
|
|
|
|
// # impl AsRef<str> for String {
|
|
|
|
// # fn as_ref(&self) -> &str {
|
|
|
|
// # ""
|
|
|
|
// # }
|
|
|
|
// # }
|
|
|
|
// #
|
2021-02-09 05:30:13 -06:00
|
|
|
// struct Person {
|
|
|
|
// nam$0e: String,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2021-09-29 07:04:32 -05:00
|
|
|
// # pub struct String;
|
|
|
|
// # impl AsRef<str> for String {
|
|
|
|
// # fn as_ref(&self) -> &str {
|
|
|
|
// # ""
|
|
|
|
// # }
|
|
|
|
// # }
|
|
|
|
// #
|
2021-02-09 05:30:13 -06:00
|
|
|
// struct Person {
|
|
|
|
// name: String,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl Person {
|
|
|
|
// /// Get a reference to the person's name.
|
2021-05-23 16:05:14 -05:00
|
|
|
// fn $0name(&self) -> &str {
|
2021-09-29 07:04:32 -05:00
|
|
|
// self.name.as_ref()
|
2021-02-09 05:30:13 -06:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn generate_getter(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-05-23 15:13:35 -05:00
|
|
|
generate_getter_impl(acc, ctx, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-05-23 15:22:38 -05:00
|
|
|
// fn $0name_mut(&mut self) -> &mut String {
|
2021-05-23 15:13:35 -05:00
|
|
|
// &mut self.name
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn generate_getter_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|
|
|
generate_getter_impl(acc, ctx, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn generate_getter_impl(
|
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
|
|
|
mutable: bool,
|
|
|
|
) -> Option<()> {
|
2021-02-09 05:30:13 -06:00
|
|
|
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
|
2021-05-23 15:13:35 -05:00
|
|
|
let mut fn_name = to_lower_snake_case(&field_name.to_string());
|
|
|
|
if mutable {
|
|
|
|
format_to!(fn_name, "_mut");
|
|
|
|
}
|
2021-06-12 22:54:16 -05:00
|
|
|
let impl_def = find_struct_impl(ctx, &ast::Adt::Struct(strukt.clone()), fn_name.as_str())?;
|
2021-02-09 05:30:13 -06:00
|
|
|
|
2021-05-23 15:13:35 -05:00
|
|
|
let (id, label) = if mutable {
|
|
|
|
("generate_getter_mut", "Generate a mut getter method")
|
|
|
|
} else {
|
|
|
|
("generate_getter", "Generate a getter method")
|
|
|
|
};
|
2021-02-09 05:30:13 -06:00
|
|
|
let target = field.syntax().text_range();
|
2021-02-13 12:58:58 -06:00
|
|
|
acc.add_group(
|
|
|
|
&GroupLabel("Generate getter/setter".to_owned()),
|
2021-05-23 15:13:35 -05:00
|
|
|
AssistId(id, AssistKind::Generate),
|
|
|
|
label,
|
2021-02-09 05:30:13 -06:00
|
|
|
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));
|
2021-05-23 16:05:14 -05:00
|
|
|
let (ty, body) = if mutable {
|
2021-10-20 14:35:35 -05:00
|
|
|
(format!("&mut {}", field_ty), format!("&mut self.{}", field_name))
|
2021-05-23 16:05:14 -05:00
|
|
|
} else {
|
2021-10-20 14:35:35 -05:00
|
|
|
let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(field_ty.syntax()).krate());
|
2021-09-29 07:04:32 -05:00
|
|
|
ctx.sema
|
|
|
|
.resolve_type(&field_ty)
|
2021-10-20 14:35:35 -05:00
|
|
|
.and_then(|ty| convert_reference_type(ty, ctx.db(), famous_defs))
|
2021-09-29 07:04:32 -05:00
|
|
|
.map(|conversion| {
|
|
|
|
cov_mark::hit!(convert_reference_type);
|
|
|
|
(
|
|
|
|
conversion.convert_type(ctx.db()),
|
|
|
|
conversion.getter(field_name.to_string()),
|
|
|
|
)
|
|
|
|
})
|
2021-05-23 16:05:14 -05:00
|
|
|
.unwrap_or_else(|| (format!("&{}", field_ty), format!("&self.{}", field_name)))
|
|
|
|
};
|
|
|
|
|
2021-02-09 05:30:13 -06:00
|
|
|
format_to!(
|
|
|
|
buf,
|
2021-05-23 15:13:35 -05:00
|
|
|
" /// Get a {}reference to the {}'s {}.
|
2021-05-23 16:05:14 -05:00
|
|
|
{}fn {}(&{}self) -> {} {{
|
|
|
|
{}
|
2021-02-09 05:30:13 -06:00
|
|
|
}}",
|
2021-05-23 15:13:35 -05:00
|
|
|
mutable.then(|| "mutable ").unwrap_or_default(),
|
|
|
|
to_lower_snake_case(&strukt_name.to_string()).replace('_', " "),
|
|
|
|
fn_name.trim_end_matches("_mut").replace('_', " "),
|
2021-02-09 05:30:13 -06:00
|
|
|
vis,
|
|
|
|
fn_name,
|
2021-05-23 16:05:14 -05:00
|
|
|
mutable.then(|| "mut ").unwrap_or_default(),
|
|
|
|
ty,
|
|
|
|
body,
|
2021-02-09 05:30:13 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
let start_offset = impl_def
|
2021-02-12 04:48:43 -06:00
|
|
|
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
|
2021-02-09 05:30:13 -06:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf);
|
|
|
|
strukt.syntax().text_range().end()
|
|
|
|
});
|
|
|
|
|
2021-05-23 15:22:38 -05:00
|
|
|
match ctx.config.snippet_cap {
|
|
|
|
Some(cap) => {
|
|
|
|
builder.insert_snippet(cap, start_offset, buf.replacen("fn ", "fn $0", 1))
|
|
|
|
}
|
|
|
|
None => builder.insert(start_offset, buf),
|
|
|
|
}
|
2021-02-09 05:30:13 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_getter_from_field() {
|
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
dat$0a: Data,
|
|
|
|
}
|
|
|
|
"#,
|
2021-02-09 05:30:13 -06:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
data: Data,
|
2021-02-09 05:30:13 -06:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
2021-02-09 05:30:13 -06:00
|
|
|
/// Get a reference to the context's data.
|
2021-05-23 15:22:38 -05:00
|
|
|
fn $0data(&self) -> &Data {
|
2021-02-09 05:30:13 -06:00
|
|
|
&self.data
|
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-02-09 05:30:13 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
2021-05-23 15:15:54 -05:00
|
|
|
generate_getter_mut,
|
2021-02-09 05:30:13 -06:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
dat$0a: Data,
|
|
|
|
}
|
|
|
|
"#,
|
2021-02-09 05:30:13 -06:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
data: Data,
|
2021-02-09 05:30:13 -06:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
2021-05-23 15:15:54 -05:00
|
|
|
/// Get a mutable reference to the context's data.
|
2021-05-23 15:22:38 -05:00
|
|
|
fn $0data_mut(&mut self) -> &mut Data {
|
2021-05-23 15:15:54 -05:00
|
|
|
&mut self.data
|
2021-02-09 05:30:13 -06:00
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-02-12 04:48:43 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-23 15:15:54 -05:00
|
|
|
fn test_generate_getter_already_implemented() {
|
|
|
|
check_assist_not_applicable(
|
2021-02-12 04:48:43 -06:00
|
|
|
generate_getter,
|
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
dat$0a: Data,
|
2021-02-12 04:48:43 -06:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
|
|
|
fn data(&self) -> &Data {
|
2021-02-12 04:48:43 -06:00
|
|
|
&self.data
|
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-02-09 05:30:13 -06:00
|
|
|
);
|
2021-05-23 15:13:35 -05:00
|
|
|
|
2021-05-23 15:15:54 -05:00
|
|
|
check_assist_not_applicable(
|
2021-05-23 15:13:35 -05:00
|
|
|
generate_getter_mut,
|
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
dat$0a: Data,
|
2021-05-23 15:13:35 -05:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
|
|
|
fn data_mut(&mut self) -> &mut Data {
|
2021-05-23 15:13:35 -05:00
|
|
|
&mut self.data
|
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-05-23 15:13:35 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-23 15:15:54 -05:00
|
|
|
fn test_generate_getter_from_field_with_visibility_marker() {
|
2021-05-23 15:13:35 -05:00
|
|
|
check_assist(
|
2021-05-23 15:15:54 -05:00
|
|
|
generate_getter,
|
2021-05-23 15:13:35 -05:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
pub(crate) struct Context {
|
|
|
|
dat$0a: Data,
|
|
|
|
}
|
|
|
|
"#,
|
2021-05-23 15:13:35 -05:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
pub(crate) struct Context {
|
|
|
|
data: Data,
|
2021-05-23 15:13:35 -05:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
2021-05-23 15:15:54 -05:00
|
|
|
/// Get a reference to the context's data.
|
2021-05-23 15:22:38 -05:00
|
|
|
pub(crate) fn $0data(&self) -> &Data {
|
2021-05-23 15:15:54 -05:00
|
|
|
&self.data
|
2021-05-23 15:13:35 -05:00
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-05-23 15:13:35 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-23 15:15:54 -05:00
|
|
|
fn test_multiple_generate_getter() {
|
2021-05-23 15:13:35 -05:00
|
|
|
check_assist(
|
2021-05-23 15:15:54 -05:00
|
|
|
generate_getter,
|
2021-05-23 15:13:35 -05:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
data: Data,
|
2021-05-23 15:13:35 -05:00
|
|
|
cou$0nt: usize,
|
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
2021-05-23 15:15:54 -05:00
|
|
|
/// Get a reference to the context's data.
|
2021-05-23 15:19:00 -05:00
|
|
|
fn data(&self) -> &Data {
|
2021-05-23 15:15:54 -05:00
|
|
|
&self.data
|
2021-05-23 15:13:35 -05:00
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-05-23 15:13:35 -05:00
|
|
|
r#"
|
2021-05-23 15:19:00 -05:00
|
|
|
struct Context {
|
|
|
|
data: Data,
|
2021-05-23 15:13:35 -05:00
|
|
|
count: usize,
|
|
|
|
}
|
|
|
|
|
2021-05-23 15:19:00 -05:00
|
|
|
impl Context {
|
2021-05-23 15:15:54 -05:00
|
|
|
/// Get a reference to the context's data.
|
2021-05-23 15:19:00 -05:00
|
|
|
fn data(&self) -> &Data {
|
2021-05-23 15:15:54 -05:00
|
|
|
&self.data
|
2021-05-23 15:13:35 -05:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:15:54 -05:00
|
|
|
/// Get a reference to the context's count.
|
2021-05-23 15:22:38 -05:00
|
|
|
fn $0count(&self) -> &usize {
|
2021-05-23 15:15:54 -05:00
|
|
|
&self.count
|
2021-05-23 15:13:35 -05:00
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
}
|
2021-05-23 16:05:14 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-29 07:04:32 -05:00
|
|
|
fn test_not_a_special_case() {
|
|
|
|
cov_mark::check_count!(convert_reference_type, 0);
|
|
|
|
// Fake string which doesn't implement AsRef<str>
|
2021-05-23 16:05:14 -05:00
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
pub struct String;
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: $0String }
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
pub struct String;
|
|
|
|
|
|
|
|
struct S { foo: String }
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
/// Get a reference to the s's foo.
|
|
|
|
fn $0foo(&self) -> &String {
|
|
|
|
&self.foo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_convert_reference_type() {
|
|
|
|
cov_mark::check_count!(convert_reference_type, 6);
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
|
|
|
//- minicore: copy
|
|
|
|
struct S { foo: $0bool }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S { foo: bool }
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
/// Get a reference to the s's foo.
|
|
|
|
fn $0foo(&self) -> bool {
|
|
|
|
self.foo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
// AsRef<str>
|
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
|
|
|
//- minicore: as_ref
|
|
|
|
pub struct String;
|
|
|
|
impl AsRef<str> for String {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S { foo: $0String }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
pub struct String;
|
|
|
|
impl AsRef<str> for String {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: String }
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
/// Get a reference to the s's foo.
|
|
|
|
fn $0foo(&self) -> &str {
|
2021-09-29 07:04:32 -05:00
|
|
|
self.foo.as_ref()
|
2021-05-23 16:05:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2021-09-29 07:04:32 -05:00
|
|
|
|
|
|
|
// AsRef<T>
|
2021-05-23 16:05:14 -05:00
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
//- minicore: as_ref
|
|
|
|
struct Sweets;
|
|
|
|
|
|
|
|
pub struct Box<T>(T);
|
|
|
|
impl<T> AsRef<T> for Box<T> {
|
|
|
|
fn as_ref(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: $0Box<Sweets> }
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
struct Sweets;
|
|
|
|
|
|
|
|
pub struct Box<T>(T);
|
|
|
|
impl<T> AsRef<T> for Box<T> {
|
|
|
|
fn as_ref(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: Box<Sweets> }
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
/// Get a reference to the s's foo.
|
|
|
|
fn $0foo(&self) -> &Sweets {
|
|
|
|
self.foo.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2021-09-29 07:04:32 -05:00
|
|
|
|
|
|
|
// AsRef<[T]>
|
2021-05-23 16:05:14 -05:00
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
//- minicore: as_ref
|
|
|
|
pub struct Vec<T>;
|
|
|
|
impl<T> AsRef<[T]> for Vec<T> {
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: $0Vec<()> }
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
pub struct Vec<T>;
|
|
|
|
impl<T> AsRef<[T]> for Vec<T> {
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: Vec<()> }
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
/// Get a reference to the s's foo.
|
|
|
|
fn $0foo(&self) -> &[()] {
|
2021-09-29 07:04:32 -05:00
|
|
|
self.foo.as_ref()
|
2021-05-23 16:05:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2021-09-29 07:04:32 -05:00
|
|
|
|
|
|
|
// Option
|
2021-05-23 16:05:14 -05:00
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
//- minicore: option
|
|
|
|
struct Failure;
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: $0Option<Failure> }
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-09-29 07:04:32 -05:00
|
|
|
struct Failure;
|
|
|
|
|
2021-05-23 16:05:14 -05:00
|
|
|
struct S { foo: Option<Failure> }
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
/// Get a reference to the s's foo.
|
|
|
|
fn $0foo(&self) -> Option<&Failure> {
|
|
|
|
self.foo.as_ref()
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 07:04:32 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Result
|
|
|
|
check_assist(
|
|
|
|
generate_getter,
|
|
|
|
r#"
|
|
|
|
//- minicore: result
|
|
|
|
struct Context {
|
|
|
|
dat$0a: Result<bool, i32>,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Context {
|
|
|
|
data: Result<bool, i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
/// Get a reference to the context's data.
|
|
|
|
fn $0data(&self) -> Result<&bool, &i32> {
|
|
|
|
self.data.as_ref()
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 15:19:00 -05:00
|
|
|
"#,
|
2021-05-23 15:13:35 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|