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

Handle escape sequences in doc-comments #173

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions argh/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ Options:
);
}

#[test]
fn escaped_doc_comment_description() {
#[derive(FromArgs)]
/// A \description\:
/// \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~\
struct Cmd {
#[argh(switch)]
/// a \description\:
/// \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~\
_s: bool,
}

assert_help_string::<Cmd>(
r###"Usage: test_arg_0 [--s]

A \description: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\

Options:
--s a \description: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\
--help display usage information
"###,
);
}

#[test]
fn explicit_long_value_for_option() {
#[derive(FromArgs, Debug)]
Expand Down
35 changes: 32 additions & 3 deletions argh_derive/src/parse_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ fn parse_attr_doc(errors: &Errors, attr: &syn::Attribute, slot: &mut Option<Desc
return;
};

// Don't replace an existing description.
// Don't replace an existing explicit description.
if slot.as_ref().map(|d| d.explicit).unwrap_or(false) {
return;
}
Expand All @@ -509,14 +509,43 @@ fn parse_attr_doc(errors: &Errors, attr: &syn::Attribute, slot: &mut Option<Desc
let lit_str = if let Some(previous) = slot {
let previous = &previous.content;
let previous_span = previous.span();
syn::LitStr::new(&(previous.value() + &*lit_str.value()), previous_span)
syn::LitStr::new(&(previous.value() + &unescape_doc(lit_str.value())), previous_span)
} else {
lit_str.clone()
syn::LitStr::new(&unescape_doc(lit_str.value()), lit_str.span())
};
*slot = Some(Description { explicit: false, content: lit_str });
}
}

/// Replaces escape sequences in doc-comments with the characters they represent.
///
/// Rustdoc understands CommonMark escape sequences consisting of a backslash followed by an ASCII
/// punctuation character. Any other backslash is treated as a literal backslash.
fn unescape_doc(s: String) -> String {
let mut result = String::with_capacity(s.len());

let mut characters = s.chars().peekable();
while let Some(mut character) = characters.next() {
if character == '\\' {
if let Some(next_character) = characters.peek() {
if next_character.is_ascii_punctuation() {
character = *next_character;
characters.next();
}
}
}

// Braces must be escaped as this string will be used as a format string
if character == '{' || character == '}' {
result.push(character);
}

result.push(character);
}

result
}

fn parse_attr_description(errors: &Errors, m: &syn::MetaNameValue, slot: &mut Option<Description>) {
let lit_str =
if let Some(lit_str) = errors.expect_lit_str(&m.value) { lit_str } else { return };
Expand Down