Skip to content

Commit 9478bdf

Browse files
fables-talesclaude
andauthored
Fix multiline rescue exception formatting (#356) (#798)
* Fix multiline rescue exception formatting (#356) When rescue has many exception types that exceed the line length, format them across multiple lines with proper indentation: rescue OpenSSL::SSL::SSLError, OpenSSL::SSL::SSLError, OpenSSL::SSL::SSLError => exception Short lists still stay on one line: rescue A, B, C => e Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Add test case for long single-line rescue breaking Verifies that a long rescue line on a single line gets properly broken across multiple lines when it exceeds the line length limit. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 555f117 commit 9478bdf

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
begin
2+
rescue OpenSSL::SSL::SSLError,
3+
OpenSSL::SSL::SSLError,
4+
OpenSSL::SSL::SSLError,
5+
OpenSSL::SSL::SSLError,
6+
OpenSSL::SSL::SSLError,
7+
OpenSSL::SSL::SSLError,
8+
OpenSSL::SSL::SSLError,
9+
OpenSSL::SSL::SSLError => exception
10+
end
11+
12+
begin
13+
rescue A, B, C => e
14+
end
15+
16+
begin
17+
rescue StandardError => e
18+
end
19+
20+
begin
21+
rescue A, B
22+
end
23+
24+
begin
25+
rescue StandardError
26+
end
27+
28+
begin
29+
rescue OpenSSL::SSL::SSLError, OpenSSL::SSL::SSLError, OpenSSL::SSL::SSLError, OpenSSL::SSL::SSLError, OpenSSL::SSL::SSLError => exception
30+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
begin
2+
rescue OpenSSL::SSL::SSLError,
3+
OpenSSL::SSL::SSLError,
4+
OpenSSL::SSL::SSLError,
5+
OpenSSL::SSL::SSLError,
6+
OpenSSL::SSL::SSLError,
7+
OpenSSL::SSL::SSLError,
8+
OpenSSL::SSL::SSLError,
9+
OpenSSL::SSL::SSLError => exception
10+
end
11+
12+
begin
13+
rescue A, B, C => e
14+
end
15+
16+
begin
17+
rescue StandardError => e
18+
end
19+
20+
begin
21+
rescue A, B
22+
end
23+
24+
begin
25+
rescue StandardError
26+
end
27+
28+
begin
29+
rescue OpenSSL::SSL::SSLError,
30+
OpenSSL::SSL::SSLError,
31+
OpenSSL::SSL::SSLError,
32+
OpenSSL::SSL::SSLError,
33+
OpenSSL::SSL::SSLError => exception
34+
end

librubyfmt/src/format_prism.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,30 +4632,41 @@ fn format_rescue_node<'src>(ps: &mut ParserState<'src>, rescue_node: prism::Resc
46324632

46334633
ps.emit_keyword("rescue");
46344634
let exceptions = rescue_node.exceptions();
4635+
let reference = rescue_node.reference();
46354636
if !exceptions.is_empty() {
46364637
ps.with_start_of_line(false, |ps| {
4637-
ps.emit_space();
4638-
format_list_like_thing(
4639-
ps,
4640-
exceptions,
4641-
rescue_node
4642-
.exceptions()
4643-
.iter()
4644-
.last()
4645-
.unwrap()
4646-
.location()
4647-
.end_offset(),
4648-
true,
4649-
);
4650-
});
4651-
}
4638+
ps.inline_breakable_of(BreakableDelims::for_binary_op(), |ps| {
4639+
let exceptions_count = exceptions.len();
4640+
for (idx, exception) in exceptions.iter().enumerate() {
4641+
if idx == 0 {
4642+
// First exception: just a space after 'rescue', no indent
4643+
ps.emit_space();
4644+
format_node(ps, exception);
4645+
} else {
4646+
// Subsequent exceptions: indent on new line
4647+
ps.emit_soft_indent();
4648+
format_node(ps, exception);
4649+
}
46524650

4653-
if let Some(reference) = rescue_node.reference() {
4651+
if idx != exceptions_count - 1 {
4652+
ps.emit_comma();
4653+
ps.emit_soft_newline();
4654+
}
4655+
}
4656+
if let Some(ref_node) = reference {
4657+
ps.emit_space();
4658+
ps.emit_op("=>");
4659+
ps.emit_space();
4660+
format_node(ps, ref_node);
4661+
}
4662+
});
4663+
});
4664+
} else if let Some(ref_node) = reference {
46544665
ps.emit_space();
46554666
ps.emit_op("=>");
46564667
ps.emit_space();
46574668
ps.with_start_of_line(false, |ps| {
4658-
format_node(ps, reference);
4669+
format_node(ps, ref_node);
46594670
});
46604671
}
46614672

0 commit comments

Comments
 (0)