2022-03-31 22:19:45 -05:00
|
|
|
use ide_db::assists::GroupLabel;
|
2021-02-14 04:15:20 -06:00
|
|
|
use stdx::to_lower_snake_case;
|
2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::HasVisibility;
|
|
|
|
use syntax::ast::{self, AstNode, HasName};
|
2021-02-04 18:57:39 -06:00
|
|
|
|
2021-02-14 11:28:22 -06:00
|
|
|
use crate::{
|
2021-02-15 15:25:33 -06:00
|
|
|
utils::{add_method_to_adt, find_struct_impl},
|
2021-02-14 11:28:22 -06:00
|
|
|
AssistContext, AssistId, AssistKind, Assists,
|
|
|
|
};
|
2021-02-04 18:57:39 -06:00
|
|
|
|
2021-02-14 03:09:56 -06:00
|
|
|
// Assist: generate_enum_is_method
|
2021-02-04 18:57:39 -06:00
|
|
|
//
|
2022-04-01 15:22:49 -05:00
|
|
|
// Generate an `is_` method for this enum variant.
|
2021-02-04 18:57:39 -06:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Version {
|
|
|
|
// Undefined,
|
|
|
|
// Minor$0,
|
|
|
|
// Major,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Version {
|
|
|
|
// Undefined,
|
|
|
|
// Minor,
|
|
|
|
// Major,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl Version {
|
2021-02-05 08:35:24 -06:00
|
|
|
// /// Returns `true` if the version is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
// ///
|
|
|
|
// /// [`Minor`]: Version::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
// #[must_use]
|
2021-02-04 18:57:39 -06:00
|
|
|
// fn is_minor(&self) -> bool {
|
|
|
|
// matches!(self, Self::Minor)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-02-04 18:57:39 -06:00
|
|
|
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
|
|
|
|
let variant_name = variant.name()?;
|
2021-02-14 03:33:46 -06:00
|
|
|
let parent_enum = ast::Adt::Enum(variant.parent_enum());
|
2021-02-15 15:25:33 -06:00
|
|
|
let pattern_suffix = match variant.kind() {
|
|
|
|
ast::StructKind::Record(_) => " { .. }",
|
|
|
|
ast::StructKind::Tuple(_) => "(..)",
|
|
|
|
ast::StructKind::Unit => "",
|
|
|
|
};
|
2021-02-04 18:57:39 -06:00
|
|
|
|
2021-08-19 05:39:06 -05:00
|
|
|
let enum_name = parent_enum.name()?;
|
|
|
|
let enum_lowercase_name = to_lower_snake_case(&enum_name.to_string()).replace('_', " ");
|
2021-03-26 12:30:59 -05:00
|
|
|
let fn_name = format!("is_{}", &to_lower_snake_case(&variant_name.text()));
|
2021-02-04 18:57:39 -06:00
|
|
|
|
|
|
|
// Return early if we've found an existing new fn
|
2022-10-07 14:24:57 -05:00
|
|
|
let impl_def = find_struct_impl(ctx, &parent_enum, &[fn_name.clone()])?;
|
2021-02-04 18:57:39 -06:00
|
|
|
|
|
|
|
let target = variant.syntax().text_range();
|
2022-03-31 22:19:45 -05:00
|
|
|
acc.add_group(
|
2022-04-01 15:22:49 -05:00
|
|
|
&GroupLabel("Generate an `is_`,`as_`, or `try_into_` for this enum variant".to_owned()),
|
2021-02-14 03:09:56 -06:00
|
|
|
AssistId("generate_enum_is_method", AssistKind::Generate),
|
2022-04-01 15:22:49 -05:00
|
|
|
"Generate an `is_` method for this enum variant",
|
2021-02-04 18:57:39 -06:00
|
|
|
target,
|
|
|
|
|builder| {
|
2022-10-10 13:22:01 -05:00
|
|
|
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{v} "));
|
2021-02-14 04:15:20 -06:00
|
|
|
let method = format!(
|
2022-10-10 13:22:01 -05:00
|
|
|
" /// Returns `true` if the {enum_lowercase_name} is [`{variant_name}`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
2022-10-10 13:22:01 -05:00
|
|
|
/// [`{variant_name}`]: {enum_name}::{variant_name}
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2022-10-10 13:22:01 -05:00
|
|
|
{vis}fn {fn_name}(&self) -> bool {{
|
|
|
|
matches!(self, Self::{variant_name}{pattern_suffix})
|
2021-02-04 18:57:39 -06:00
|
|
|
}}",
|
|
|
|
);
|
|
|
|
|
2021-02-14 04:15:20 -06:00
|
|
|
add_method_to_adt(builder, &parent_enum, impl_def, &method);
|
2021-02-04 18:57:39 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_generate_enum_is_from_variant() {
|
2021-02-04 18:57:39 -06:00
|
|
|
check_assist(
|
2021-02-14 03:09:56 -06:00
|
|
|
generate_enum_is_method,
|
2021-02-04 18:57:39 -06:00
|
|
|
r#"
|
|
|
|
enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor$0,
|
|
|
|
Major,
|
|
|
|
}"#,
|
|
|
|
r#"enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor,
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
2021-02-05 08:35:24 -06:00
|
|
|
/// Returns `true` if the variant is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Minor`]: Variant::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-04 18:57:39 -06:00
|
|
|
fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor)
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_generate_enum_is_already_implemented() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_enum_is_method,
|
2021-02-04 18:57:39 -06:00
|
|
|
r#"
|
|
|
|
enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor$0,
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
|
|
|
fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor)
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_generate_enum_is_from_tuple_variant() {
|
2021-02-14 03:33:46 -06:00
|
|
|
check_assist(
|
|
|
|
generate_enum_is_method,
|
2021-02-04 18:57:39 -06:00
|
|
|
r#"
|
|
|
|
enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor(u32)$0,
|
|
|
|
Major,
|
2021-02-14 03:33:46 -06:00
|
|
|
}"#,
|
|
|
|
r#"enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor(u32),
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
|
|
|
/// Returns `true` if the variant is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Minor`]: Variant::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-14 03:33:46 -06:00
|
|
|
fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor(..))
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_generate_enum_is_from_record_variant() {
|
2021-02-14 03:33:46 -06:00
|
|
|
check_assist(
|
|
|
|
generate_enum_is_method,
|
|
|
|
r#"
|
|
|
|
enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor { foo: i32 }$0,
|
|
|
|
Major,
|
|
|
|
}"#,
|
|
|
|
r#"enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor { foo: i32 },
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
|
|
|
/// Returns `true` if the variant is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Minor`]: Variant::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-14 03:33:46 -06:00
|
|
|
fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor { .. })
|
|
|
|
}
|
2021-02-04 18:57:39 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_generate_enum_is_from_variant_with_one_variant() {
|
2021-02-04 18:57:39 -06:00
|
|
|
check_assist(
|
2021-02-14 03:09:56 -06:00
|
|
|
generate_enum_is_method,
|
2021-02-04 18:57:39 -06:00
|
|
|
r#"enum Variant { Undefi$0ned }"#,
|
|
|
|
r#"
|
|
|
|
enum Variant { Undefined }
|
|
|
|
|
|
|
|
impl Variant {
|
2021-02-05 08:35:24 -06:00
|
|
|
/// Returns `true` if the variant is [`Undefined`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Undefined`]: Variant::Undefined
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-04 18:57:39 -06:00
|
|
|
fn is_undefined(&self) -> bool {
|
|
|
|
matches!(self, Self::Undefined)
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_generate_enum_is_from_variant_with_visibility_marker() {
|
2021-02-04 18:57:39 -06:00
|
|
|
check_assist(
|
2021-02-14 03:09:56 -06:00
|
|
|
generate_enum_is_method,
|
2021-02-04 18:57:39 -06:00
|
|
|
r#"
|
|
|
|
pub(crate) enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor$0,
|
|
|
|
Major,
|
|
|
|
}"#,
|
|
|
|
r#"pub(crate) enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor,
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
2021-02-05 08:35:24 -06:00
|
|
|
/// Returns `true` if the variant is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Minor`]: Variant::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-04 18:57:39 -06:00
|
|
|
pub(crate) fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor)
|
|
|
|
}
|
2021-02-12 04:48:43 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-14 04:06:42 -06:00
|
|
|
fn test_multiple_generate_enum_is_from_variant() {
|
2021-02-12 04:48:43 -06:00
|
|
|
check_assist(
|
2021-02-14 03:09:56 -06:00
|
|
|
generate_enum_is_method,
|
2021-02-12 04:48:43 -06:00
|
|
|
r#"
|
|
|
|
enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor,
|
|
|
|
Major$0,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
|
|
|
/// Returns `true` if the variant is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Minor`]: Variant::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-12 04:48:43 -06:00
|
|
|
fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor)
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"enum Variant {
|
|
|
|
Undefined,
|
|
|
|
Minor,
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
|
|
|
/// Returns `true` if the variant is [`Minor`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Minor`]: Variant::Minor
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-12 04:48:43 -06:00
|
|
|
fn is_minor(&self) -> bool {
|
|
|
|
matches!(self, Self::Minor)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the variant is [`Major`].
|
2021-08-19 05:39:06 -05:00
|
|
|
///
|
|
|
|
/// [`Major`]: Variant::Major
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-02-12 04:48:43 -06:00
|
|
|
fn is_major(&self) -> bool {
|
|
|
|
matches!(self, Self::Major)
|
|
|
|
}
|
2021-08-19 05:39:06 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_enum_is_variant_names() {
|
|
|
|
check_assist(
|
|
|
|
generate_enum_is_method,
|
|
|
|
r#"
|
|
|
|
enum GeneratorState {
|
|
|
|
Yielded,
|
|
|
|
Complete$0,
|
|
|
|
Major,
|
|
|
|
}"#,
|
|
|
|
r#"enum GeneratorState {
|
|
|
|
Yielded,
|
|
|
|
Complete,
|
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GeneratorState {
|
|
|
|
/// Returns `true` if the generator state is [`Complete`].
|
|
|
|
///
|
|
|
|
/// [`Complete`]: GeneratorState::Complete
|
2022-03-17 08:10:25 -05:00
|
|
|
#[must_use]
|
2021-08-19 05:39:06 -05:00
|
|
|
fn is_complete(&self) -> bool {
|
|
|
|
matches!(self, Self::Complete)
|
|
|
|
}
|
2021-02-04 18:57:39 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|