Skip to content

Commit 11a8821

Browse files
committed
Fix #2728.
1 parent 0ec1533 commit 11a8821

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,16 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<String> {
718718
let mut result = String::with_capacity(snippet.len());
719719
let mut is_first = true;
720720

721+
// While formatting the code, ignore the config's newline style setting and always use "\n"
722+
// instead of "\r\n" for the newline characters.
723+
let mut config_with_unix_newline = config.clone();
724+
config_with_unix_newline
725+
.set()
726+
.newline_style(NewlineStyle::Unix);
727+
let formatted = format_snippet(&snippet, &config_with_unix_newline)?;
728+
721729
// Trim "fn main() {" on the first line and "}" on the last line,
722730
// then unindent the whole code block.
723-
let formatted = format_snippet(&snippet, config)?;
724-
// 2 = "}\n"
725731
let block_len = formatted.rfind('}').unwrap_or(formatted.len());
726732
let mut is_indented = true;
727733
for (kind, ref line) in LineClasses::new(&formatted[FN_MAIN_PREFIX.len()..block_len]) {

tests/source/issue-2728.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-newline_style: Windows
3+
4+
//! ```rust
5+
//! extern crate uom;
6+
//! ```
7+
8+
fn main() {}

tests/target/issue-2728.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-newline_style: Windows
3+
4+
//! ```rust
5+
//! extern crate uom;
6+
//! ```
7+
8+
fn main() {}

0 commit comments

Comments
 (0)