Skip to content

Commit 3fd2134

Browse files
authored
[fix] Properly indent case/in statements (#887)
* Add test fixtures * [fix] Properly indent case/in statements
1 parent 1be9136 commit 3fd2134

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module App
2+
config = {db: {user: 'admin', password: 'abc123'}}
3+
4+
case config
5+
in {db: {user:}}
6+
puts "Connect with user '#{user}'"
7+
in {connection: {username: }}
8+
puts "Connect with user '#{username}'"
9+
else
10+
puts "Unrecognized structure of config"
11+
end
12+
end
13+
14+
class Foo
15+
def bar(value)
16+
case value
17+
in 1
18+
"one"
19+
in 2
20+
"two"
21+
end
22+
end
23+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module App
2+
config = {db: {user: "admin", password: "abc123"}}
3+
4+
case config
5+
in {db: {user:}}
6+
puts "Connect with user '#{user}'"
7+
in {connection: {username:}}
8+
puts "Connect with user '#{username}'"
9+
else
10+
puts "Unrecognized structure of config"
11+
end
12+
end
13+
14+
class Foo
15+
def bar(value)
16+
case value
17+
in 1
18+
"one"
19+
in 2
20+
"two"
21+
end
22+
end
23+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
case 1
2+
in 2 # Two
3+
in 3 # Three
4+
end
5+
6+
case value
7+
in :a # comment a
8+
do_a
9+
in :b # comment b
10+
do_b
11+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
case 1
2+
# Two
3+
in 2
4+
# Three
5+
in 3
6+
end
7+
8+
case value
9+
# comment a
10+
in :a
11+
do_a
12+
# comment b
13+
in :b
14+
do_b
15+
end

librubyfmt/src/format_prism.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,11 @@ fn format_case_match_node<'src>(
588588
ps.emit_newline();
589589
ps.with_start_of_line(true, |ps| {
590590
for condition in case_match_node.conditions().iter() {
591+
// We keep start_of_line false and handle indentation here
592+
// so that format_node's trailing emit_newline doesn't insert
593+
// a blank line between consecutive `in` clauses.
594+
ps.at_offset(condition.location().start_offset());
595+
ps.emit_indent();
591596
ps.with_start_of_line(false, |ps| format_node(ps, condition));
592597
}
593598

0 commit comments

Comments
 (0)