From 390f8642637280f10c6c499a3fbf965373800cf3 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 17 Sep 2023 12:16:43 +0200 Subject: [PATCH] align text in structured args --- src/commonmark.rs | 18 +++++++++++++++- src/main.rs | 18 ++++++++++++++++ src/snapshots/nixdoc__multi_line.snap | 31 +++++++++++++++++++++++++++ test/multi-line.nix | 22 +++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/snapshots/nixdoc__multi_line.snap create mode 100644 test/multi-line.nix diff --git a/src/commonmark.rs b/src/commonmark.rs index 9e5e82f..6a158e0 100644 --- a/src/commonmark.rs +++ b/src/commonmark.rs @@ -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()) ) } @@ -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 { diff --git a/src/main.rs b/src/main.rs index f660a15..e2b8244 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); +} diff --git a/src/snapshots/nixdoc__multi_line.snap b/src/snapshots/nixdoc__multi_line.snap new file mode 100644 index 0000000..f2bf620 --- /dev/null +++ b/src/snapshots/nixdoc__multi_line.snap @@ -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 + + + diff --git a/test/multi-line.nix b/test/multi-line.nix new file mode 100644 index 0000000..2ea930d --- /dev/null +++ b/test/multi-line.nix @@ -0,0 +1,22 @@ +{ + /* Some description */ + f = + { + /* First line + + Second line + */ + x + }: x; + + /* Some other description */ + g = + { + /* + First line + + Second line + */ + x + }: x; +}