Skip to content

Commit

Permalink
Don't add extra space before RustDoc comments
Browse files Browse the repository at this point in the history
In v0.11.0 the `sanitize_line` function was updated to add a space to
the beginning of non-empty RustDoc comments, but often those comments
already contain a space. This commit updates the check to ensure the
extra space is only added if it doesn't already exist.

Fixes #693.
  • Loading branch information
sd2k committed Aug 2, 2022
1 parent 4459a1e commit f53bae5
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion prost-build/src/ast.rs
Expand Up @@ -149,7 +149,7 @@ impl Comments {

let mut s = RULE_URL.replace_all(line, r"<$0>").to_string();
s = RULE_BRACKETS.replace_all(&s, r"\$1$2\$3").to_string();
if !s.is_empty() {
if !s.chars().next().map_or(false, |c| c == ' ') {
s.insert(0, ' ');
}
s
Expand Down Expand Up @@ -202,6 +202,40 @@ pub struct Method {
mod tests {
use super::*;

#[test]
fn test_comment_append_with_indent_leaves_prespaced_lines() {
struct TestCases {
name: &'static str,
input: String,
expected: String,
}

let tests = vec![
TestCases {
name: "existing_space",
input: " A line with a single leading space.".to_string(),
expected: "/// A line with a single leading space.\n".to_string(),
},
TestCases {
name: "non_existing_space",
input: "A line without a single leading space.".to_string(),
expected: "/// A line without a single leading space.\n".to_string(),
},
];
for t in tests {
let input = Comments {
leading_detached: vec![],
leading: vec![],
trailing: vec![t.input],
};

let mut actual = "".to_string();
input.append_with_indent(0, &mut actual);

assert_eq!(t.expected, actual, "failed {}", t.name);
}
}

#[test]
fn test_comment_append_with_indent_sanitizes_comment_doc_url() {
struct TestCases {
Expand Down

0 comments on commit f53bae5

Please sign in to comment.