@@ -328,7 +328,19 @@ def deconstruct_keys(_keys)
328
328
def format ( q )
329
329
q . text ( "__END__" )
330
330
q . breakable_force
331
- q . seplist ( value . split ( /\r ?\n / , -1 ) , Formatter ::BREAKABLE_RETURN_SEPARATOR ) { |line | q . text ( line ) }
331
+
332
+ first = true
333
+ value . each_line ( chomp : true ) do |line |
334
+ if first
335
+ first = false
336
+ else
337
+ q . breakable_return
338
+ end
339
+
340
+ q . text ( line )
341
+ end
342
+
343
+ q . breakable_return if value . end_with? ( "\n " )
332
344
end
333
345
end
334
346
@@ -792,6 +804,17 @@ def format(q)
792
804
# [one, two, three]
793
805
#
794
806
class ArrayLiteral < Node
807
+ # It's very common to use seplist with ->(q) { q.breakable_space }. We wrap
808
+ # that pattern into an object to cut down on having to create a bunch of
809
+ # lambdas all over the place.
810
+ class BreakableSpaceSeparator
811
+ def call ( q )
812
+ q . breakable_space
813
+ end
814
+ end
815
+
816
+ BREAKABLE_SPACE_SEPARATOR = BreakableSpaceSeparator . new
817
+
795
818
# Formats an array of multiple simple string literals into the %w syntax.
796
819
class QWordsFormatter
797
820
# [Args] the contents of the array
@@ -806,7 +829,7 @@ def format(q)
806
829
q . group do
807
830
q . indent do
808
831
q . breakable_empty
809
- q . seplist ( contents . parts , Formatter :: BREAKABLE_SPACE_SEPARATOR ) do |part |
832
+ q . seplist ( contents . parts , BREAKABLE_SPACE_SEPARATOR ) do |part |
810
833
if part . is_a? ( StringLiteral )
811
834
q . format ( part . parts . first )
812
835
else
@@ -834,7 +857,7 @@ def format(q)
834
857
q . group do
835
858
q . indent do
836
859
q . breakable_empty
837
- q . seplist ( contents . parts , Formatter :: BREAKABLE_SPACE_SEPARATOR ) do |part |
860
+ q . seplist ( contents . parts , BREAKABLE_SPACE_SEPARATOR ) do |part |
838
861
q . format ( part . value )
839
862
end
840
863
end
@@ -4034,9 +4057,19 @@ def format(q)
4034
4057
parts . each do |part |
4035
4058
if part . is_a? ( TStringContent )
4036
4059
value = Quotes . normalize ( part . value , closing_quote )
4037
- q . seplist ( value . split ( /\r ?\n / , -1 ) , Formatter ::BREAKABLE_RETURN_SEPARATOR ) do |text |
4038
- q . text ( text )
4060
+ first = true
4061
+
4062
+ value . each_line ( chomp : true ) do |line |
4063
+ if first
4064
+ first = false
4065
+ else
4066
+ q . breakable_return
4067
+ end
4068
+
4069
+ q . text ( line )
4039
4070
end
4071
+
4072
+ q . breakable_return if value . end_with? ( "\n " )
4040
4073
else
4041
4074
q . format ( part )
4042
4075
end
@@ -4957,30 +4990,32 @@ def deconstruct_keys(_keys)
4957
4990
4958
4991
# This is a very specific behavior where you want to force a newline, but
4959
4992
# don't want to force the break parent.
4960
- class Separator
4961
- DOC = PrettierPrint ::Breakable . new ( " " , 1 , indent : false , force : true )
4962
-
4963
- def call ( q )
4964
- q . target << DOC
4965
- end
4966
- end
4967
-
4968
- # We're going to keep an instance around so we don't have to allocate a new
4969
- # one every time we format a heredoc.
4970
- SEPARATOR = Separator . new
4993
+ SEPARATOR = PrettierPrint ::Breakable . new ( " " , 1 , indent : false , force : true )
4971
4994
4972
4995
def format ( q )
4973
4996
q . group do
4974
4997
q . format ( beginning )
4975
4998
4976
4999
q . line_suffix ( priority : Formatter ::HEREDOC_PRIORITY ) do
4977
5000
q . group do
4978
- SEPARATOR . call ( q )
5001
+ q . target << SEPARATOR
4979
5002
4980
5003
parts . each do |part |
4981
5004
if part . is_a? ( TStringContent )
4982
- texts = part . value . split ( /\r ?\n / , -1 )
4983
- q . seplist ( texts , SEPARATOR ) { |text | q . text ( text ) }
5005
+ value = part . value
5006
+ first = true
5007
+
5008
+ value . each_line ( chomp : true ) do |line |
5009
+ if first
5010
+ first = false
5011
+ else
5012
+ q . target << SEPARATOR
5013
+ end
5014
+
5015
+ q . text ( line )
5016
+ end
5017
+
5018
+ q . target << SEPARATOR if value . end_with? ( "\n " )
4984
5019
else
4985
5020
q . format ( part )
4986
5021
end
@@ -7295,7 +7330,7 @@ def format(q)
7295
7330
q . group do
7296
7331
q . indent do
7297
7332
q . breakable_empty
7298
- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
7333
+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
7299
7334
q . format ( element )
7300
7335
end
7301
7336
end
@@ -7388,7 +7423,7 @@ def format(q)
7388
7423
q . group do
7389
7424
q . indent do
7390
7425
q . breakable_empty
7391
- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
7426
+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
7392
7427
q . format ( element )
7393
7428
end
7394
7429
end
@@ -8626,9 +8661,19 @@ def format(q)
8626
8661
parts . each do |part |
8627
8662
if part . is_a? ( TStringContent )
8628
8663
value = Quotes . normalize ( part . value , closing_quote )
8629
- q . seplist ( value . split ( /\r ?\n / , -1 ) , Formatter ::BREAKABLE_RETURN_SEPARATOR ) do |text |
8630
- q . text ( text )
8664
+ first = true
8665
+
8666
+ value . each_line ( chomp : true ) do |line |
8667
+ if first
8668
+ first = false
8669
+ else
8670
+ q . breakable_return
8671
+ end
8672
+
8673
+ q . text ( line )
8631
8674
end
8675
+
8676
+ q . breakable_return if value . end_with? ( "\n " )
8632
8677
else
8633
8678
q . format ( part )
8634
8679
end
@@ -8845,7 +8890,7 @@ def format(q)
8845
8890
q . group do
8846
8891
q . indent do
8847
8892
q . breakable_empty
8848
- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
8893
+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
8849
8894
q . format ( element )
8850
8895
end
8851
8896
end
@@ -10184,7 +10229,7 @@ def format(q)
10184
10229
q . group do
10185
10230
q . indent do
10186
10231
q . breakable_empty
10187
- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
10232
+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
10188
10233
q . format ( element )
10189
10234
end
10190
10235
end
0 commit comments