]> git.lizzy.rs Git - rust.git/commitdiff
Fix #2728.
authorShohei Wada <pc@wada314.jp>
Wed, 13 Jun 2018 12:54:06 +0000 (21:54 +0900)
committerShohei Wada <pc@wada314.jp>
Tue, 19 Jun 2018 11:51:49 +0000 (20:51 +0900)
src/lib.rs
tests/source/issue-2728.rs [new file with mode: 0644]
tests/target/issue-2728.rs [new file with mode: 0644]

index 7c33a01953be43863c130a04fa442b737571c979..114e72c4847beaf5a4bd1fc6aeb9fe2e9613890c 100644 (file)
@@ -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]) {
diff --git a/tests/source/issue-2728.rs b/tests/source/issue-2728.rs
new file mode 100644 (file)
index 0000000..6cb41b7
--- /dev/null
@@ -0,0 +1,8 @@
+// rustfmt-wrap_comments: true
+// rustfmt-newline_style: Windows
+
+//! ```rust
+//! extern crate uom;
+//! ```
+
+fn main() {}
diff --git a/tests/target/issue-2728.rs b/tests/target/issue-2728.rs
new file mode 100644 (file)
index 0000000..6cb41b7
--- /dev/null
@@ -0,0 +1,8 @@
+// rustfmt-wrap_comments: true
+// rustfmt-newline_style: Windows
+
+//! ```rust
+//! extern crate uom;
+//! ```
+
+fn main() {}