Skip to content

Commit f3acdc9

Browse files
committed
have cfg_select break long meta item key-value lines
1 parent 05c3e9e commit f3acdc9

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

src/tools/rustfmt/src/attr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ pub(crate) fn rewrite_meta_item_result(
355355
// Insert a line break before the `=`.
356356
let mut result = String::new();
357357
result.push_str(&path);
358-
result.push_str(&shape.indent.to_string_with_newline(context.config));
358+
let indented = shape.indent.block_indent(context.config);
359+
result.push_str(&indented.to_string_with_newline(context.config));
359360
result.push_str("= ");
360361
result.push_str(context.snippet(lit.span));
361362

src/tools/rustfmt/src/macros.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol};
2020
use tracing::debug;
2121

2222
use crate::Config;
23+
use crate::attr::rewrite_meta_item_inner_result;
2324
use crate::comment::{
2425
CharClasses, FindUncommented, FullCodeCharKind, LineClasses, contains_comment,
2526
};
@@ -1513,6 +1514,39 @@ fn format_lazy_static(
15131514
Ok(result)
15141515
}
15151516

1517+
fn format_cfg_select_rule(
1518+
rule: &ast::MetaItemInner,
1519+
context: &RewriteContext<'_>,
1520+
shape: Shape,
1521+
span: Span,
1522+
) -> RewriteResult {
1523+
let mut result = String::with_capacity(128);
1524+
1525+
// The cfg plus ` => {` should stay within the line length.
1526+
let rule_shape = shape
1527+
.sub_width(" => {".len())
1528+
.max_width_error(shape.width, span)?;
1529+
1530+
let formatted = rewrite_meta_item_inner_result(rule, context, rule_shape, true)?;
1531+
result.push_str(&formatted);
1532+
1533+
let is_key_value = match rule {
1534+
ast::MetaItemInner::MetaItem(ref meta_item) => {
1535+
matches!(meta_item.kind, ast::MetaItemKind::NameValue(_))
1536+
}
1537+
_ => false,
1538+
};
1539+
1540+
if is_key_value && formatted.contains('\n') {
1541+
result.push_str(&shape.indent.to_string_with_newline(context.config));
1542+
result.push_str("=>");
1543+
} else {
1544+
result.push_str(" =>");
1545+
}
1546+
1547+
Ok(result)
1548+
}
1549+
15161550
fn format_cfg_select(
15171551
context: &RewriteContext<'_>,
15181552
shape: Shape,
@@ -1529,16 +1563,10 @@ fn format_cfg_select(
15291563
.block_indent(context.config.tab_spaces())
15301564
.with_max_width(context.config);
15311565

1532-
// The cfg plus ` => {` should stay within the line length.
1533-
let rule_shape = shape
1534-
.sub_width(" => {".len())
1535-
.max_width_error(shape.width, span)?;
1536-
15371566
result.push_str(&shape.indent.to_string_with_newline(context.config));
15381567

15391568
for (rule, rhs, _) in branches.reachable {
1540-
result.push_str(&rule.rewrite_result(context, rule_shape)?);
1541-
result.push_str(" =>");
1569+
result.push_str(&format_cfg_select_rule(&rule, context, shape, span)?);
15421570
result.push_str(&format_cfg_select_rhs(context, shape, rhs)?);
15431571
}
15441572

@@ -1552,14 +1580,13 @@ fn format_cfg_select(
15521580

15531581
match lhs {
15541582
CfgSelectPredicate::Cfg(rule) => {
1555-
result.push_str(&rule.rewrite_result(context, rule_shape)?);
1583+
result.push_str(&format_cfg_select_rule(&rule, context, shape, span)?);
15561584
}
15571585
CfgSelectPredicate::Wildcard(_) => {
1558-
result.push('_');
1586+
result.push_str("_ =>");
15591587
}
15601588
}
15611589

1562-
result.push_str(" =>");
15631590
result.push_str(&format_cfg_select_rhs(context, shape, rhs)?);
15641591
}
15651592

src/tools/rustfmt/tests/source/cfg_select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ cfg_select! {
8888
=> {
8989
// abc
9090
}
91-
_ => {
91+
anything( "some other long long long long long thing long long long long long long long long long long long",) => {}
9292
// abc
9393
}
9494
}

src/tools/rustfmt/tests/target/cfg_select.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,15 @@ cfg_select! {
117117
}
118118

119119
cfg_select! {
120-
feature = "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff" => {}
121-
feature = "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff" => {}
122-
_ => {}
120+
feature
121+
= "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff"
122+
=> {
123+
println!();
124+
}
125+
feature
126+
= "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff"
127+
=> {}
128+
anything(
129+
"some other long long long long long thing long long long long long long long long long long long",
130+
) => {}
123131
}

0 commit comments

Comments
 (0)