@@ -816,6 +816,29 @@ def format(q)
816
816
end
817
817
end
818
818
819
+ class EmptyWithCommentsFormatter
820
+ # [LBracket] the opening bracket
821
+ attr_reader :lbracket
822
+
823
+ def initialize ( lbracket )
824
+ @lbracket = lbracket
825
+ end
826
+
827
+ def format ( q )
828
+ q . group do
829
+ q . text ( "[" )
830
+ q . indent do
831
+ lbracket . comments . each do |comment |
832
+ q . breakable ( force : true )
833
+ comment . format ( q )
834
+ end
835
+ end
836
+ q . breakable ( force : true )
837
+ q . text ( "]" )
838
+ end
839
+ end
840
+ end
841
+
819
842
# [LBracket] the bracket that opens this array
820
843
attr_reader :lbracket
821
844
@@ -867,6 +890,11 @@ def format(q)
867
890
return
868
891
end
869
892
893
+ if empty_with_comments?
894
+ EmptyWithCommentsFormatter . new ( lbracket ) . format ( q )
895
+ return
896
+ end
897
+
870
898
q . group do
871
899
q . format ( lbracket )
872
900
@@ -919,6 +947,12 @@ def var_refs?(q)
919
947
q . maxwidth * 2
920
948
)
921
949
end
950
+
951
+ # If we have an empty array that contains only comments, then we're going
952
+ # to do some special printing to ensure they get indented correctly.
953
+ def empty_with_comments?
954
+ contents . nil? && lbracket . comments . any? && lbracket . comments . none? ( &:inline? )
955
+ end
922
956
end
923
957
924
958
# AryPtn represents matching against an array pattern using the Ruby 2.7+
@@ -4304,6 +4338,29 @@ def format(q)
4304
4338
# { key => value }
4305
4339
#
4306
4340
class HashLiteral < Node
4341
+ class EmptyWithCommentsFormatter
4342
+ # [LBrace] the opening brace
4343
+ attr_reader :lbrace
4344
+
4345
+ def initialize ( lbrace )
4346
+ @lbrace = lbrace
4347
+ end
4348
+
4349
+ def format ( q )
4350
+ q . group do
4351
+ q . text ( "{" )
4352
+ q . indent do
4353
+ lbrace . comments . each do |comment |
4354
+ q . breakable ( force : true )
4355
+ comment . format ( q )
4356
+ end
4357
+ end
4358
+ q . breakable ( force : true )
4359
+ q . text ( "}" )
4360
+ end
4361
+ end
4362
+ end
4363
+
4307
4364
# [LBrace] the left brace that opens this hash
4308
4365
attr_reader :lbrace
4309
4366
@@ -4348,7 +4405,18 @@ def format_key(q, key)
4348
4405
4349
4406
private
4350
4407
4408
+ # If we have an empty hash that contains only comments, then we're going
4409
+ # to do some special printing to ensure they get indented correctly.
4410
+ def empty_with_comments?
4411
+ assocs . empty? && lbrace . comments . any? && lbrace . comments . none? ( &:inline? )
4412
+ end
4413
+
4351
4414
def format_contents ( q )
4415
+ if empty_with_comments?
4416
+ EmptyWithCommentsFormatter . new ( lbrace ) . format ( q )
4417
+ return
4418
+ end
4419
+
4352
4420
q . format ( lbrace )
4353
4421
4354
4422
if assocs . empty?
@@ -7577,19 +7645,20 @@ def attach_comments(start_char, end_char)
7577
7645
comment = parser_comments [ comment_index ]
7578
7646
location = comment . location
7579
7647
7580
- if !comment . inline? && ( start_char <= location . start_char ) &&
7581
- ( end_char >= location . end_char ) && !comment . ignore?
7582
- parser_comments . delete_at ( comment_index )
7583
-
7584
- while ( node = body [ body_index ] ) &&
7585
- (
7586
- node . is_a? ( VoidStmt ) ||
7587
- node . location . start_char < location . start_char
7588
- )
7648
+ if !comment . inline? && ( start_char <= location . start_char ) && ( end_char >= location . end_char ) && !comment . ignore?
7649
+ while ( node = body [ body_index ] ) && ( node . is_a? ( VoidStmt ) || node . location . start_char < location . start_char )
7589
7650
body_index += 1
7590
7651
end
7591
7652
7592
- body . insert ( body_index , comment )
7653
+ if body_index != 0 && body [ body_index - 1 ] . location . start_char < location . start_char && body [ body_index - 1 ] . location . end_char > location . start_char
7654
+ # The previous node entirely encapsules the comment, so we don't
7655
+ # want to attach it here since it will get attached normally. This
7656
+ # is mostly in the case of hash and array literals.
7657
+ comment_index += 1
7658
+ else
7659
+ parser_comments . delete_at ( comment_index )
7660
+ body . insert ( body_index , comment )
7661
+ end
7593
7662
else
7594
7663
comment_index += 1
7595
7664
end
0 commit comments