From 19ab1fd30ebf20e4a21b08787086bd761677cb1e Mon Sep 17 00:00:00 2001 From: 160R <160R@protonmail.com> Date: Tue, 28 Dec 2021 04:54:40 +0100 Subject: [PATCH 1/3] display \n as newline without verbatim_doc_comment --- clap_derive/src/utils/doc_comments.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clap_derive/src/utils/doc_comments.rs b/clap_derive/src/utils/doc_comments.rs index 7ac872cf364..256f6d42d6a 100644 --- a/clap_derive/src/utils/doc_comments.rs +++ b/clap_derive/src/utils/doc_comments.rs @@ -103,5 +103,5 @@ fn is_blank(s: &str) -> bool { } fn merge_lines(lines: &[&str]) -> String { - lines.iter().map(|s| s.trim()).collect::>().join(" ") + lines.iter().map(|s| s.trim().replace("\\n", "\n")).collect::>().join(" ") } From 7fbc0aaca3f3a4602483995087c76e3c3344011c Mon Sep 17 00:00:00 2001 From: 160R <160R@protonmail.com> Date: Tue, 28 Dec 2021 22:11:36 +0100 Subject: [PATCH 2/3] allow to escape slash with slash --- clap_derive/src/utils/doc_comments.rs | 33 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/clap_derive/src/utils/doc_comments.rs b/clap_derive/src/utils/doc_comments.rs index 256f6d42d6a..63b2a00018c 100644 --- a/clap_derive/src/utils/doc_comments.rs +++ b/clap_derive/src/utils/doc_comments.rs @@ -25,6 +25,7 @@ pub fn process_doc_comment(lines: Vec, name: &str, preprocess: bool) -> // remove one leading space no matter what for line in lines.iter_mut() { + println!("{}", line); if line.starts_with(' ') { *line = &line[1..]; } @@ -55,7 +56,7 @@ pub fn process_doc_comment(lines: Vec, name: &str, preprocess: bool) -> ] } else { let short = if preprocess { - let s = merge_lines(&lines); + let s = process_paragraph(&lines); remove_period(s) } else { lines.join("\n") @@ -83,7 +84,7 @@ fn split_paragraphs(lines: &[&str]) -> Vec { last_line += start + len; if len != 0 { - Some(merge_lines(&slice[..len])) + Some(process_paragraph(&slice[..len])) } else { None } @@ -91,6 +92,30 @@ fn split_paragraphs(lines: &[&str]) -> Vec { .collect() } +fn process_paragraph(lines: &[&str]) -> String { + lines + .iter() + .map(|s| { + let mut ln = String::with_capacity(s.len()); + let mut chars = s.trim().chars(); + while let Some(c) = chars.next() { + if c == '\\' { + match chars.next() { + Some('\\') => ln.push('\\'), + Some('n') => ln.push('\n'), + Some(x) => { ln.push(c); ln.push(x) }, + None => ln.push(c), + } + } else { + ln.push(c); + } + } + ln + }) + .collect::>() + .join(" ") +} + fn remove_period(mut s: String) -> String { if s.ends_with('.') && !s.ends_with("..") { s.pop(); @@ -101,7 +126,3 @@ fn remove_period(mut s: String) -> String { fn is_blank(s: &str) -> bool { s.trim().is_empty() } - -fn merge_lines(lines: &[&str]) -> String { - lines.iter().map(|s| s.trim().replace("\\n", "\n")).collect::>().join(" ") -} From 2f235cfe30fbf84bece73e1a6426d453642f9479 Mon Sep 17 00:00:00 2001 From: 160R <160R@protonmail.com> Date: Wed, 29 Dec 2021 01:21:13 +0100 Subject: [PATCH 3/3] use markdowns's behavior of \ --- clap_derive/src/utils/doc_comments.rs | 47 ++++++++++++++++++++------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/clap_derive/src/utils/doc_comments.rs b/clap_derive/src/utils/doc_comments.rs index 63b2a00018c..aeb9915dcfb 100644 --- a/clap_derive/src/utils/doc_comments.rs +++ b/clap_derive/src/utils/doc_comments.rs @@ -25,7 +25,6 @@ pub fn process_doc_comment(lines: Vec, name: &str, preprocess: bool) -> // remove one leading space no matter what for line in lines.iter_mut() { - println!("{}", line); if line.starts_with(' ') { *line = &line[1..]; } @@ -95,25 +94,51 @@ fn split_paragraphs(lines: &[&str]) -> Vec { fn process_paragraph(lines: &[&str]) -> String { lines .iter() - .map(|s| { - let mut ln = String::with_capacity(s.len()); - let mut chars = s.trim().chars(); + .map(|ln| ln.trim()) + // Ignore empty lines even if they were escaped with slash + .filter(|ln| ln != &"\\" && !ln.is_empty()) + // Run actual slash escaping + .map(|ln| { + let expected_len = ln.len() + 1; + let mut chars = ln.chars(); + let mut ln = String::with_capacity(expected_len); while let Some(c) = chars.next() { if c == '\\' { - match chars.next() { - Some('\\') => ln.push('\\'), - Some('n') => ln.push('\n'), - Some(x) => { ln.push(c); ln.push(x) }, - None => ln.push(c), + match chars.next() { + // Escape slash with slash + Some('\\') => { + ln.push('\\') + }, + // Don't escape other letters + Some(x) => { + ln.push('\\'); + ln.push(x); + }, + // Escape newline + None => { + // Remove whitespace so it wouldn't + // mess with terminal wrapping + ln = trim_end(ln); + ln.push('\n'); + return ln + }, } } else { ln.push(c); } } + // Since every line is trimmed we need + // this space to avoid gluing words together + ln.push(' '); ln }) - .collect::>() - .join(" ") + .collect() +} + +fn trim_end(mut s: String) -> String { + let while_text = s.trim_end_matches(|c: char| c.is_ascii_whitespace()).len(); + s.truncate(while_text); + s } fn remove_period(mut s: String) -> String {