diff --git a/src/attribute.rs b/src/attribute.rs index 1ba3d52c6c..e6329b7491 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -5,6 +5,9 @@ use super::*; #[serde(rename_all = "kebab-case")] pub(crate) enum Attribute<'src> { Confirm(Option>), + Doc { + docstring: Option, + }, #[strum(disabled)] Group { name: StringLiteral<'src>, @@ -27,6 +30,9 @@ impl<'src> Attribute<'src> { let name_str = name.lexeme(); Ok(match (name_str, maybe_argument) { + ("doc", arg) => Self::Doc { + docstring: arg.map(|arg| arg.cooked.clone()), + }, ("group", Some(name)) => Self::Group { name }, ("group", None) => { return Err(name.error(CompileErrorKind::MissingAttributeArgument { diff --git a/src/list_recipes.rs b/src/list_recipes.rs index 0a87fe6956..34902f5db9 100644 --- a/src/list_recipes.rs +++ b/src/list_recipes.rs @@ -76,7 +76,7 @@ fn print_recipe( let padding = max_line_width.saturating_sub(line_widths.get(name).copied().unwrap_or(max_line_width)); - match (i, recipe.doc) { + match (i, recipe.docstring()) { (0, Some(doc)) => print_doc_comment(doc, padding, doc_color), (0, None) => (), _ => { diff --git a/src/recipe.rs b/src/recipe.rs index 9fc3ef3c8b..764cc9385c 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -439,6 +439,15 @@ impl<'src, D> Recipe<'src, D> { } groups } + + pub(crate) fn docstring(&self) -> Option<&str> { + for attribute in &self.attributes { + if let Attribute::Doc { docstring } = attribute { + return docstring.as_ref().map(|s| s.as_str()); + } + } + self.doc + } } impl<'src, D: Display> ColorDisplay for Recipe<'src, D> { diff --git a/tests/attributes.rs b/tests/attributes.rs index a3a451ccd7..e92c424996 100644 --- a/tests/attributes.rs +++ b/tests/attributes.rs @@ -129,3 +129,33 @@ fn unexpected_attribute_argument() { .status(1) .run(); } + +test! { + name: doc_attribute, + justfile: " + # Non-document comment + [doc('The real docstring')] + foo: + echo foo + ", + args: ("--list"), + stdout: " + Available recipes: + foo # The real docstring + ", +} + +test! { + name: suppress_doc_comment, + justfile: " + # Non-document comment + [doc] + foo: + echo foo + ", + args: ("--list"), + stdout: " + Available recipes: + foo + ", +}