Skip to content

Commit

Permalink
feat: support descriptions from comments
Browse files Browse the repository at this point in the history
Check if the first node of a given nix file is a comment
and if so, insert them after the files heading while correcting
whitespace.
  • Loading branch information
phaer committed Nov 9, 2023
1 parent 7682e3c commit 2fe77d9
Show file tree
Hide file tree
Showing 3 changed files with 620 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,28 @@ fn collect_entries(root: rnix::Root, category: &str) -> Vec<ManualEntry> {
vec![]
}

fn retrieve_description(
nix: &rnix::Root,
description: &str,
category: &str,
) -> String {
format!(
"# {} {{#sec-functions-library-{}}}\n{}",
description,
category,
dedent(
&nix.syntax()
.first_child()
.and_then(|node|
retrieve_doc_comment(&node, true)
// we add two spaces to the beginning after comment
// markers are stripped to keep dedent correct.
.map(|mut s| {s.insert_str(0, " "); s})
) .unwrap_or_default()
)
)
}

fn main() {
let mut output = io::stdout();
let opts = Options::parse();
Expand All @@ -406,14 +428,14 @@ fn main() {
.expect("could not read location information"),
};
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
let description = retrieve_description(
&nix,
&opts.description,
&opts.category,
);

// TODO: move this to commonmark.rs
writeln!(
output,
"# {} {{#sec-functions-library-{}}}\n",
&opts.description, opts.category
)
.expect("Failed to write header");
writeln!(output, "{}", description).expect("Failed to write header");

for entry in collect_entries(nix, &opts.category) {
entry
Expand Down Expand Up @@ -450,6 +472,26 @@ fn test_main() {
insta::assert_snapshot!(output);
}

#[test]
fn test_description_of_lib_debug() {
let mut output = Vec::new();
let src = fs::read_to_string("test/lib-debug.nix").unwrap();
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
let category = "debug";
let desc = retrieve_description(&nix, &"Debug", category);
writeln!(output, "{}", desc).expect("Failed to write header");

for entry in collect_entries(nix, category) {
entry
.write_section(&Default::default(), &mut output)
.expect("Failed to write section")
}

let output = String::from_utf8(output).expect("not utf8");

insta::assert_snapshot!(output);
}

#[test]
fn test_arg_formatting() {
let mut output = Vec::new();
Expand Down
Loading

0 comments on commit 2fe77d9

Please sign in to comment.