Skip to content

Commit fb55135

Browse files
authored
Merge pull request #2785 from wada314/issue-2728
Fix #2728.
2 parents 488e6fd + 036244c commit fb55135

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,18 @@ 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. This is okay because the output here is
723+
// not directly outputted by rustfmt command, but used by the comment formatter's input.
724+
// We have output-file-wide "\n" ==> "\r\n" conversion proccess after here if it's necessary.
725+
let mut config_with_unix_newline = config.clone();
726+
config_with_unix_newline
727+
.set()
728+
.newline_style(NewlineStyle::Unix);
729+
let formatted = format_snippet(&snippet, &config_with_unix_newline)?;
730+
721731
// Trim "fn main() {" on the first line and "}" on the last line,
722732
// then unindent the whole code block.
723-
let formatted = format_snippet(&snippet, config)?;
724-
// 2 = "}\n"
725733
let block_len = formatted.rfind('}').unwrap_or(formatted.len());
726734
let mut is_indented = true;
727735
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)