Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc attribute for docstrings #2050

Merged
merged 5 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,27 @@ Available recipes:
test # test stuff
```

The `[doc]` attribute can be used to set or suppress a recipe's doc comment:

```just
# This comment won't appear
[doc('Build stuff')]
build:
./bin/build

# This one won't either
[doc]
test:
./bin/test
```

```sh
$ just --list
Available recipes:
build # Build stuff
test
```

### Variables and Substitution

Variables, strings, concatenation, path joining, and substitution using `{{…}}`
Expand Down Expand Up @@ -1622,8 +1643,9 @@ Recipes may be annotated with attributes that change their behavior.
| Name | Description |
|------|-------------|
| `[confirm]`<sup>1.17.0</sup> | Require confirmation prior to executing recipe. |
| `[confirm('prompt')]`<sup>1.23.0</sup> | Require confirmation prior to executing recipe with a custom prompt. |
| `[group('NAME"']`<sup>master</sup> | Put recipe in [recipe group](#recipe-groups) `NAME`.
| `[confirm('PROMPT')]`<sup>1.23.0</sup> | Require confirmation prior to executing recipe with a custom prompt. |
| `[doc('DOC')]`<sup>master</sup> | Set recipe's [documentation comment](#documentation-comments) to `DOC`. |
| `[group('NAME"']`<sup>master</sup> | Put recipe in [recipe group](#recipe-groups) `NAME`. |
| `[linux]`<sup>1.8.0</sup> | Enable recipe on Linux. |
| `[macos]`<sup>1.8.0</sup> | Enable recipe on MacOS. |
| `[no-cd]`<sup>1.9.0</sup> | Don't change directory before executing recipe. |
Expand Down
8 changes: 5 additions & 3 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::*;
#[strum_discriminants(strum(serialize_all = "kebab-case"))]
pub(crate) enum Attribute<'src> {
Confirm(Option<StringLiteral<'src>>),
Doc(Option<StringLiteral<'src>>),
Group(StringLiteral<'src>),
Linux,
Macos,
Expand All @@ -24,7 +25,7 @@ pub(crate) enum Attribute<'src> {
impl AttributeDiscriminant {
fn argument_range(self) -> RangeInclusive<usize> {
match self {
Self::Confirm => 0..=1,
Self::Confirm | Self::Doc => 0..=1,
Self::Group => 1..=1,
Self::Linux
| Self::Macos
Expand Down Expand Up @@ -72,6 +73,7 @@ impl<'src> Attribute<'src> {

Ok(match discriminant {
Confirm => Self::Confirm(argument),
Doc => Self::Doc(argument),
Group => Self::Group(argument.unwrap()),
Linux => Self::Linux,
Macos => Self::Macos,
Expand All @@ -91,7 +93,8 @@ impl<'src> Attribute<'src> {
fn argument(&self) -> Option<&StringLiteral> {
match self {
Self::Confirm(prompt) => prompt.as_ref(),
Self::Group(name) => Some(name),
Self::Doc(doc) => doc.as_ref(),
Self::Group(group) => Some(group),
Self::Linux
| Self::Macos
| Self::NoCd
Expand All @@ -107,7 +110,6 @@ impl<'src> Attribute<'src> {
impl<'src> Display for Attribute<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.name())?;

if let Some(argument) = self.argument() {
write!(f, "({argument})")?;
}
Expand Down
9 changes: 9 additions & 0 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ impl<'src, D> Recipe<'src, D> {
})
.collect()
}

pub(crate) fn doc(&self) -> Option<&str> {
for attribute in &self.attributes {
if let Attribute::Doc(doc) = attribute {
return doc.as_ref().map(|s| s.cooked.as_ref());
}
}
self.doc
}
}

impl<'src, D: Display> ColorDisplay for Recipe<'src, D> {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ impl Subcommand {
);

let doc = if i == 0 {
recipe.doc.map(Cow::Borrowed)
recipe.doc().map(Cow::Borrowed)
} else {
Some(Cow::Owned(format!("alias for `{}`", recipe.name)))
};
Expand Down
42 changes: 42 additions & 0 deletions tests/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,45 @@ fn unexpected_attribute_argument() {
.status(1)
.run();
}

#[test]
fn doc_attribute() {
Test::new()
.justfile(
"
# Non-document comment
[doc('The real docstring')]
foo:
echo foo
",
)
.args(["--list"])
.stdout(
"
Available recipes:
foo # The real docstring
",
)
.run();
}

#[test]
fn doc_attribute_suppress() {
Test::new()
.justfile(
"
# Non-document comment
[doc]
foo:
echo foo
",
)
.args(["--list"])
.stdout(
"
Available recipes:
foo
",
)
.run();
}