diff --git a/compiler/fmt/src/spaces.rs b/compiler/fmt/src/spaces.rs index e8985a398b..938f7baed5 100644 --- a/compiler/fmt/src/spaces.rs +++ b/compiler/fmt/src/spaces.rs @@ -111,10 +111,16 @@ pub fn fmt_comments_only<'a, I>( fn fmt_comment<'a>(buf: &mut String<'a>, comment: &'a str) { buf.push('#'); + if !comment.starts_with(" ") { + buf.push(' '); + } buf.push_str(comment); } fn fmt_docs<'a>(buf: &mut String<'a>, docs: &'a str) { buf.push_str("##"); + if !docs.starts_with(" ") { + buf.push(' '); + } buf.push_str(docs); } diff --git a/compiler/fmt/tests/test_fmt.rs b/compiler/fmt/tests/test_fmt.rs index 89d7775d47..179e72d122 100644 --- a/compiler/fmt/tests/test_fmt.rs +++ b/compiler/fmt/tests/test_fmt.rs @@ -129,6 +129,24 @@ mod test_fmt { ); } + #[test] + fn force_space_at_begining_of_comment() { + expr_formats_to( + indoc!( + r#" + #comment + f + "# + ), + indoc!( + r#" + # comment + f + "# + ), + ); + } + #[test] fn func_def() { expr_formats_same(indoc!( @@ -704,7 +722,7 @@ mod test_fmt { r#" { z: 44, - #comment 0 + # comment 0 y: 41, # comment 1 # comment 2