Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to escape newline with \ like in markdown #3228

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions clap_derive/src/utils/doc_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn process_doc_comment(lines: Vec<String>, 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")
Expand Down Expand Up @@ -83,14 +83,64 @@ fn split_paragraphs(lines: &[&str]) -> Vec<String> {
last_line += start + len;

if len != 0 {
Some(merge_lines(&slice[..len]))
Some(process_paragraph(&slice[..len]))
} else {
None
}
})
.collect()
}

fn process_paragraph(lines: &[&str]) -> String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go this route, I'd like to see test cases for this to show we are matching expected commonmark behavior

lines
.iter()
.map(|ln| ln.trim())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm misreading the commonmark spec but it doesn't sound like there can be spaces between \\ and \n which a trim would allow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked the commonmark preview. one space after leaves a \\. More then one space after is treated as \\\n because of another hard break rule.

// 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 == '\\' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without code fence support, we could accidentally create a hard break when we shouldn't and people can end up relying on that.

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

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 {
if s.ends_with('.') && !s.ends_with("..") {
s.pop();
Expand All @@ -101,7 +151,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()).collect::<Vec<_>>().join(" ")
}