@@ -123,7 +123,7 @@ def to_json(*opts)
123
123
end
124
124
125
125
def construct_keys
126
- PP . format ( +"" ) { |q | Visitor ::MatchVisitor . new ( q ) . visit ( self ) }
126
+ PrettierPrint . format ( +"" ) { |q | Visitor ::MatchVisitor . new ( q ) . visit ( self ) }
127
127
end
128
128
end
129
129
@@ -1666,52 +1666,6 @@ def format(q)
1666
1666
end
1667
1667
end
1668
1668
1669
- # This module will remove any breakables from the list of contents so that no
1670
- # newlines are present in the output.
1671
- module RemoveBreaks
1672
- class << self
1673
- def call ( doc )
1674
- marker = Object . new
1675
- stack = [ doc ]
1676
-
1677
- while stack . any?
1678
- doc = stack . pop
1679
-
1680
- if doc == marker
1681
- stack . pop
1682
- next
1683
- end
1684
-
1685
- stack += [ doc , marker ]
1686
-
1687
- case doc
1688
- when PrettyPrint ::Align , PrettyPrint ::Indent , PrettyPrint ::Group
1689
- doc . contents . map! { |child | remove_breaks ( child ) }
1690
- stack += doc . contents . reverse
1691
- when PrettyPrint ::IfBreak
1692
- doc . flat_contents . map! { |child | remove_breaks ( child ) }
1693
- stack += doc . flat_contents . reverse
1694
- end
1695
- end
1696
- end
1697
-
1698
- private
1699
-
1700
- def remove_breaks ( doc )
1701
- case doc
1702
- when PrettyPrint ::Breakable
1703
- text = PrettyPrint ::Text . new
1704
- text . add ( object : doc . force? ? "; " : doc . separator , width : doc . width )
1705
- text
1706
- when PrettyPrint ::IfBreak
1707
- PrettyPrint ::Align . new ( indent : 0 , contents : doc . flat_contents )
1708
- else
1709
- doc
1710
- end
1711
- end
1712
- end
1713
- end
1714
-
1715
1669
# BlockVar represents the parameters being declared for a block. Effectively
1716
1670
# this node is everything contained within the pipes. This includes all of the
1717
1671
# various parameter types, as well as block-local variable declarations.
@@ -1752,8 +1706,7 @@ def deconstruct_keys(_keys)
1752
1706
1753
1707
def format ( q )
1754
1708
q . group ( 0 , "|" , "|" ) do
1755
- doc = q . format ( params )
1756
- RemoveBreaks . call ( doc )
1709
+ q . remove_breaks ( q . format ( params ) )
1757
1710
1758
1711
if locals . any?
1759
1712
q . text ( "; " )
@@ -3096,31 +3049,6 @@ def format(q)
3096
3049
3097
3050
private
3098
3051
3099
- # This is a somewhat naive method that is attempting to sum up the width of
3100
- # the doc nodes that make up the given doc node. This is used to align
3101
- # content.
3102
- def doc_width ( parent )
3103
- queue = [ parent ]
3104
- width = 0
3105
-
3106
- until queue . empty?
3107
- doc = queue . shift
3108
-
3109
- case doc
3110
- when PrettyPrint ::Text
3111
- width += doc . width
3112
- when PrettyPrint ::Indent , PrettyPrint ::Align , PrettyPrint ::Group
3113
- queue = doc . contents + queue
3114
- when PrettyPrint ::IfBreak
3115
- queue = doc . break_contents + queue
3116
- when PrettyPrint ::Breakable
3117
- width = 0
3118
- end
3119
- end
3120
-
3121
- width
3122
- end
3123
-
3124
3052
def argument_alignment ( q , doc )
3125
3053
# Very special handling case for rspec matchers. In general with rspec
3126
3054
# matchers you expect to see something like:
@@ -3138,7 +3066,7 @@ def argument_alignment(q, doc)
3138
3066
if %w[ to not_to to_not ] . include? ( message . value )
3139
3067
0
3140
3068
else
3141
- width = doc_width ( doc ) + 1
3069
+ width = q . last_position ( doc ) + 1
3142
3070
width > ( q . maxwidth / 2 ) ? 0 : width
3143
3071
end
3144
3072
end
@@ -4891,17 +4819,9 @@ def deconstruct_keys(_keys)
4891
4819
end
4892
4820
4893
4821
def format ( q )
4894
- # This is a very specific behavior that should probably be included in the
4895
- # prettyprint module. It's when you want to force a newline, but don't
4896
- # want to force the break parent.
4897
- breakable = -> do
4898
- q . target << PrettyPrint ::Breakable . new (
4899
- " " ,
4900
- 1 ,
4901
- indent : false ,
4902
- force : true
4903
- )
4904
- end
4822
+ # This is a very specific behavior where you want to force a newline, but
4823
+ # don't want to force the break parent.
4824
+ breakable = -> { q . breakable ( indent : false , force : :skip_break_parent ) }
4905
4825
4906
4826
q . group do
4907
4827
q . format ( beginning )
@@ -5325,9 +5245,8 @@ def format_ternary(q)
5325
5245
# force it into the output but we _don't_ want to explicitly
5326
5246
# break the parent. If a break-parent shows up in the tree, then
5327
5247
# it's going to force it all the way up to the tree, which is
5328
- # going to negate the ternary. Maybe this should be an option in
5329
- # prettyprint? As in force: :no_break_parent or something.
5330
- q . target << PrettyPrint ::Breakable . new ( " " , 1 , force : true )
5248
+ # going to negate the ternary.
5249
+ q . breakable ( force : :skip_break_parent )
5331
5250
q . format ( node . consequent . statements )
5332
5251
end
5333
5252
end
@@ -8314,8 +8233,7 @@ def format(q)
8314
8233
# same line in the source, then we're going to leave them in place and
8315
8234
# assume that's the way the developer wanted this expression
8316
8235
# represented.
8317
- doc = q . group ( 0 , '#{' , "}" ) { q . format ( statements ) }
8318
- RemoveBreaks . call ( doc )
8236
+ q . remove_breaks ( q . group ( 0 , '#{' , "}" ) { q . format ( statements ) } )
8319
8237
else
8320
8238
q . group do
8321
8239
q . text ( '#{' )
0 commit comments