]> git.lizzy.rs Git - rust.git/blobdiff - crates/assists/src/handlers/generate_enum_match_method.rs
Add getter/setter assists
[rust.git] / crates / assists / src / handlers / generate_enum_match_method.rs
index 270b438b705b1c0f2a3da576cb58a1795173526f..c3ff38b6668290499be114e6c6a317be505c40f2 100644 (file)
@@ -1,9 +1,12 @@
 use stdx::{format_to, to_lower_snake_case};
+use syntax::ast::VisibilityOwner;
 use syntax::ast::{self, AstNode, NameOwner};
-use syntax::{ast::VisibilityOwner, T};
 use test_utils::mark;
 
-use crate::{utils::find_struct_impl, AssistContext, AssistId, AssistKind, Assists};
+use crate::{
+    utils::{find_impl_block, find_struct_impl, generate_impl_text},
+    AssistContext, AssistId, AssistKind, Assists,
+};
 
 // Assist: generate_enum_match_method
 //
@@ -25,6 +28,7 @@
 // }
 //
 // impl Version {
+//     /// Returns `true` if the version is [`Minor`].
 //     fn is_minor(&self) -> bool {
 //         matches!(self, Self::Minor)
 //     }
@@ -39,12 +43,13 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
         return None;
     }
 
+    let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string());
     let fn_name = to_lower_snake_case(&variant_name.to_string());
 
     // Return early if we've found an existing new fn
     let impl_def = find_struct_impl(
         &ctx,
-        &ast::AdtDef::Enum(parent_enum.clone()),
+        &ast::Adt::Enum(parent_enum.clone()),
         format!("is_{}", fn_name).as_str(),
     )?;
 
@@ -61,31 +66,23 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
             }
 
             let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
-
             format_to!(
                 buf,
-                "    {}fn is_{}(&self) -> bool {{
+                "    /// Returns `true` if the {} is [`{}`].
+    {}fn is_{}(&self) -> bool {{
         matches!(self, Self::{})
     }}",
+                enum_lowercase_name,
+                variant_name,
                 vis,
                 fn_name,
                 variant_name
             );
 
             let start_offset = impl_def
-                .and_then(|impl_def| {
-                    buf.push('\n');
-                    let start = impl_def
-                        .syntax()
-                        .descendants_with_tokens()
-                        .find(|t| t.kind() == T!['{'])?
-                        .text_range()
-                        .end();
-
-                    Some(start)
-                })
+                .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(&ast::Adt::Enum(parent_enum.clone()), &buf);
                     parent_enum.syntax().text_range().end()
                 });
 
@@ -94,16 +91,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;
@@ -133,6 +120,7 @@ enum Variant {
 }
 
 impl Variant {
+    /// Returns `true` if the variant is [`Minor`].
     fn is_minor(&self) -> bool {
         matches!(self, Self::Minor)
     }
@@ -180,6 +168,7 @@ fn test_generate_enum_match_from_variant_with_one_variant() {
 enum Variant { Undefined }
 
 impl Variant {
+    /// Returns `true` if the variant is [`Undefined`].
     fn is_undefined(&self) -> bool {
         matches!(self, Self::Undefined)
     }
@@ -204,6 +193,7 @@ pub(crate) enum Variant {
 }
 
 impl Variant {
+    /// Returns `true` if the variant is [`Minor`].
     pub(crate) fn is_minor(&self) -> bool {
         matches!(self, Self::Minor)
     }