From 324c5d3113b8073ae27130fbb57e3a0e47b9f30c Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 20 May 2024 23:22:56 -0700 Subject: [PATCH] Show submodule recipes in --choose (#2069) --- src/namepath.rs | 25 ++++++++++++++----------- src/subcommand.rs | 19 ++++++++++++------- tests/choose.rs | 17 +++++++++++++++++ 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/namepath.rs b/src/namepath.rs index 899b32f..6d4a798 100644 --- a/src/namepath.rs +++ b/src/namepath.rs @@ -9,20 +9,23 @@ impl<'src> Namepath<'src> { } } -impl<'str> Serialize for Namepath<'str> { +impl<'src> Display for Namepath<'src> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + for (i, name) in self.0.iter().enumerate() { + if i > 0 { + write!(f, "::")?; + } + write!(f, "{name}")?; + } + Ok(()) + } +} + +impl<'src> Serialize for Namepath<'src> { fn serialize(&self, serializer: S) -> Result where S: Serializer, { - let mut path = String::new(); - - for (i, name) in self.0.iter().enumerate() { - if i > 0 { - path.push_str("::"); - } - path.push_str(name.lexeme()); - } - - serializer.serialize_str(&path) + serializer.serialize_str(&format!("{self}")) } } diff --git a/src/subcommand.rs b/src/subcommand.rs index ce4e756..5a695bf 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -209,12 +209,17 @@ impl Subcommand { overrides: &BTreeMap, chooser: Option<&str>, ) -> Result<(), Error<'src>> { - let recipes = justfile - .public_recipes(config.unsorted) - .iter() - .filter(|recipe| recipe.min_arguments() == 0) - .copied() - .collect::>>(); + let mut recipes = Vec::<&Recipe>::new(); + let mut stack = vec![justfile]; + while let Some(module) = stack.pop() { + recipes.extend( + module + .public_recipes(config.unsorted) + .iter() + .filter(|recipe| recipe.min_arguments() == 0), + ); + stack.extend(module.modules.values()); + } if recipes.is_empty() { return Err(Error::NoChoosableRecipes); @@ -249,7 +254,7 @@ impl Subcommand { .stdin .as_mut() .expect("Child was created with piped stdio") - .write_all(format!("{}\n", recipe.name).as_bytes()) + .write_all(format!("{}\n", recipe.namepath).as_bytes()) { return Err(Error::ChooserWrite { io_error, chooser }); } diff --git a/tests/choose.rs b/tests/choose.rs index 892045c..0ba1ae3 100644 --- a/tests/choose.rs +++ b/tests/choose.rs @@ -80,6 +80,23 @@ fn skip_private_recipes() { .run(); } +#[test] +fn recipes_in_submodules_can_be_chosen() { + Test::new() + .args(["--unstable", "--choose"]) + .env("JUST_CHOOSER", "head -n10") + .write("bar.just", "baz:\n echo BAZ") + .test_round_trip(false) + .justfile( + " + mod bar + ", + ) + .stderr("echo BAZ\n") + .stdout("BAZ\n") + .run(); +} + #[test] fn skip_recipes_that_require_arguments() { Test::new()