Skip to content

Commit

Permalink
align text in structured args
Browse files Browse the repository at this point in the history
  • Loading branch information
asymmetric committed Sep 17, 2023
1 parent b0720dc commit 390f864
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/commonmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Argument {
format!(
"`{}`\n\n: {}\n\n",
arg.name,
arg.doc.unwrap_or("Function argument".into()).trim()
handle_indentation(arg.doc.unwrap_or("Function argument".into()).trim())
)
}

Expand Down Expand Up @@ -85,6 +85,22 @@ impl Argument {
}
}

/// Since the first line starts with `: `, indent every other line by 2 spaces, so
/// that the text aligns, to result in:
///
/// : first line
/// every other line
fn handle_indentation(raw: &str) -> String {
let result: String = match raw.split_once('\n') {
Some((first, rest)) => {
format!("{}\n{}", first, textwrap::indent(rest, " "))
}
None => raw.into(),
};

result
}

/// Represents a single manual section describing a library function.
#[derive(Clone, Debug)]
pub struct ManualEntry {
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,21 @@ fn test_line_comments() {

insta::assert_snapshot!(output);
}

#[test]
fn test_multi_line() {
let mut output = Vec::new();
let src = fs::read_to_string("test/multi-line.nix").unwrap();
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
let category = "let";

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);
}
31 changes: 31 additions & 0 deletions src/snapshots/nixdoc__multi_line.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: src/main.rs
expression: output
---
## `lib.let.f` {#function-library-lib.let.f}

Some description

structured function argument

: `x`

: First line

Second line


## `lib.let.g` {#function-library-lib.let.g}

Some other description

structured function argument

: `x`

: First line

Second line



22 changes: 22 additions & 0 deletions test/multi-line.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
/* Some description */
f =
{
/* First line
Second line
*/
x
}: x;

/* Some other description */
g =
{
/*
First line
Second line
*/
x
}: x;
}

0 comments on commit 390f864

Please sign in to comment.