From 8f7a0470b073803317e85e306308a4b9457dc5cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Campinas?= Date: Sat, 13 Oct 2018 09:57:43 +0200 Subject: [PATCH 1/1] handle lines prefixed with a # inside code blocks --- src/comment.rs | 11 +++++++++-- tests/source/wrapped_hidden_code_block.rs | 10 ++++++++++ tests/target/wrapped_hidden_code_block.rs | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/source/wrapped_hidden_code_block.rs create mode 100644 tests/target/wrapped_hidden_code_block.rs diff --git a/src/comment.rs b/src/comment.rs index 213879e50a1..c946cc6e1c0 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -752,9 +752,16 @@ fn trim_custom_comment_prefix(s: &str) -> String { .map(|line| { let left_trimmed = line.trim_left(); if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) { - left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX) + let orig = left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX); + // due to comment wrapping, a line that was originaly behind `#` is split over + // multiple lines, which needs then to be prefixed with a `#` + if !orig.trim_left().starts_with("# ") { + format!("# {}", orig) + } else { + orig.to_string() + } } else { - line + line.to_string() } }) .collect::>() diff --git a/tests/source/wrapped_hidden_code_block.rs b/tests/source/wrapped_hidden_code_block.rs new file mode 100644 index 00000000000..d8de8443812 --- /dev/null +++ b/tests/source/wrapped_hidden_code_block.rs @@ -0,0 +1,10 @@ +// rustfmt-max_width: 79 +// rustfmt-wrap_comments: true + +/// ```rust +/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd)not(dox), feature(cfg_target_feature, target_feature, stdsimd))] +/// +/// // Est lectus hendrerit lorem, eget dignissim orci nisl sit amet massa. Etiam volutpat lobortis eros. +/// let x = 42; +/// ``` +fn func() {} diff --git a/tests/target/wrapped_hidden_code_block.rs b/tests/target/wrapped_hidden_code_block.rs new file mode 100644 index 00000000000..572f448c4e7 --- /dev/null +++ b/tests/target/wrapped_hidden_code_block.rs @@ -0,0 +1,13 @@ +// rustfmt-max_width: 79 +// rustfmt-wrap_comments: true + +/// ```rust +/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, +/// # stdsimd)not(dox), feature(cfg_target_feature, target_feature, +/// # stdsimd))] +/// +/// // Est lectus hendrerit lorem, eget dignissim orci nisl sit amet massa. +/// // Etiam volutpat lobortis eros. +/// let x = 42; +/// ``` +fn func() {} -- 2.44.0