From 0ee13089b435919ef1ff6a7f4e0e1d6ba482a405 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Thu, 18 Aug 2022 14:45:06 -0400 Subject: [PATCH 01/11] Improvements: Adding the tests cases for the issue --- .../issue-5507-indent-style-block.rs | 18 +++++++++++++ .../issue-5507-indent-style-block.rs | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/source/issue-5507/issue-5507-indent-style-block.rs create mode 100644 tests/target/issue-5507/issue-5507-indent-style-block.rs diff --git a/tests/source/issue-5507/issue-5507-indent-style-block.rs b/tests/source/issue-5507/issue-5507-indent-style-block.rs new file mode 100644 index 00000000000..198fa739707 --- /dev/null +++ b/tests/source/issue-5507/issue-5507-indent-style-block.rs @@ -0,0 +1,18 @@ +struct EmptyBody + where T: Eq { +} + +struct LineComment + where T: Eq { + // body +} + +struct BlockComment + where T: Eq { + /* block comment */ +} + +struct HasBody + where T: Eq { + x: T +} diff --git a/tests/target/issue-5507/issue-5507-indent-style-block.rs b/tests/target/issue-5507/issue-5507-indent-style-block.rs new file mode 100644 index 00000000000..9b2c973e10c --- /dev/null +++ b/tests/target/issue-5507/issue-5507-indent-style-block.rs @@ -0,0 +1,25 @@ +struct EmptyBody +where + T: Eq, +{} + +struct LineComment +where + T: Eq, +{ + // body +} + +struct BlockComment +where + T: Eq, +{ + /* block comment */ +} + +struct HasBody +where + T: Eq, +{ + x: T, +} From 2e46b4c56a6a9559f405b746d7c12eba69034056 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Mon, 22 Aug 2022 15:12:22 -0400 Subject: [PATCH 02/11] Improvements: The formatting for generic structs containing where clauses was improved for config `version: Two` --- src/items.rs | 38 ++++++++++++++++--- .../issue-5507-indent-style-block.rs | 2 + .../issue-5507-indent-style-visual.rs | 21 ++++++++++ .../issue-5507-indent-style-block.rs | 6 +-- .../issue-5507-indent-style-visual.rs | 22 +++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 tests/source/issue-5507/issue-5507-indent-style-visual.rs create mode 100644 tests/target/issue-5507/issue-5507-indent-style-visual.rs diff --git a/src/items.rs b/src/items.rs index 755a41f6bf0..fde3774a3cc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1262,6 +1262,29 @@ fn format_unit_struct( Some(format!("{}{};", header_str, generics_str)) } +fn set_brace_pos(has_fields: bool, has_content: bool, version: Version) -> BracePos { + return match version { + Version::One => { + if has_fields { + return BracePos::Auto; + } + BracePos::ForceSameLine + } + Version::Two => { + match (has_fields, has_content) { + // Doesn't have fields, there is nothing between {}, has generics. + (true, true) => BracePos::ForceSameLine, + + // Doesn't have fields, has something between { }, has generics. + (true, false) => BracePos::Auto, + + // Has fields, has generics. + (false, _) => BracePos::Auto, + } + } + }; +} + pub(crate) fn format_struct_struct( context: &RewriteContext<'_>, struct_parts: &StructParts<'_>, @@ -1289,11 +1312,11 @@ pub(crate) fn format_struct_struct( context, g, context.config.brace_style(), - if fields.is_empty() { - BracePos::ForceSameLine - } else { - BracePos::Auto - }, + set_brace_pos( + !fields.is_empty(), + span.hi() <= body_lo + BytePos(5), + context.config.version(), + ), offset, // make a span that starts right after `struct Foo` mk_sp(header_hi, body_lo), @@ -1311,6 +1334,11 @@ pub(crate) fn format_struct_struct( } } }; + let test = mk_sp(body_lo, span.hi()); + //println!("/// Span locations ///"); + //dbg!(test); + //dbg!(body_lo + BytePos(1)); + //println!("/// Span locations ///"); // 1 = `}` let overhead = if fields.is_empty() { 1 } else { 0 }; let total_width = result.len() + generics_str.len() + overhead; diff --git a/tests/source/issue-5507/issue-5507-indent-style-block.rs b/tests/source/issue-5507/issue-5507-indent-style-block.rs index 198fa739707..19d5d3d89c1 100644 --- a/tests/source/issue-5507/issue-5507-indent-style-block.rs +++ b/tests/source/issue-5507/issue-5507-indent-style-block.rs @@ -1,3 +1,5 @@ +// rustfmt-version: Two + struct EmptyBody where T: Eq { } diff --git a/tests/source/issue-5507/issue-5507-indent-style-visual.rs b/tests/source/issue-5507/issue-5507-indent-style-visual.rs new file mode 100644 index 00000000000..d8bc356204f --- /dev/null +++ b/tests/source/issue-5507/issue-5507-indent-style-visual.rs @@ -0,0 +1,21 @@ +// rustfmt-indent_style: Visual +// rustfmt-version: Two + +struct EmptyBody + where T: Eq { +} + +struct LineComment + where T: Eq { + // body +} + +struct BlockComment + where T: Eq { + /* block comment */ +} + +struct HasBody + where T: Eq { + x: T +} diff --git a/tests/target/issue-5507/issue-5507-indent-style-block.rs b/tests/target/issue-5507/issue-5507-indent-style-block.rs index 9b2c973e10c..96de938c070 100644 --- a/tests/target/issue-5507/issue-5507-indent-style-block.rs +++ b/tests/target/issue-5507/issue-5507-indent-style-block.rs @@ -1,3 +1,5 @@ +// rustfmt-version: Two + struct EmptyBody where T: Eq, @@ -13,9 +15,7 @@ where struct BlockComment where T: Eq, -{ - /* block comment */ -} +{/* block comment */} struct HasBody where diff --git a/tests/target/issue-5507/issue-5507-indent-style-visual.rs b/tests/target/issue-5507/issue-5507-indent-style-visual.rs new file mode 100644 index 00000000000..608ae9f06e8 --- /dev/null +++ b/tests/target/issue-5507/issue-5507-indent-style-visual.rs @@ -0,0 +1,22 @@ +// rustfmt-indent_style: Visual +// rustfmt-version: Two + +struct EmptyBody + where T: Eq +{} + +struct LineComment + where T: Eq +{ + // body +} + +struct BlockComment + where T: Eq +{/* block comment */} + +struct HasBody + where T: Eq +{ + x: T, +} From 58e2a54b9c66daa6cbc8296ccb85d50c21d234be Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Mon, 22 Aug 2022 15:17:34 -0400 Subject: [PATCH 03/11] Bugfix: Removing unnecesary value. --- src/items.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/items.rs b/src/items.rs index fde3774a3cc..9a1c705fd70 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1334,11 +1334,6 @@ pub(crate) fn format_struct_struct( } } }; - let test = mk_sp(body_lo, span.hi()); - //println!("/// Span locations ///"); - //dbg!(test); - //dbg!(body_lo + BytePos(1)); - //println!("/// Span locations ///"); // 1 = `}` let overhead = if fields.is_empty() { 1 } else { 0 }; let total_width = result.len() + generics_str.len() + overhead; From b3e36514246ec595e2b32291afd253002eb91c0e Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Mon, 22 Aug 2022 22:36:41 -0400 Subject: [PATCH 04/11] Improvements: A refactor was performed in order to improve readability (based on the comments received on the review process). The test cases were improved, in order to emphasize the fix. --- src/items.rs | 67 ++++++++++++------- ...le-visual.rs => issue-5507-version-one.rs} | 12 +++- ...yle-block.rs => issue-5507-version-two.rs} | 8 +++ .../issue-5507-indent-style-visual.rs | 22 ------ .../issue-5507/issue-5507-version-one.rs | 31 +++++++++ ...yle-block.rs => issue-5507-version-two.rs} | 10 +++ 6 files changed, 102 insertions(+), 48 deletions(-) rename tests/source/issue-5507/{issue-5507-indent-style-visual.rs => issue-5507-version-one.rs} (65%) rename tests/source/issue-5507/{issue-5507-indent-style-block.rs => issue-5507-version-two.rs} (72%) delete mode 100644 tests/target/issue-5507/issue-5507-indent-style-visual.rs create mode 100644 tests/target/issue-5507/issue-5507-version-one.rs rename tests/target/issue-5507/{issue-5507-indent-style-block.rs => issue-5507-version-two.rs} (72%) diff --git a/src/items.rs b/src/items.rs index 9a1c705fd70..09106ef4db7 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1262,27 +1262,50 @@ fn format_unit_struct( Some(format!("{}{};", header_str, generics_str)) } -fn set_brace_pos(has_fields: bool, has_content: bool, version: Version) -> BracePos { - return match version { - Version::One => { - if has_fields { - return BracePos::Auto; - } - BracePos::ForceSameLine - } - Version::Two => { - match (has_fields, has_content) { - // Doesn't have fields, there is nothing between {}, has generics. - (true, true) => BracePos::ForceSameLine, - - // Doesn't have fields, has something between { }, has generics. - (true, false) => BracePos::Auto, - - // Has fields, has generics. +//fn set_brace_pos(has_no_fields: bool, has_content: bool, version: Version) -> BracePos { +// match version { +// Version::One => { +// if has_no_fields { +// return BracePos::ForceSameLine; +// } +// BracePos::Auto +// } +// Version::Two => { +// match (has_no_fields, has_content) { +// (true, true) => BracePos::ForceSameLine, +// (true, false) => BracePos::Auto, +// (false, _) => BracePos::Auto, +// } +// }, +// } +//} + +fn set_brace_pos( + context: &RewriteContext<'_>, + struct_parts: &StructParts<'_>, + fields: &[ast::FieldDef], + body_lo: BytePos, +) -> BracePos { + if fields.is_empty() { + let span = struct_parts.span; + let generics_lo = if let Some(generics) = struct_parts.generics { + generics.span.lo() + } else { + body_lo + }; + let snippet = context.snippet(mk_sp(generics_lo, span.hi())); + let body_contains_comments = contains_comment(snippet); + if context.config.version() == Version::Two { + return match (fields.is_empty(), body_contains_comments) { + (true, true) => BracePos::Auto, + (true, false) if struct_parts.generics.is_some() => BracePos::Auto, + (true, false) => BracePos::ForceSameLine, (false, _) => BracePos::Auto, - } + }; } - }; + return BracePos::ForceSameLine; + } + BracePos::Auto } pub(crate) fn format_struct_struct( @@ -1312,11 +1335,7 @@ pub(crate) fn format_struct_struct( context, g, context.config.brace_style(), - set_brace_pos( - !fields.is_empty(), - span.hi() <= body_lo + BytePos(5), - context.config.version(), - ), + set_brace_pos(&context, &struct_parts, &fields, body_lo), offset, // make a span that starts right after `struct Foo` mk_sp(header_hi, body_lo), diff --git a/tests/source/issue-5507/issue-5507-indent-style-visual.rs b/tests/source/issue-5507/issue-5507-version-one.rs similarity index 65% rename from tests/source/issue-5507/issue-5507-indent-style-visual.rs rename to tests/source/issue-5507/issue-5507-version-one.rs index d8bc356204f..987c736ed99 100644 --- a/tests/source/issue-5507/issue-5507-indent-style-visual.rs +++ b/tests/source/issue-5507/issue-5507-version-one.rs @@ -1,5 +1,4 @@ -// rustfmt-indent_style: Visual -// rustfmt-version: Two +// rustfmt-version: One struct EmptyBody where T: Eq { @@ -10,6 +9,15 @@ struct LineComment // body } + +struct MultiLineComment + where T: Eq { + /* + Multiline + comment. + */ +} + struct BlockComment where T: Eq { /* block comment */ diff --git a/tests/source/issue-5507/issue-5507-indent-style-block.rs b/tests/source/issue-5507/issue-5507-version-two.rs similarity index 72% rename from tests/source/issue-5507/issue-5507-indent-style-block.rs rename to tests/source/issue-5507/issue-5507-version-two.rs index 19d5d3d89c1..6e1d7215749 100644 --- a/tests/source/issue-5507/issue-5507-indent-style-block.rs +++ b/tests/source/issue-5507/issue-5507-version-two.rs @@ -9,6 +9,14 @@ struct LineComment // body } +struct MultiLineComment + where T: Eq { + /* + Multiline + comment. + */ +} + struct BlockComment where T: Eq { /* block comment */ diff --git a/tests/target/issue-5507/issue-5507-indent-style-visual.rs b/tests/target/issue-5507/issue-5507-indent-style-visual.rs deleted file mode 100644 index 608ae9f06e8..00000000000 --- a/tests/target/issue-5507/issue-5507-indent-style-visual.rs +++ /dev/null @@ -1,22 +0,0 @@ -// rustfmt-indent_style: Visual -// rustfmt-version: Two - -struct EmptyBody - where T: Eq -{} - -struct LineComment - where T: Eq -{ - // body -} - -struct BlockComment - where T: Eq -{/* block comment */} - -struct HasBody - where T: Eq -{ - x: T, -} diff --git a/tests/target/issue-5507/issue-5507-version-one.rs b/tests/target/issue-5507/issue-5507-version-one.rs new file mode 100644 index 00000000000..be2860ef3e3 --- /dev/null +++ b/tests/target/issue-5507/issue-5507-version-one.rs @@ -0,0 +1,31 @@ +// rustfmt-version: One + +struct EmptyBody +where + T: Eq, {} + +struct LineComment +where + T: Eq, { + // body +} + +struct MultiLineComment +where + T: Eq, { + /* + Multiline + comment. + */ +} + +struct BlockComment +where + T: Eq, {/* block comment */} + +struct HasBody +where + T: Eq, +{ + x: T, +} diff --git a/tests/target/issue-5507/issue-5507-indent-style-block.rs b/tests/target/issue-5507/issue-5507-version-two.rs similarity index 72% rename from tests/target/issue-5507/issue-5507-indent-style-block.rs rename to tests/target/issue-5507/issue-5507-version-two.rs index 96de938c070..d93fe43acb1 100644 --- a/tests/target/issue-5507/issue-5507-indent-style-block.rs +++ b/tests/target/issue-5507/issue-5507-version-two.rs @@ -12,6 +12,16 @@ where // body } +struct MultiLineComment +where + T: Eq, +{ + /* + Multiline + comment. + */ +} + struct BlockComment where T: Eq, From b5323caa7a3453afcc3dbbef8a7e135bf5fe8d01 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Mon, 22 Aug 2022 22:38:27 -0400 Subject: [PATCH 05/11] Improvements: Removing unnecessary comments. --- src/items.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/items.rs b/src/items.rs index 09106ef4db7..27852ec3b57 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1262,24 +1262,6 @@ fn format_unit_struct( Some(format!("{}{};", header_str, generics_str)) } -//fn set_brace_pos(has_no_fields: bool, has_content: bool, version: Version) -> BracePos { -// match version { -// Version::One => { -// if has_no_fields { -// return BracePos::ForceSameLine; -// } -// BracePos::Auto -// } -// Version::Two => { -// match (has_no_fields, has_content) { -// (true, true) => BracePos::ForceSameLine, -// (true, false) => BracePos::Auto, -// (false, _) => BracePos::Auto, -// } -// }, -// } -//} - fn set_brace_pos( context: &RewriteContext<'_>, struct_parts: &StructParts<'_>, From 0c0fb58aae2cca85549868a9fdf0e1dc9f045cc0 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Mon, 22 Aug 2022 22:39:52 -0400 Subject: [PATCH 06/11] Improvements: Removing additional line break. --- tests/source/issue-5507/issue-5507-version-one.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/source/issue-5507/issue-5507-version-one.rs b/tests/source/issue-5507/issue-5507-version-one.rs index 987c736ed99..6e7e8012d06 100644 --- a/tests/source/issue-5507/issue-5507-version-one.rs +++ b/tests/source/issue-5507/issue-5507-version-one.rs @@ -9,7 +9,6 @@ struct LineComment // body } - struct MultiLineComment where T: Eq { /* From 254c829b063b3eaaa8083deb894fc6f4f8d929f9 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Sat, 17 Sep 2022 12:41:20 -0400 Subject: [PATCH 07/11] Improvements: Now empty impl blocks and impl blocks containing single line block comments are formatted equally in V2 and V1. --- src/items.rs | 30 ++++++++++++++++--- .../issue-5507/issue-5507-version-two.rs | 6 ++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/items.rs b/src/items.rs index 27852ec3b57..da5fcb039e7 100644 --- a/src/items.rs +++ b/src/items.rs @@ -10,9 +10,9 @@ use rustc_span::{symbol, BytePos, Span, DUMMY_SP}; use crate::attr::filter_inline_attrs; use crate::comment::{ - combine_strs_with_missing_comments, contains_comment, is_last_comment_block, - recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment, - FindUncommented, + combine_strs_with_missing_comments, comment_style, contains_comment, find_comment_end, + is_last_comment_block, recover_comment_removed, recover_missing_comment_in_span, + rewrite_missing_comment, FindUncommented, }; use crate::config::lists::*; use crate::config::{BraceStyle, Config, IndentStyle, Version}; @@ -1277,10 +1277,32 @@ fn set_brace_pos( }; let snippet = context.snippet(mk_sp(generics_lo, span.hi())); let body_contains_comments = contains_comment(snippet); + let has_single_line_block_comment = if body_contains_comments { + let comment_start = match snippet.find('/') { + Some(i) => i, + None => 0, + }; + let comment_snippet = &snippet[comment_start..]; + let comment = comment_style(&snippet[comment_start..], false); + let is_block_comment = comment.is_block_comment(); + let is_single_line_comment = if is_block_comment { + let comment_end = match find_comment_end(comment_snippet) { + Some(i) => i, + None => comment_start, + }; + is_single_line(&comment_snippet[..=comment_end - 1]) + } else { + false + }; + is_single_line_comment + } else { + false + }; + if context.config.version() == Version::Two { return match (fields.is_empty(), body_contains_comments) { + (true, true) if has_single_line_block_comment => BracePos::ForceSameLine, (true, true) => BracePos::Auto, - (true, false) if struct_parts.generics.is_some() => BracePos::Auto, (true, false) => BracePos::ForceSameLine, (false, _) => BracePos::Auto, }; diff --git a/tests/target/issue-5507/issue-5507-version-two.rs b/tests/target/issue-5507/issue-5507-version-two.rs index d93fe43acb1..ea38108baf5 100644 --- a/tests/target/issue-5507/issue-5507-version-two.rs +++ b/tests/target/issue-5507/issue-5507-version-two.rs @@ -2,8 +2,7 @@ struct EmptyBody where - T: Eq, -{} + T: Eq, {} struct LineComment where @@ -24,8 +23,7 @@ where struct BlockComment where - T: Eq, -{/* block comment */} + T: Eq, {/* block comment */} struct HasBody where From 088369e565c43e790b5a5884fa936d33b9670f75 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Sat, 17 Sep 2022 12:50:57 -0400 Subject: [PATCH 08/11] Improvements: replacing &snippet[comment_start..] for comment_snippet in order to avoid unnecesary duplication. [items.rs - 1286]. --- src/items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index da5fcb039e7..d728cb8b0a2 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1283,7 +1283,7 @@ fn set_brace_pos( None => 0, }; let comment_snippet = &snippet[comment_start..]; - let comment = comment_style(&snippet[comment_start..], false); + let comment = comment_style(comment_snippet, false); let is_block_comment = comment.is_block_comment(); let is_single_line_comment = if is_block_comment { let comment_end = match find_comment_end(comment_snippet) { From f52638258a13ec5cd67f00b9fd9eb9bd0132c967 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Fri, 7 Oct 2022 19:35:11 -0400 Subject: [PATCH 09/11] Improvements: Removing unnecesary match clause (fields.is_empty()) [items.rs - 1302]. --- src/items.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/items.rs b/src/items.rs index d728cb8b0a2..06cf79f7e44 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1300,11 +1300,10 @@ fn set_brace_pos( }; if context.config.version() == Version::Two { - return match (fields.is_empty(), body_contains_comments) { - (true, true) if has_single_line_block_comment => BracePos::ForceSameLine, - (true, true) => BracePos::Auto, - (true, false) => BracePos::ForceSameLine, - (false, _) => BracePos::Auto, + return match body_contains_comments { + true if has_single_line_block_comment => BracePos::ForceSameLine, + true => BracePos::Auto, + false => BracePos::ForceSameLine, }; } return BracePos::ForceSameLine; From c5098f89172ca76e756626dc405158567d1a1650 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Thu, 13 Oct 2022 08:30:10 -0400 Subject: [PATCH 10/11] Improvements: Making changes to the signature of the set_struct_brace_pos function, in order to simplify it. --- src/items.rs | 65 ++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/src/items.rs b/src/items.rs index 06cf79f7e44..07a08e01780 100644 --- a/src/items.rs +++ b/src/items.rs @@ -10,9 +10,9 @@ use rustc_span::{symbol, BytePos, Span, DUMMY_SP}; use crate::attr::filter_inline_attrs; use crate::comment::{ - combine_strs_with_missing_comments, comment_style, contains_comment, find_comment_end, - is_last_comment_block, recover_comment_removed, recover_missing_comment_in_span, - rewrite_missing_comment, FindUncommented, + combine_strs_with_missing_comments, comment_style, contains_comment, is_last_comment_block, + recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment, + FindUncommented, }; use crate::config::lists::*; use crate::config::{BraceStyle, Config, IndentStyle, Version}; @@ -1262,53 +1262,28 @@ fn format_unit_struct( Some(format!("{}{};", header_str, generics_str)) } -fn set_brace_pos( +fn set_struct_brace_pos( context: &RewriteContext<'_>, - struct_parts: &StructParts<'_>, fields: &[ast::FieldDef], - body_lo: BytePos, + span: Span, ) -> BracePos { - if fields.is_empty() { - let span = struct_parts.span; - let generics_lo = if let Some(generics) = struct_parts.generics { - generics.span.lo() - } else { - body_lo - }; - let snippet = context.snippet(mk_sp(generics_lo, span.hi())); - let body_contains_comments = contains_comment(snippet); - let has_single_line_block_comment = if body_contains_comments { - let comment_start = match snippet.find('/') { - Some(i) => i, - None => 0, - }; - let comment_snippet = &snippet[comment_start..]; - let comment = comment_style(comment_snippet, false); - let is_block_comment = comment.is_block_comment(); - let is_single_line_comment = if is_block_comment { - let comment_end = match find_comment_end(comment_snippet) { - Some(i) => i, - None => comment_start, - }; - is_single_line(&comment_snippet[..=comment_end - 1]) - } else { - false - }; - is_single_line_comment - } else { - false - }; + if !fields.is_empty() { + return BracePos::Auto; + } + let snippet = context.snippet(span).trim(); - if context.config.version() == Version::Two { - return match body_contains_comments { - true if has_single_line_block_comment => BracePos::ForceSameLine, - true => BracePos::Auto, - false => BracePos::ForceSameLine, - }; - } + if snippet.is_empty() || context.config.version() == Version::One { return BracePos::ForceSameLine; } - BracePos::Auto + + let is_single_line = is_single_line(snippet); + let is_block_comment = comment_style(snippet, false).is_block_comment(); + + if is_single_line && is_block_comment { + BracePos::ForceSameLine + } else { + BracePos::Auto + } } pub(crate) fn format_struct_struct( @@ -1338,7 +1313,7 @@ pub(crate) fn format_struct_struct( context, g, context.config.brace_style(), - set_brace_pos(&context, &struct_parts, &fields, body_lo), + set_struct_brace_pos(&context, &fields, mk_sp(body_lo, span.hi() - BytePos(1))), offset, // make a span that starts right after `struct Foo` mk_sp(header_hi, body_lo), From 5cd84e133aeb79f0668d11378befe6e8ec80f305 Mon Sep 17 00:00:00 2001 From: Martin Juarez Date: Thu, 13 Oct 2022 16:47:36 -0400 Subject: [PATCH 11/11] Improvements: Now the inner_span variable is hoisted on the outter most scope of the format_struct_struct function. --- src/items.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/items.rs b/src/items.rs index 07a08e01780..29b5f367a36 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1308,12 +1308,13 @@ pub(crate) fn format_struct_struct( context.snippet_provider.span_after(span, "{") }; + let inner_span = mk_sp(body_lo, span.hi() - BytePos(1)); let generics_str = match struct_parts.generics { Some(g) => format_generics( context, g, context.config.brace_style(), - set_struct_brace_pos(&context, &fields, mk_sp(body_lo, span.hi() - BytePos(1))), + set_struct_brace_pos(&context, &fields, inner_span), offset, // make a span that starts right after `struct Foo` mk_sp(header_hi, body_lo), @@ -1346,7 +1347,6 @@ pub(crate) fn format_struct_struct( } if fields.is_empty() { - let inner_span = mk_sp(body_lo, span.hi() - BytePos(1)); format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}"); return Some(result); }