From f53bae5532720252f2d57bbe7928fbb1a65087e6 Mon Sep 17 00:00:00 2001 From: Ben Sully Date: Tue, 2 Aug 2022 08:17:39 +0100 Subject: [PATCH] Don't add extra space before RustDoc comments 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. --- prost-build/src/ast.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/prost-build/src/ast.rs b/prost-build/src/ast.rs index 9b9988411..8815fc568 100644 --- a/prost-build/src/ast.rs +++ b/prost-build/src/ast.rs @@ -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 @@ -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 {