Skip to content

Commit

Permalink
Doc attribute and test
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed May 20, 2024
1 parent dc904ac commit a0e1982
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use super::*;
#[serde(rename_all = "kebab-case")]
pub(crate) enum Attribute<'src> {
Confirm(Option<StringLiteral<'src>>),
Doc {
docstring: Option<String>,
},
#[strum(disabled)]
Group {
name: StringLiteral<'src>,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/list_recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (),
_ => {
Expand Down
9 changes: 9 additions & 0 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
30 changes: 30 additions & 0 deletions tests/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
",
}

0 comments on commit a0e1982

Please sign in to comment.