@@ -123,7 +123,7 @@ def to_json(*opts)
123123 end
124124
125125 def construct_keys
126- PP . format ( +"" ) { |q | Visitor ::MatchVisitor . new ( q ) . visit ( self ) }
126+ PrettierPrint . format ( +"" ) { |q | Visitor ::MatchVisitor . new ( q ) . visit ( self ) }
127127 end
128128 end
129129
@@ -1666,52 +1666,6 @@ def format(q)
16661666 end
16671667 end
16681668
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-
17151669 # BlockVar represents the parameters being declared for a block. Effectively
17161670 # this node is everything contained within the pipes. This includes all of the
17171671 # various parameter types, as well as block-local variable declarations.
@@ -1752,8 +1706,7 @@ def deconstruct_keys(_keys)
17521706
17531707 def format ( q )
17541708 q . group ( 0 , "|" , "|" ) do
1755- doc = q . format ( params )
1756- RemoveBreaks . call ( doc )
1709+ q . remove_breaks ( q . format ( params ) )
17571710
17581711 if locals . any?
17591712 q . text ( "; " )
@@ -3096,31 +3049,6 @@ def format(q)
30963049
30973050 private
30983051
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-
31243052 def argument_alignment ( q , doc )
31253053 # Very special handling case for rspec matchers. In general with rspec
31263054 # matchers you expect to see something like:
@@ -3138,7 +3066,7 @@ def argument_alignment(q, doc)
31383066 if %w[ to not_to to_not ] . include? ( message . value )
31393067 0
31403068 else
3141- width = doc_width ( doc ) + 1
3069+ width = q . last_position ( doc ) + 1
31423070 width > ( q . maxwidth / 2 ) ? 0 : width
31433071 end
31443072 end
@@ -4891,17 +4819,9 @@ def deconstruct_keys(_keys)
48914819 end
48924820
48934821 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 ) }
49054825
49064826 q . group do
49074827 q . format ( beginning )
@@ -5325,9 +5245,8 @@ def format_ternary(q)
53255245 # force it into the output but we _don't_ want to explicitly
53265246 # break the parent. If a break-parent shows up in the tree, then
53275247 # 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 )
53315250 q . format ( node . consequent . statements )
53325251 end
53335252 end
@@ -8314,8 +8233,7 @@ def format(q)
83148233 # same line in the source, then we're going to leave them in place and
83158234 # assume that's the way the developer wanted this expression
83168235 # represented.
8317- doc = q . group ( 0 , '#{' , "}" ) { q . format ( statements ) }
8318- RemoveBreaks . call ( doc )
8236+ q . remove_breaks ( q . group ( 0 , '#{' , "}" ) { q . format ( statements ) } )
83198237 else
83208238 q . group do
83218239 q . text ( '#{' )
0 commit comments