diff --git a/src/justfile.rs b/src/justfile.rs index 838a268abf..2998bc75ee 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -1239,7 +1239,7 @@ foo := \"xx\" goodbye := \"y\" -hello a b c: x y z +hello a b c: x y z #hello #! blah #blarg {{ foo + bar }}abc{{ goodbye + \"x\" }}xyz diff --git a/src/parser.rs b/src/parser.rs index 88f67205a6..4bfaeae057 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1398,8 +1398,16 @@ impl<'run, 'src> Parser<'run, 'src> { dependencies.append(&mut subsequents); } - if self.next_are(&[Comment, Eol, Indent]) || self.next_are(&[Eol, Indent]) { - self.expect_eol()?; + let trailing_comment = if self.next_are(&[Comment, Eol, Indent]) { + self + .accept(Comment)? + .map(|comment| comment.lexeme().trim_end()) + } else { + None + }; + + if self.next_are(&[Eol, Indent]) { + self.expect(Eol)?; } let body = self.parse_body()?; @@ -1454,6 +1462,7 @@ impl<'run, 'src> Parser<'run, 'src> { quiet, recipe_path: None, shebang: shebang || script, + trailing_comment, variable_references: HashSet::new(), }) } diff --git a/src/recipe.rs b/src/recipe.rs index 0ee35842ca..22d0a3d8de 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -24,6 +24,8 @@ pub(crate) struct Recipe<'src, D = Dependency<'src>> { pub(crate) recipe_path: Option, pub(crate) shebang: bool, #[serde(skip)] + pub(crate) trailing_comment: Option<&'src str>, + #[serde(skip)] pub(crate) variable_references: HashSet, } @@ -773,6 +775,10 @@ impl ColorDisplay for Recipe<'_, D> { write!(f, " {dependency}")?; } + if let Some(trailing_comment) = self.trailing_comment { + write!(f, " {trailing_comment}")?; + } + for (i, line) in self.body.iter().enumerate() { if i == 0 { writeln!(f)?; diff --git a/src/unresolved_recipe.rs b/src/unresolved_recipe.rs index be0b3f1949..efb402705a 100644 --- a/src/unresolved_recipe.rs +++ b/src/unresolved_recipe.rs @@ -249,6 +249,7 @@ impl<'src> UnresolvedRecipe<'src> { quiet: self.quiet, recipe_path: Some(recipe_path), shebang: self.shebang, + trailing_comment: self.trailing_comment, variable_references, }) } diff --git a/tests/format.rs b/tests/format.rs index 0b627867bf..5adb188e48 100644 --- a/tests/format.rs +++ b/tests/format.rs @@ -1848,14 +1848,14 @@ fn trailing_comment_does_not_become_doc_comment() { } #[test] -fn trailing_comment_recipe_with_body_is_stripped() { +fn trailing_comment_recipe_with_body() { assert_dump( " foo: # bar echo baz ", " - foo: + foo: # bar echo baz ", ); @@ -1874,7 +1874,7 @@ fn trailing_comment_export() { } #[test] -fn trailing_comment_recipe_with_dependencies_and_body_is_stripped() { +fn trailing_comment_recipe_with_dependencies_and_body() { assert_dump( " foo: bar # baz @@ -1883,7 +1883,7 @@ fn trailing_comment_recipe_with_dependencies_and_body_is_stripped() { bar: ", " - foo: bar + foo: bar # baz echo qux bar: