@@ -328,7 +328,19 @@ def deconstruct_keys(_keys)
328328 def format ( q )
329329 q . text ( "__END__" )
330330 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 " )
332344 end
333345 end
334346
@@ -792,6 +804,17 @@ def format(q)
792804 # [one, two, three]
793805 #
794806 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+
795818 # Formats an array of multiple simple string literals into the %w syntax.
796819 class QWordsFormatter
797820 # [Args] the contents of the array
@@ -806,7 +829,7 @@ def format(q)
806829 q . group do
807830 q . indent do
808831 q . breakable_empty
809- q . seplist ( contents . parts , Formatter :: BREAKABLE_SPACE_SEPARATOR ) do |part |
832+ q . seplist ( contents . parts , BREAKABLE_SPACE_SEPARATOR ) do |part |
810833 if part . is_a? ( StringLiteral )
811834 q . format ( part . parts . first )
812835 else
@@ -834,7 +857,7 @@ def format(q)
834857 q . group do
835858 q . indent do
836859 q . breakable_empty
837- q . seplist ( contents . parts , Formatter :: BREAKABLE_SPACE_SEPARATOR ) do |part |
860+ q . seplist ( contents . parts , BREAKABLE_SPACE_SEPARATOR ) do |part |
838861 q . format ( part . value )
839862 end
840863 end
@@ -4034,9 +4057,19 @@ def format(q)
40344057 parts . each do |part |
40354058 if part . is_a? ( TStringContent )
40364059 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 )
40394070 end
4071+
4072+ q . breakable_return if value . end_with? ( "\n " )
40404073 else
40414074 q . format ( part )
40424075 end
@@ -4957,30 +4990,32 @@ def deconstruct_keys(_keys)
49574990
49584991 # This is a very specific behavior where you want to force a newline, but
49594992 # 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 )
49714994
49724995 def format ( q )
49734996 q . group do
49744997 q . format ( beginning )
49754998
49764999 q . line_suffix ( priority : Formatter ::HEREDOC_PRIORITY ) do
49775000 q . group do
4978- SEPARATOR . call ( q )
5001+ q . target << SEPARATOR
49795002
49805003 parts . each do |part |
49815004 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 " )
49845019 else
49855020 q . format ( part )
49865021 end
@@ -7295,7 +7330,7 @@ def format(q)
72957330 q . group do
72967331 q . indent do
72977332 q . breakable_empty
7298- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
7333+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
72997334 q . format ( element )
73007335 end
73017336 end
@@ -7388,7 +7423,7 @@ def format(q)
73887423 q . group do
73897424 q . indent do
73907425 q . breakable_empty
7391- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
7426+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
73927427 q . format ( element )
73937428 end
73947429 end
@@ -8626,9 +8661,19 @@ def format(q)
86268661 parts . each do |part |
86278662 if part . is_a? ( TStringContent )
86288663 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 )
86318674 end
8675+
8676+ q . breakable_return if value . end_with? ( "\n " )
86328677 else
86338678 q . format ( part )
86348679 end
@@ -8845,7 +8890,7 @@ def format(q)
88458890 q . group do
88468891 q . indent do
88478892 q . breakable_empty
8848- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
8893+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
88498894 q . format ( element )
88508895 end
88518896 end
@@ -10184,7 +10229,7 @@ def format(q)
1018410229 q . group do
1018510230 q . indent do
1018610231 q . breakable_empty
10187- q . seplist ( elements , Formatter ::BREAKABLE_SPACE_SEPARATOR ) do |element |
10232+ q . seplist ( elements , ArrayLiteral ::BREAKABLE_SPACE_SEPARATOR ) do |element |
1018810233 q . format ( element )
1018910234 end
1019010235 end
0 commit comments