Skip to content

Fix #2728. #2785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,18 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<String> {
let mut result = String::with_capacity(snippet.len());
let mut is_first = true;

// While formatting the code, ignore the config's newline style setting and always use "\n"
// instead of "\r\n" for the newline characters. This is okay because the output here is
// not directly outputted by rustfmt command, but used by the comment formatter's input.
// We have output-file-wide "\n" ==> "\r\n" conversion proccess after here if it's necessary.
let mut config_with_unix_newline = config.clone();
config_with_unix_newline
.set()
.newline_style(NewlineStyle::Unix);
let formatted = format_snippet(&snippet, &config_with_unix_newline)?;

// Trim "fn main() {" on the first line and "}" on the last line,
// then unindent the whole code block.
let formatted = format_snippet(&snippet, config)?;
// 2 = "}\n"
let block_len = formatted.rfind('}').unwrap_or(formatted.len());
let mut is_indented = true;
for (kind, ref line) in LineClasses::new(&formatted[FN_MAIN_PREFIX.len()..block_len]) {
Expand Down
8 changes: 8 additions & 0 deletions tests/source/issue-2728.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_comments: true
// rustfmt-newline_style: Windows

//! ```rust
//! extern crate uom;
//! ```

fn main() {}
8 changes: 8 additions & 0 deletions tests/target/issue-2728.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_comments: true
// rustfmt-newline_style: Windows

//! ```rust
//! extern crate uom;
//! ```

fn main() {}