Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8e6fd67

Browse files
committed
Fix up assoc indentation
1 parent 66aece0 commit 8e6fd67

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

bin/test

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ END {
3535
}
3636

3737
__END__
38-
assoc.rb:1
38+
assign.rb:6
39+
assoc.rb:4
3940
assoc.rb:8
4041
bare_assoc_hash.rb:4
42+
bare_assoc_hash.rb:5
4143
brace_block.rb:1
4244
break.rb:9
4345
call.rb:7
@@ -52,7 +54,6 @@ command_call.rb:3
5254
command_call.rb:4
5355
command_call.rb:7
5456
command_call.rb:9
55-
command_call.rb:10
5657
command_call.rb:11
5758
def.rb:6
5859
def_endless.rb:9
@@ -66,7 +67,7 @@ elsif.rb:3
6667
embdoc.rb:0
6768
embdoc.rb:1
6869
end_content.rb:0
69-
heredoc.rb:17
70+
hash.rb:6
7071
heredoc.rb:18
7172
if.rb:3
7273
if.rb:7

lib/syntax_tree/prism/formatter.rb

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -427,39 +427,12 @@ def format_prefix(operator_loc, value)
427427

428428
# foo = bar
429429
def format_write(operator_loc, value)
430-
indent_value = true
431-
current = value
432-
433-
# Here, determine whether or not we should attempt to indent the value of
434-
# the write. For certain nodes we can to avoid this because it will look
435-
# strange.
436-
until current.nil?
437-
case current.type
438-
when :array_node
439-
indent_value = current.opening_loc.nil?
440-
break
441-
when :hash_node, :lambda_node
442-
indent_value = false
443-
break
444-
when :string_node, :x_string_node, :interpolated_string_node, :interpolated_x_string_node
445-
indent_value = current.opening_loc ? !current.opening.start_with?("<<") : true
446-
break
447-
when :call_node
448-
current = current.receiver
449-
when :interpolated_symbol_node
450-
indent_value = current.opening_loc ? !current.opening.start_with?("%s") : true
451-
break
452-
else
453-
break
454-
end
455-
end
456-
457430
group do
458431
yield
459432
text(" ")
460433
loc(operator_loc)
461434

462-
if indent_value
435+
if value.indent_write?
463436
indent do
464437
breakable_space
465438
format(value)

lib/syntax_tree/prism/nodes.rb

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ def contains_write?
4242

4343
false
4444
end
45+
46+
def indent_write?
47+
case type
48+
when :array_node
49+
opening_loc.nil?
50+
when :hash_node, :lambda_node
51+
false
52+
when :string_node, :x_string_node, :interpolated_string_node, :interpolated_x_string_node
53+
opening_loc ? !opening.start_with?("<<") : true
54+
when :call_node
55+
receiver ? receiver.indent_write? : true
56+
when :interpolated_symbol_node
57+
opening_loc ? !opening.start_with?("%s") : true
58+
else
59+
true
60+
end
61+
end
4562
end
4663

4764
class AliasGlobalVariableNode
@@ -242,14 +259,29 @@ def format(q)
242259
if operator_loc
243260
q.format(key)
244261
q.text(" #{operator}")
245-
q.breakable_space
246-
q.format(value)
262+
263+
if value.indent_write?
264+
q.indent do
265+
q.breakable_space
266+
q.format(value)
267+
end
268+
else
269+
q.text(" ")
270+
q.format(value)
271+
end
247272
else
248273
q.format(key)
249274

250275
if value && !value.is_a?(ImplicitNode)
251-
q.text(" ")
252-
q.format(value)
276+
if value.indent_write?
277+
q.indent do
278+
q.breakable_space
279+
q.format(value)
280+
end
281+
else
282+
q.text(" ")
283+
q.format(value)
284+
end
253285
end
254286
end
255287
end

0 commit comments

Comments
 (0)