Skip to content

Commit

Permalink
Give modules doc comments for --list (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spatenheinz authored Jun 29, 2024
1 parent e07da79 commit ef6a813
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 38 deletions.
29 changes: 22 additions & 7 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ pub(crate) struct Analyzer<'src> {

impl<'src> Analyzer<'src> {
pub(crate) fn analyze(
asts: &HashMap<PathBuf, Ast<'src>>,
doc: Option<&'src str>,
loaded: &[PathBuf],
name: Option<Name<'src>>,
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
name: Option<Name<'src>>,
) -> CompileResult<'src, Justfile<'src>> {
Self::default().justfile(loaded, paths, asts, root, name)
Self::default().justfile(asts, doc, loaded, name, paths, root)
}

fn justfile(
mut self,
asts: &HashMap<PathBuf, Ast<'src>>,
doc: Option<&'src str>,
loaded: &[PathBuf],
name: Option<Name<'src>>,
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
name: Option<Name<'src>>,
) -> CompileResult<'src, Justfile<'src>> {
let mut recipes = Vec::new();

Expand Down Expand Up @@ -84,10 +86,22 @@ impl<'src> Analyzer<'src> {
stack.push(asts.get(absolute).unwrap());
}
}
Item::Module { absolute, name, .. } => {
Item::Module {
absolute,
name,
doc,
..
} => {
if let Some(absolute) = absolute {
define(*name, "module", false)?;
modules.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
modules.insert(Self::analyze(
asts,
*doc,
loaded,
Some(*name),
paths,
absolute,
)?);
}
}
Item::Recipe(recipe) => {
Expand Down Expand Up @@ -172,6 +186,7 @@ impl<'src> Analyzer<'src> {
Rc::clone(next)
}),
}),
doc,
loaded: loaded.into(),
modules,
name,
Expand Down
5 changes: 3 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Compiler {
name,
optional,
relative,
..
} => {
if !unstable {
return Err(Error::Unstable {
Expand Down Expand Up @@ -107,7 +108,7 @@ impl Compiler {
asts.insert(current.path, ast.clone());
}

let justfile = Analyzer::analyze(&loaded, &paths, &asts, root, None)?;
let justfile = Analyzer::analyze(&asts, None, &loaded, None, &paths, root)?;

Ok(Compilation {
asts,
Expand Down Expand Up @@ -184,7 +185,7 @@ impl Compiler {
asts.insert(root.clone(), ast);
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert(root.clone(), root.clone());
Analyzer::analyze(&[], &paths, &asts, &root, None)
Analyzer::analyze(&asts, None, &[], None, &paths, &root)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) enum Item<'src> {
},
Module {
absolute: Option<PathBuf>,
doc: Option<&'src str>,
name: Name<'src>,
optional: bool,
relative: Option<StringLiteral<'src>>,
Expand Down
1 change: 1 addition & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Invocation<'src: 'run, 'run> {
pub(crate) struct Justfile<'src> {
pub(crate) aliases: Table<'src, Alias<'src>>,
pub(crate) assignments: Table<'src, Assignment<'src>>,
pub(crate) doc: Option<&'src str>,
#[serde(rename = "first", serialize_with = "keyed::serialize_option")]
pub(crate) default: Option<Rc<Recipe<'src>>>,
#[serde(skip)]
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ impl<'run, 'src> Parser<'run, 'src> {
|| self.next_are(&[Identifier, Identifier, StringToken])
|| self.next_are(&[Identifier, QuestionMark]) =>
{
let doc = pop_doc_comment(&mut items, eol_since_last_comment);

self.presume_keyword(Keyword::Mod)?;

let optional = self.accepted(QuestionMark)?;
Expand All @@ -387,6 +389,7 @@ impl<'run, 'src> Parser<'run, 'src> {

items.push(Item::Module {
absolute: None,
doc,
name,
optional,
relative,
Expand Down
54 changes: 41 additions & 13 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ impl Subcommand {
}

fn list_module(config: &Config, module: &Justfile, depth: usize) {
fn format_doc(
config: &Config,
name: &str,
doc: Option<&str>,
max_signature_width: usize,
signature_widths: &BTreeMap<&str, usize>,
) {
if let Some(doc) = doc {
if doc.lines().count() <= 1 {
print!(
"{:padding$}{} {}",
"",
config.color.stdout().doc().paint("#"),
config.color.stdout().doc().paint(doc),
padding = max_signature_width.saturating_sub(signature_widths[name]) + 1,
);
}
}
println!();
}

let aliases = if config.no_aliases {
BTreeMap::new()
} else {
Expand Down Expand Up @@ -468,6 +489,11 @@ impl Subcommand {
);
}
}
if !config.list_submodules {
for (name, _) in &module.modules {
signature_widths.insert(name, UnicodeWidthStr::width(format!("{name} ...").as_str()));
}
}

signature_widths
};
Expand Down Expand Up @@ -554,18 +580,13 @@ impl Subcommand {
RecipeSignature { name, recipe }.color_display(config.color.stdout())
);

if let Some(doc) = doc {
if doc.lines().count() <= 1 {
print!(
"{:padding$}{} {}",
"",
config.color.stdout().doc().paint("#"),
config.color.stdout().doc().paint(&doc),
padding = max_signature_width.saturating_sub(signature_widths[name]) + 1,
);
}
}
println!();
format_doc(
config,
name,
doc.as_deref(),
max_signature_width,
&signature_widths,
);
}
}
}
Expand All @@ -582,7 +603,14 @@ impl Subcommand {
}
} else {
for submodule in module.modules(config) {
println!("{list_prefix}{} ...", submodule.name(),);
print!("{list_prefix}{} ...", submodule.name());
format_doc(
config,
submodule.name(),
submodule.doc,
max_signature_width,
&signature_widths,
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(crate) fn analysis_error(
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert("justfile".into(), "justfile".into());

match Analyzer::analyze(&[], &paths, &asts, &root, None) {
match Analyzer::analyze(&asts, None, &[], None, &paths, &root) {
Ok(_) => panic!("Analysis unexpectedly succeeded"),
Err(have) => {
let want = CompileError {
Expand Down
30 changes: 15 additions & 15 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ b := env_var_or_default('ZADDY', 'HTAP')
x := env_var_or_default('XYZ', 'ABC')
foo:
/bin/echo '{{p}}' '{{b}}' '{{x}}'
/usr/bin/env echo '{{p}}' '{{b}}' '{{x}}'
"#,
stdout: format!("{} HTAP ABC\n", env::var("USER").unwrap()).as_str(),
stderr: format!("/bin/echo '{}' 'HTAP' 'ABC'\n", env::var("USER").unwrap()).as_str(),
stderr: format!("/usr/bin/env echo '{}' 'HTAP' 'ABC'\n", env::var("USER").unwrap()).as_str(),
}

#[cfg(not(windows))]
Expand All @@ -52,10 +52,10 @@ ext := extension('/foo/bar/baz.hello')
jn := join('a', 'b')
foo:
/bin/echo '{{we}}' '{{fs}}' '{{fn}}' '{{dir}}' '{{ext}}' '{{jn}}'
/usr/bin/env echo '{{we}}' '{{fs}}' '{{fn}}' '{{dir}}' '{{ext}}' '{{jn}}'
"#,
stdout: "/foo/bar/baz baz baz.hello /foo/bar hello a/b\n",
stderr: "/bin/echo '/foo/bar/baz' 'baz' 'baz.hello' '/foo/bar' 'hello' 'a/b'\n",
stderr: "/usr/bin/env echo '/foo/bar/baz' 'baz' 'baz.hello' '/foo/bar' 'hello' 'a/b'\n",
}

#[cfg(not(windows))]
Expand All @@ -69,10 +69,10 @@ dir := parent_directory('/foo/')
ext := extension('/foo/bar/baz.hello.ciao')
foo:
/bin/echo '{{we}}' '{{fs}}' '{{fn}}' '{{dir}}' '{{ext}}'
/usr/bin/env echo '{{we}}' '{{fs}}' '{{fn}}' '{{dir}}' '{{ext}}'
"#,
stdout: "/foo/bar/baz baz.hello baz.hello.ciao / ciao\n",
stderr: "/bin/echo '/foo/bar/baz' 'baz.hello' 'baz.hello.ciao' '/' 'ciao'\n",
stderr: "/usr/bin/env echo '/foo/bar/baz' 'baz.hello' 'baz.hello.ciao' '/' 'ciao'\n",
}

#[cfg(not(windows))]
Expand All @@ -82,7 +82,7 @@ test! {
we := without_extension('')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{} {}\n{}\n{}\n{}\n{}\n",
Expand All @@ -102,7 +102,7 @@ test! {
we := extension('')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{}\n{}\n{}\n{}\n{}\n",
Expand All @@ -121,7 +121,7 @@ test! {
we := extension('foo')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{}\n{}\n{}\n{}\n{}\n",
Expand All @@ -140,7 +140,7 @@ test! {
we := file_stem('')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{}\n{}\n{}\n{}\n{}\n",
Expand All @@ -159,7 +159,7 @@ test! {
we := file_name('')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{}\n{}\n{}\n{}\n{}\n",
Expand All @@ -178,7 +178,7 @@ test! {
we := parent_directory('')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{} {}\n{}\n{}\n{}\n{}\n",
Expand All @@ -198,7 +198,7 @@ test! {
we := parent_directory('/')
foo:
/bin/echo '{{we}}'
/usr/bin/env echo '{{we}}'
"#,
stdout: "",
stderr: format!("{} {}\n{}\n{}\n{}\n{}\n",
Expand All @@ -220,10 +220,10 @@ b := env_var_or_default('ZADDY', 'HTAP')
x := env_var_or_default('XYZ', 'ABC')
foo:
/bin/echo '{{p}}' '{{b}}' '{{x}}'
/usr/bin/env echo '{{p}}' '{{b}}' '{{x}}'
"#,
stdout: format!("{} HTAP ABC\n", env::var("USERNAME").unwrap()).as_str(),
stderr: format!("/bin/echo '{}' 'HTAP' 'ABC'\n", env::var("USERNAME").unwrap()).as_str(),
stderr: format!("/usr/bin/env echo '{}' 'HTAP' 'ABC'\n", env::var("USERNAME").unwrap()).as_str(),
}

test! {
Expand Down
Loading

0 comments on commit ef6a813

Please sign in to comment.