From 778a57052f38a22f5369262b637ed07dbfa36802 Mon Sep 17 00:00:00 2001 From: ChinYing-Li Date: Sat, 6 Mar 2021 19:41:11 +0800 Subject: [PATCH] Keep blank lines in chained block-style comments (#4012) --- src/formatting/chains.rs | 15 ++++++++++++--- tests/source/chains_with_comment.rs | 11 +++++++++++ tests/target/chains_with_comment.rs | 11 +++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/formatting/chains.rs b/src/formatting/chains.rs index ca8f427d26a..dd3f500e8ef 100644 --- a/src/formatting/chains.rs +++ b/src/formatting/chains.rs @@ -859,9 +859,18 @@ fn trim_tries(s: &str) -> String { for (kind, rich_char) in CharClasses::new(s.chars()) { match rich_char.get_char() { '\n' => { - if result.is_empty() || !line_buffer.trim().is_empty() { - result.push_str(&line_buffer); - result.push('\n') + let last_new_line = result.rfind('\n').unwrap_or(0); + match result[..last_new_line].rfind('\n') { + // Remove the current line if it's blank and follows a line-style comment + Some(second_last_newline) + if result[second_last_newline + 1..] + .trim_start() + .starts_with("//") + && line_buffer.trim().is_empty() => {} + _ => { + result.push_str(&line_buffer); + result.push('\n'); + } } line_buffer.clear(); } diff --git a/tests/source/chains_with_comment.rs b/tests/source/chains_with_comment.rs index 91160711b89..baa6fedc0fe 100644 --- a/tests/source/chains_with_comment.rs +++ b/tests/source/chains_with_comment.rs @@ -119,3 +119,14 @@ fn foo() { ? ? ? .baz; } + +// #4012 +pub fn foo2() { + let a = chain + .get() + /* + + + */ + .set(); +} diff --git a/tests/target/chains_with_comment.rs b/tests/target/chains_with_comment.rs index 522d70713bc..38470645d10 100644 --- a/tests/target/chains_with_comment.rs +++ b/tests/target/chains_with_comment.rs @@ -135,3 +135,14 @@ fn foo() { // comment .baz; } + +// #4012 +pub fn foo2() { + let a = chain + .get() + /* + + + */ + .set(); +}