Skip to content

Commit 20007cd

Browse files
authored
[fix] Properly multiline multi-statement blocks in string embexprs (#897)
* Fixtures * [fix] Properly multiline multi-statement blocks in string embexprs
1 parent c891abd commit 20007cd

4 files changed

Lines changed: 96 additions & 4 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"value: #{proc { a; b }.call}"
2+
3+
"value: #{-> { a; b }.call}"
4+
5+
"value: #{lambda { a; b }.call}"
6+
7+
"value: #{foo { a; b }.call}"
8+
9+
"value: #{proc { a; b; c }.call}"
10+
11+
class Foo
12+
def bar
13+
"value: #{proc { a; b }.call}"
14+
end
15+
16+
def baz
17+
if condition
18+
"nested: #{lambda { x; y; z }.call}"
19+
end
20+
end
21+
end
22+
23+
"value: #{proc {
24+
a # first
25+
b # second
26+
}.call}"
27+
28+
"value: #{proc {
29+
# leading comment
30+
a
31+
b
32+
}.call}"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"value: #{proc {
2+
a
3+
b
4+
}.call}"
5+
6+
"value: #{-> {
7+
a
8+
b
9+
}.call}"
10+
11+
"value: #{lambda {
12+
a
13+
b
14+
}.call}"
15+
16+
"value: #{foo {
17+
a
18+
b
19+
}.call}"
20+
21+
"value: #{proc {
22+
a
23+
b
24+
c
25+
}.call}"
26+
27+
class Foo
28+
def bar
29+
"value: #{proc {
30+
a
31+
b
32+
}.call}"
33+
end
34+
35+
def baz
36+
if condition
37+
"nested: #{lambda {
38+
x
39+
y
40+
z
41+
}.call}"
42+
end
43+
end
44+
end
45+
46+
"value: #{proc {
47+
# first
48+
a
49+
# second
50+
b
51+
}.call}"
52+
53+
"value: #{proc {
54+
# leading comment
55+
a
56+
b
57+
}.call}"

librubyfmt/src/render_queue_writer.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,12 @@ impl<'src> RenderQueueWriter<'src> {
198198
fn format_breakable_entry(accum: &mut Intermediary<'src>, be: BreakableEntry<'src>) {
199199
// We generally will force expressions embedded in strings to be on a single line,
200200
// but if that expression has a heredoc nested in it, we should let it render across lines
201-
// so that the collapsing newlines render properly.
202-
let force_single_line =
203-
!be.any_collapsing_newline_has_heredoc_content() && be.in_string_embexpr();
201+
// so that the collapsing newlines render properly. Similarly, if the entry already
202+
// contains hard newlines (e.g. a multi-statement brace block), forcing single-line
203+
// would leave dangling newlines and squash statements together.
204+
let force_single_line = be.in_string_embexpr()
205+
&& !be.any_collapsing_newline_has_heredoc_content()
206+
&& !be.contains_hard_newline();
204207

205208
let mut tokens = Vec::new();
206209
if !force_single_line

librubyfmt/src/render_targets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'src> BreakableEntry<'src> {
185185
self.in_string_embexpr
186186
}
187187

188-
fn contains_hard_newline(&self) -> bool {
188+
pub fn contains_hard_newline(&self) -> bool {
189189
self.tokens.iter().any(tokens_contain_hard_newline)
190190
}
191191

0 commit comments

Comments
 (0)