Skip to content

Commit d818f52

Browse files
authored
[fix] Properly track hard newlines in brace blocks (#875)
* Test fixtures * [fix] Properly recurse through breakable token types when tooking for hard newlines
1 parent ab5ed7e commit d818f52

3 files changed

Lines changed: 86 additions & 6 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
foo(-> { a; b })
2+
foo(proc { a; b })
3+
foo(lambda { a; b })
4+
foo(bar { a; b })
5+
[proc { a; b }, other]
6+
{key: proc { a; b }}
7+
foo([1, 2, 3], proc { a; b })
8+
nested = foo(bar(baz { a; b }))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
foo(
2+
-> {
3+
a
4+
b
5+
}
6+
)
7+
foo(
8+
proc {
9+
a
10+
b
11+
}
12+
)
13+
foo(
14+
lambda {
15+
a
16+
b
17+
}
18+
)
19+
foo(
20+
bar {
21+
a
22+
b
23+
}
24+
)
25+
[
26+
proc {
27+
a
28+
b
29+
},
30+
other
31+
]
32+
{
33+
key: proc {
34+
a
35+
b
36+
}
37+
}
38+
foo(
39+
[1, 2, 3],
40+
proc {
41+
a
42+
b
43+
}
44+
)
45+
nested = foo(
46+
bar(
47+
baz {
48+
a
49+
b
50+
}
51+
)
52+
)

librubyfmt/src/render_targets.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ fn insert_at<T>(idx: usize, target: &mut Vec<T>, input: impl IntoIterator<Item =
77
target.splice(idx..idx, input);
88
}
99

10+
/// Recursively checks if a token tree contains any `HardNewLine`
11+
fn tokens_contain_hard_newline(token: &AbstractLineToken<'_>) -> bool {
12+
match token {
13+
AbstractLineToken::ConcreteLineToken(ConcreteLineToken::HardNewLine) => true,
14+
AbstractLineToken::BreakableEntry(be) => {
15+
be.tokens().iter().any(tokens_contain_hard_newline)
16+
}
17+
AbstractLineToken::BreakableCallChainEntry(bcce) => {
18+
bcce.tokens().iter().any(tokens_contain_hard_newline)
19+
}
20+
AbstractLineToken::ConditionalLayoutEntry(cle) => cle.contains_hard_newline(),
21+
_ => false,
22+
}
23+
}
24+
1025
#[derive(Copy, Clone, Debug)]
1126
pub enum ConvertType {
1227
MultiLine,
@@ -171,12 +186,7 @@ impl<'src> BreakableEntry<'src> {
171186
}
172187

173188
fn contains_hard_newline(&self) -> bool {
174-
self.tokens.iter().any(|t| {
175-
matches!(
176-
t,
177-
AbstractLineToken::ConcreteLineToken(ConcreteLineToken::HardNewLine)
178-
)
179-
})
189+
self.tokens.iter().any(tokens_contain_hard_newline)
180190
}
181191

182192
pub fn single_line_len(&self) -> usize {
@@ -615,6 +625,16 @@ impl<'src> ConditionalLayoutEntry<'src> {
615625
|| self.predicate_tokens.iter().any(Self::token_is_multiline)
616626
}
617627

628+
pub fn contains_hard_newline(&self) -> bool {
629+
self.statement_tokens
630+
.iter()
631+
.any(tokens_contain_hard_newline)
632+
|| self
633+
.predicate_tokens
634+
.iter()
635+
.any(tokens_contain_hard_newline)
636+
}
637+
618638
fn token_is_multiline(token: &AbstractLineToken<'src>) -> bool {
619639
match token {
620640
AbstractLineToken::ConcreteLineToken(ConcreteLineToken::HardNewLine) => true,

0 commit comments

Comments
 (0)