From 15e936bfc73c3c488512fd3dbce43625fa723c29 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 4 Jun 2017 06:29:08 +0900 Subject: [PATCH] Preserve the layout of comment after return type --- src/items.rs | 24 ++++++++++++++++++++---- tests/target/issue-1113.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 tests/target/issue-1113.rs diff --git a/src/items.rs b/src/items.rs index 644837bd30b..f76ae1c1a4b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -259,10 +259,11 @@ impl<'a> FmtVisitor<'a> { has_body, true)); - if self.config.fn_brace_style() != BraceStyle::AlwaysNextLine && !result.contains('\n') { - newline_brace = false; - } else if force_newline_brace { + if force_newline_brace { newline_brace = true; + } else if self.config.fn_brace_style() != BraceStyle::AlwaysNextLine && + !result.contains('\n') { + newline_brace = false; } // Prepare for the function body by possibly adding a newline and @@ -1757,10 +1758,25 @@ fn rewrite_fn_base(context: &RewriteContext, if where_clause.predicates.is_empty() { let snippet_hi = span.hi; let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi)); + // Try to preserve the layout of the original snippet. + let original_starts_with_newline = + snippet + .find(|c| c != ' ') + .map_or(false, |i| snippet[i..].starts_with('\n')); + let original_ends_with_newline = snippet + .rfind(|c| c != ' ') + .map_or(false, |i| snippet[i..].ends_with('\n')); let snippet = snippet.trim(); if !snippet.is_empty() { - result.push(' '); + result.push(if original_starts_with_newline { + '\n' + } else { + ' ' + }); result.push_str(snippet); + if original_ends_with_newline { + force_new_line_for_brace = true; + } } } else { // FIXME it would be nice to catch comments between the return type diff --git a/tests/target/issue-1113.rs b/tests/target/issue-1113.rs new file mode 100644 index 00000000000..1245bcd057c --- /dev/null +++ b/tests/target/issue-1113.rs @@ -0,0 +1,33 @@ +pub fn foo() -> fmt::Result +//pub fn writeStringToken +{ + panic!() +} + +pub fn foo() -> fmt::Result // pub fn writeStringToken +{ + panic!() +} + +pub fn foo() -> fmt::Result /* pub fn writeStringToken */ { + panic!() +} + +pub fn foo() -> fmt::Result +/* pub fn writeStringToken */ { + panic!() +} + +pub fn foo() -> fmt::Result +/* pub fn writeStringToken */ +{ + panic!() +} + +pub fn foo() -> fmt::Result /* + * + * + */ +{ + panic!() +}