2022-03-06 12:01:30 -06:00
|
|
|
use ide_db::{famous_defs::FamousDefs, RootDatabase};
|
2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::{self, AstNode, HasName};
|
2020-12-13 15:00:44 -06:00
|
|
|
|
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
|
|
|
// Assist: generate_default_from_enum_variant
|
|
|
|
//
|
|
|
|
// Adds a Default impl for an enum using a variant.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Version {
|
|
|
|
// Undefined,
|
2021-01-06 14:15:48 -06:00
|
|
|
// Minor$0,
|
2020-12-13 15:00:44 -06:00
|
|
|
// Major,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Version {
|
|
|
|
// Undefined,
|
|
|
|
// Minor,
|
|
|
|
// Major,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl Default for Version {
|
|
|
|
// fn default() -> Self {
|
|
|
|
// Self::Minor
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn generate_default_from_enum_variant(
|
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
|
|
|
) -> Option<()> {
|
|
|
|
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
|
|
|
|
let variant_name = variant.name()?;
|
|
|
|
let enum_name = variant.parent_enum().name()?;
|
|
|
|
if !matches!(variant.kind(), ast::StructKind::Unit) {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_gen_default_on_non_unit_variant_not_implemented);
|
2020-12-13 15:00:44 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing_default_impl(&ctx.sema, &variant).is_some() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_gen_default_impl_already_exists);
|
2020-12-13 15:00:44 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let target = variant.syntax().text_range();
|
|
|
|
acc.add(
|
|
|
|
AssistId("generate_default_from_enum_variant", AssistKind::Generate),
|
|
|
|
"Generate `Default` impl from this enum variant",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
let start_offset = variant.parent_enum().syntax().text_range().end();
|
|
|
|
let buf = format!(
|
|
|
|
r#"
|
|
|
|
|
|
|
|
impl Default for {0} {{
|
|
|
|
fn default() -> Self {{
|
|
|
|
Self::{1}
|
|
|
|
}}
|
|
|
|
}}"#,
|
|
|
|
enum_name, variant_name
|
|
|
|
);
|
|
|
|
edit.insert(start_offset, buf);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn existing_default_impl(
|
|
|
|
sema: &'_ hir::Semantics<'_, RootDatabase>,
|
|
|
|
variant: &ast::Variant,
|
|
|
|
) -> Option<()> {
|
|
|
|
let variant = sema.to_def(variant)?;
|
|
|
|
let enum_ = variant.parent_enum(sema.db);
|
|
|
|
let krate = enum_.module(sema.db).krate();
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
let default_trait = FamousDefs(sema, krate).core_default_Default()?;
|
2020-12-13 15:00:44 -06:00
|
|
|
let enum_type = enum_.ty(sema.db);
|
|
|
|
|
|
|
|
if enum_type.impls_trait(sema.db, default_trait, &[]) {
|
|
|
|
Some(())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_default_from_variant() {
|
|
|
|
check_assist(
|
|
|
|
generate_default_from_enum_variant,
|
2020-12-15 14:35:41 -06:00
|
|
|
r#"
|
2021-06-17 12:49:49 -05:00
|
|
|
//- minicore: default
|
2020-12-15 14:35:41 -06:00
|
|
|
enum Variant {
|
2020-12-15 02:46:02 -06:00
|
|
|
Undefined,
|
2021-01-06 14:15:48 -06:00
|
|
|
Minor$0,
|
2020-12-15 02:46:02 -06:00
|
|
|
Major,
|
2021-06-17 12:49:49 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
enum Variant {
|
2020-12-15 02:46:02 -06:00
|
|
|
Undefined,
|
|
|
|
Minor,
|
|
|
|
Major,
|
|
|
|
}
|
2020-12-13 15:00:44 -06:00
|
|
|
|
|
|
|
impl Default for Variant {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Minor
|
|
|
|
}
|
2021-06-17 12:49:49 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-13 15:00:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_default_already_implemented() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_gen_default_impl_already_exists);
|
2021-06-17 12:49:49 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_default_from_enum_variant,
|
2020-12-15 14:35:41 -06:00
|
|
|
r#"
|
2021-06-17 12:49:49 -05:00
|
|
|
//- minicore: default
|
2020-12-15 14:35:41 -06:00
|
|
|
enum Variant {
|
2020-12-15 02:46:02 -06:00
|
|
|
Undefined,
|
2021-01-06 14:15:48 -06:00
|
|
|
Minor$0,
|
2020-12-15 02:46:02 -06:00
|
|
|
Major,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Variant {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Minor
|
|
|
|
}
|
2021-06-17 12:49:49 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-13 15:00:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_from_impl_no_element() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_gen_default_on_non_unit_variant_not_implemented);
|
2021-06-17 12:49:49 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_default_from_enum_variant,
|
2020-12-15 14:35:41 -06:00
|
|
|
r#"
|
2021-06-17 12:49:49 -05:00
|
|
|
//- minicore: default
|
2020-12-15 14:35:41 -06:00
|
|
|
enum Variant {
|
2020-12-15 02:46:02 -06:00
|
|
|
Undefined,
|
2021-01-06 14:15:48 -06:00
|
|
|
Minor(u32)$0,
|
2020-12-15 02:46:02 -06:00
|
|
|
Major,
|
2021-06-17 12:49:49 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-13 15:00:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_default_from_variant_with_one_variant() {
|
|
|
|
check_assist(
|
|
|
|
generate_default_from_enum_variant,
|
2021-06-17 12:49:49 -05:00
|
|
|
r#"
|
|
|
|
//- minicore: default
|
|
|
|
enum Variant { Undefi$0ned }
|
|
|
|
"#,
|
2020-12-15 14:35:41 -06:00
|
|
|
r#"
|
|
|
|
enum Variant { Undefined }
|
2020-12-13 15:00:44 -06:00
|
|
|
|
|
|
|
impl Default for Variant {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Undefined
|
|
|
|
}
|
2021-06-17 12:49:49 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-13 15:00:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|