Skip to content

Commit

Permalink
Add doc for modules for --list
Browse files Browse the repository at this point in the history
  • Loading branch information
Spatenheinz committed Jun 27, 2024
1 parent 570d305 commit 999c69c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
21 changes: 18 additions & 3 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ impl<'src> Analyzer<'src> {
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
name: Option<Name<'src>>,
doc: Option<&'src str>,
) -> CompileResult<'src, Justfile<'src>> {
Self::default().justfile(loaded, paths, asts, root, name)
Self::default().justfile(loaded, paths, asts, root, name, doc)
}

fn justfile(
Expand All @@ -25,6 +26,7 @@ impl<'src> Analyzer<'src> {
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
name: Option<Name<'src>>,
doc: Option<&'src str>,
) -> 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,
comment,
..
} => {
if let Some(absolute) = absolute {
define(*name, "module", false)?;
modules.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
modules.insert(Self::analyze(
loaded,
paths,
asts,
absolute,
Some(*name),
*comment,
)?);
}
}
Item::Recipe(recipe) => {
Expand Down Expand Up @@ -175,6 +189,7 @@ impl<'src> Analyzer<'src> {
loaded: loaded.into(),
modules,
name,
doc,
recipes,
settings,
source: root.into(),
Expand Down
3 changes: 2 additions & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Compiler {
name,
optional,
relative,
comment: _,
} => {
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(&loaded, &paths, &asts, root, None, None)?;

Ok(Compilation {
asts,
Expand Down
1 change: 1 addition & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) enum Item<'src> {
name: Name<'src>,
optional: bool,
relative: Option<StringLiteral<'src>>,
comment: Option<&'src str>,
},
Recipe(UnresolvedRecipe<'src>),
Set(Set<'src>),
Expand Down
1 change: 1 addition & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) struct Justfile<'src> {
pub(crate) modules: Table<'src, Justfile<'src>>,
#[serde(skip)]
pub(crate) name: Option<Name<'src>>,
pub(crate) doc: Option<&'src str>,
pub(crate) recipes: Table<'src, Rc<Recipe<'src>>>,
pub(crate) settings: Settings<'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 @@ -371,6 +371,8 @@ impl<'run, 'src> Parser<'run, 'src> {
|| self.next_are(&[Identifier, Identifier, Eol])
|| self.next_are(&[Identifier, QuestionMark]) =>
{
let comment = pop_doc_comment(&mut items, eol_since_last_comment);

self.presume_keyword(Keyword::Mod)?;

let optional = self.accepted(QuestionMark)?;
Expand All @@ -389,6 +391,7 @@ impl<'run, 'src> Parser<'run, 'src> {
name,
optional,
relative,
comment,
});
}
Some(Keyword::Set)
Expand Down
54 changes: 41 additions & 13 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,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 +559,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!();
Self::format_doc(
config,
name,
doc.as_deref(),
max_signature_width,
&signature_widths,
);
}
}
}
Expand All @@ -582,11 +582,39 @@ impl Subcommand {
}
} else {
for submodule in module.modules(config) {
println!("{list_prefix}{} ...", submodule.name(),);
print!("{list_prefix}{} ...", submodule.name(),);
Self::format_doc(
config,
submodule.name(),
submodule.doc,
max_signature_width,
&signature_widths,
);
}
}
}

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!();
}

fn show<'src>(
config: &Config,
mut module: &Justfile<'src>,
Expand Down

0 comments on commit 999c69c

Please sign in to comment.