summaryrefslogtreecommitdiff
path: root/lib/prettyprint.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/prettyprint.rb')
-rw-r--r--lib/prettyprint.rb1241
1 files changed, 508 insertions, 733 deletions
diff --git a/lib/prettyprint.rb b/lib/prettyprint.rb
index b05da3f56f..44ca5e816f 100644
--- a/lib/prettyprint.rb
+++ b/lib/prettyprint.rb
@@ -1,802 +1,577 @@
-# $Id$
-
-=begin
-= PrettyPrint
-The class implements pretty printing algorithm.
-It finds line breaks and nice indentations for grouped structure.
-
-By default, the class assumes that primitive elements are strings and
-each byte in the strings have single column in width.
-But it can be used for other situasions
-by giving suitable arguments for some methods:
-newline object and space generation block for (({PrettyPrint.new})),
-optional width argument for (({PrettyPrint#text})),
-(({PrettyPrint#breakable})), etc.
-There are several candidates to use them:
-text formatting using proportional fonts,
-multibyte characters which has columns diffrent to number of bytes,
-non-string formatting, etc.
-
-== class methods
---- PrettyPrint.new([newline]) [{|width| ...}]
- creates a buffer for pretty printing.
-
- ((|newline|)) is used for line breaks.
- (({"\n"})) is used if it is not specified.
-
- The block is used to generate spaces.
- (({{|width| ' ' * width}})) is used if it is not given.
-
-== methods
---- text(obj[, width])
- adds ((|obj|)) as a text of ((|width|)) columns in width.
-
- If ((|width|)) is not specified, (({((|obj|)).length})) is used.
-
---- breakable([sep[, width]])
- tells "you can break a line here if necessary", and a
- ((|width|))-column text ((|sep|)) is inserted if a line is not
- broken at the point.
-
- If ((|sep|)) is not specified, (({" "})) is used.
-
- If ((|width|)) is not specified, (({((|sep|)).length})) is used.
- You will have to specify this when ((|sep|)) is a multibyte
- character, for example.
-
---- nest(indent) {...}
- increases left margin after newline with ((|indent|)) for line breaks added in the block.
-
---- group {...}
- groups line break hints added in the block.
- The line break hints are all to be breaked or not.
-
---- fill_group {...}
- groups line break hints added in the block.
- The each line break hints may be breaked or not differently.
-
---- format(out[, width])
- outputs buffered data to ((|out|)).
- It tries to restrict the line length to ((|width|)) but it may
- overflow.
+# frozen_string_literal: true
+#
+# This class implements a pretty printing algorithm. It finds line breaks and
+# nice indentations for grouped structure.
+#
+# By default, the class assumes that primitive elements are strings and each
+# byte in the strings have single column in width. But it can be used for
+# other situations by giving suitable arguments for some methods:
+# * newline object and space generation block for PrettyPrint.new
+# * optional width argument for PrettyPrint#text
+# * PrettyPrint#breakable
+#
+# There are several candidate uses:
+# * text formatting using proportional fonts
+# * multibyte characters which has columns different to number of bytes
+# * non-string formatting
+#
+# == Bugs
+# * Box based formatting?
+# * Other (better) model/algorithm?
+#
+# Report any bugs at http://bugs.ruby-lang.org
+#
+# == References
+# Christian Lindig, Strictly Pretty, March 2000,
+# https://lindig.github.io/papers/strictly-pretty-2000.pdf
+#
+# Philip Wadler, A prettier printer, March 1998,
+# https://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
+#
+# == Author
+# Tanaka Akira <akr@fsij.org>
+#
+class PrettyPrint
- If ((|width|)) is not specified, 79 is assumed.
+ # The version string
+ VERSION = "0.2.0"
+
+ # This is a convenience method which is same as follows:
+ #
+ # begin
+ # q = PrettyPrint.new(output, maxwidth, newline, &genspace)
+ # ...
+ # q.flush
+ # output
+ # end
+ #
+ def PrettyPrint.format(output=''.dup, maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
+ q = PrettyPrint.new(output, maxwidth, newline, &genspace)
+ yield q
+ q.flush
+ output
+ end
- ((|out|)) must have a method named (({<<})) which accepts
- a first argument ((|obj|)) of (({PrettyPrint#text})),
- a first argument ((|sep|)) of (({PrettyPrint#breakable})),
- a first argument ((|newline|)) of (({PrettyPrint.new})),
- and
- a result of a given block for (({PrettyPrint.new})).
+ # This is similar to PrettyPrint::format but the result has no breaks.
+ #
+ # +maxwidth+, +newline+ and +genspace+ are ignored.
+ #
+ # The invocation of +breakable+ in the block doesn't break a line and is
+ # treated as just an invocation of +text+.
+ #
+ def PrettyPrint.singleline_format(output=''.dup, maxwidth=nil, newline=nil, genspace=nil)
+ q = SingleLine.new(output)
+ yield q
+ output
+ end
-== Bugs
-* Box based formatting? Other (better) model/algorithm?
+ # Creates a buffer for pretty printing.
+ #
+ # +output+ is an output target. If it is not specified, '' is assumed. It
+ # should have a << method which accepts the first argument +obj+ of
+ # PrettyPrint#text, the first argument +sep+ of PrettyPrint#breakable, the
+ # first argument +newline+ of PrettyPrint.new, and the result of a given
+ # block for PrettyPrint.new.
+ #
+ # +maxwidth+ specifies maximum line length. If it is not specified, 79 is
+ # assumed. However actual outputs may overflow +maxwidth+ if long
+ # non-breakable texts are provided.
+ #
+ # +newline+ is used for line breaks. "\n" is used if it is not specified.
+ #
+ # The block is used to generate spaces. {|width| ' ' * width} is used if it
+ # is not given.
+ #
+ def initialize(output=''.dup, maxwidth=79, newline="\n", &genspace)
+ @output = output
+ @maxwidth = maxwidth
+ @newline = newline
+ @genspace = genspace || lambda {|n| ' ' * n}
-== References
-Christian Lindig, Strictly Pretty, March 2000,
-((<URL:http://www.gaertner.de/~lindig/papers/strictly-pretty.html>))
+ @output_width = 0
+ @buffer_width = 0
+ @buffer = []
-Philip Wadler, A prettier printer, March 1998,
-((<URL:http://cm.bell-labs.com/cm/cs/who/wadler/topics/recent.html#prettier>))
+ root_group = Group.new(0)
+ @group_stack = [root_group]
+ @group_queue = GroupQueue.new(root_group)
+ @indent = 0
+ end
-=end
+ # The output object.
+ #
+ # This defaults to '', and should accept the << method
+ attr_reader :output
+
+ # The maximum width of a line, before it is separated in to a newline
+ #
+ # This defaults to 79, and should be an Integer
+ attr_reader :maxwidth
+
+ # The value that is appended to +output+ to add a new line.
+ #
+ # This defaults to "\n", and should be String
+ attr_reader :newline
+
+ # A lambda or Proc, that takes one argument, of an Integer, and returns
+ # the corresponding number of spaces.
+ #
+ # By default this is:
+ # lambda {|n| ' ' * n}
+ attr_reader :genspace
+
+ # The number of spaces to be indented
+ attr_reader :indent
+
+ # The PrettyPrint::GroupQueue of groups in stack to be pretty printed
+ attr_reader :group_queue
+
+ # Returns the group most recently added to the stack.
+ #
+ # Contrived example:
+ # out = ""
+ # => ""
+ # q = PrettyPrint.new(out)
+ # => #<PrettyPrint:0x82f85c0 @output="", @maxwidth=79, @newline="\n", @genspace=#<Proc:0x82f8368@/home/vbatts/.rvm/rubies/ruby-head/lib/ruby/2.0.0/prettyprint.rb:82 (lambda)>, @output_width=0, @buffer_width=0, @buffer=[], @group_stack=[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>], @group_queue=#<PrettyPrint::GroupQueue:0x82fb7c0 @queue=[[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>]]>, @indent=0>
+ # q.group {
+ # q.text q.current_group.inspect
+ # q.text q.newline
+ # q.group(q.current_group.depth + 1) {
+ # q.text q.current_group.inspect
+ # q.text q.newline
+ # q.group(q.current_group.depth + 1) {
+ # q.text q.current_group.inspect
+ # q.text q.newline
+ # q.group(q.current_group.depth + 1) {
+ # q.text q.current_group.inspect
+ # q.text q.newline
+ # }
+ # }
+ # }
+ # }
+ # => 284
+ # puts out
+ # #<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false>
+ # #<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false>
+ # #<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false>
+ # #<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
+ def current_group
+ @group_stack.last
+ end
-class PrettyPrint
- def initialize(newline="\n", &genspace)
- @newline = newline
- @genspace = genspace || lambda {|n| ' ' * n}
- @buf = Group.new
- @nest = [0]
- @stack = []
+ # Breaks the buffer into lines that are shorter than #maxwidth
+ def break_outmost_groups
+ while @maxwidth < @output_width + @buffer_width
+ return unless group = @group_queue.deq
+ until group.breakables.empty?
+ data = @buffer.shift
+ @output_width = data.output(@output, @output_width)
+ @buffer_width -= data.width
+ end
+ while !@buffer.empty? && Text === @buffer.first
+ text = @buffer.shift
+ @output_width = text.output(@output, @output_width)
+ @buffer_width -= text.width
+ end
+ end
end
+ # This adds +obj+ as a text of +width+ columns in width.
+ #
+ # If +width+ is not specified, obj.length is used.
+ #
def text(obj, width=obj.length)
- @buf << Text.new(obj, width)
+ if @buffer.empty?
+ @output << obj
+ @output_width += width
+ else
+ text = @buffer.last
+ unless Text === text
+ text = Text.new
+ @buffer << text
+ end
+ text.add(obj, width)
+ @buffer_width += width
+ break_outmost_groups
+ end
end
- def breakable(sep=' ', width=sep.length)
- @buf << Breakable.new(sep, width, @nest.last, @newline, @genspace)
+ # This is similar to #breakable except
+ # the decision to break or not is determined individually.
+ #
+ # Two #fill_breakable under a group may cause 4 results:
+ # (break,break), (break,non-break), (non-break,break), (non-break,non-break).
+ # This is different to #breakable because two #breakable under a group
+ # may cause 2 results:
+ # (break,break), (non-break,non-break).
+ #
+ # The text +sep+ is inserted if a line is not broken at this point.
+ #
+ # If +sep+ is not specified, " " is used.
+ #
+ # If +width+ is not specified, +sep.length+ is used. You will have to
+ # specify this when +sep+ is a multibyte character, for example.
+ #
+ def fill_breakable(sep=' ', width=sep.length)
+ group { breakable sep, width }
end
- def nest(indent)
- @nest << @nest.last + indent
- begin
- yield
- ensure
- @nest.pop
+ # This says "you can break a line here if necessary", and a +width+\-column
+ # text +sep+ is inserted if a line is not broken at the point.
+ #
+ # If +sep+ is not specified, " " is used.
+ #
+ # If +width+ is not specified, +sep.length+ is used. You will have to
+ # specify this when +sep+ is a multibyte character, for example.
+ #
+ def breakable(sep=' ', width=sep.length)
+ group = @group_stack.last
+ if group.break?
+ flush
+ @output << @newline
+ @output << @genspace.call(@indent)
+ @output_width = @indent
+ @buffer_width = 0
+ else
+ @buffer << Breakable.new(sep, width, self)
+ @buffer_width += width
+ break_outmost_groups
end
end
- def group
- g = Group.new
- @buf << g
- @stack << @buf
- @buf = g
+ # Groups line break hints added in the block. The line break hints are all
+ # to be used or not.
+ #
+ # If +indent+ is specified, the method call is regarded as nested by
+ # nest(indent) { ... }.
+ #
+ # If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
+ # before grouping. If +close_obj+ is specified, <tt>text close_obj,
+ # close_width</tt> is called after grouping.
+ #
+ def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
+ text open_obj, open_width
+ group_sub {
+ nest(indent) {
+ yield
+ }
+ }
+ text close_obj, close_width
+ end
+
+ # Takes a block and queues a new group that is indented 1 level further.
+ def group_sub
+ group = Group.new(@group_stack.last.depth + 1)
+ @group_stack.push group
+ @group_queue.enq group
begin
yield
ensure
- @buf = @stack.pop
+ @group_stack.pop
+ if group.breakables.empty?
+ @group_queue.delete group
+ end
end
end
- def fill_group
- g = FillGroup.new
- @buf << g
- @stack << @buf
- @buf = g
+ # Increases left margin after newline with +indent+ for line breaks added in
+ # the block.
+ #
+ def nest(indent)
+ @indent += indent
begin
yield
ensure
- @buf = @stack.pop
+ @indent -= indent
end
end
- def format(out, width=79)
- tails = [[-1, 0]]
- @buf.update_tails(tails, 0)
- @buf.multiline_output(out, 0, 0, width)
+ # outputs buffered data.
+ #
+ def flush
+ @buffer.each {|data|
+ @output_width = data.output(@output, @output_width)
+ }
+ @buffer.clear
+ @buffer_width = 0
end
- class Text
- def initialize(text, width)
- @text = text
- @width = width
- end
-
- def update_tails(tails, group)
- @tail = tails[-1][1]
- tails[-1][1] += @width
+ # The Text class is the means by which to collect strings from objects.
+ #
+ # This class is intended for internal use of the PrettyPrint buffers.
+ class Text # :nodoc:
+
+ # Creates a new text object.
+ #
+ # This constructor takes no arguments.
+ #
+ # The workflow is to append a PrettyPrint::Text object to the buffer, and
+ # being able to call the buffer.last() to reference it.
+ #
+ # As there are objects, use PrettyPrint::Text#add to include the objects
+ # and the width to utilized by the String version of this object.
+ def initialize
+ @objs = []
+ @width = 0
end
- attr_reader :tail
- def singleline_width
- return @width
- end
+ # The total width of the objects included in this Text object.
+ attr_reader :width
- def singleline_output(out)
- out << @text
+ # Render the String text of the objects that have been added to this Text object.
+ #
+ # Output the text to +out+, and increment the width to +output_width+
+ def output(out, output_width)
+ @objs.each {|obj| out << obj}
+ output_width + @width
end
- def multiline_output(out, group, margin, width)
- singleline_output(out)
- return margin + singleline_width
+ # Include +obj+ in the objects to be pretty printed, and increment
+ # this Text object's total width by +width+
+ def add(obj, width)
+ @objs << obj
+ @width += width
end
end
- class Breakable
- def initialize(sep, width, indent, newline, genspace)
- @sep = sep
+ # The Breakable class is used for breaking up object information
+ #
+ # This class is intended for internal use of the PrettyPrint buffers.
+ class Breakable # :nodoc:
+
+ # Create a new Breakable object.
+ #
+ # Arguments:
+ # * +sep+ String of the separator
+ # * +width+ Integer width of the +sep+
+ # * +q+ parent PrettyPrint object, to base from
+ def initialize(sep, width, q)
+ @obj = sep
@width = width
- @indent = indent
- @newline = newline
- @genspace = genspace
- end
-
- def update_tails(tails, group)
- @tail = tails[-1][1]
- if group == tails[-1][0]
- tails[-2][1] += @width + tails[-1][1]
- tails[-1][1] = 0
+ @pp = q
+ @indent = q.indent
+ @group = q.current_group
+ @group.breakables.push self
+ end
+
+ # Holds the separator String
+ #
+ # The +sep+ argument from ::new
+ attr_reader :obj
+
+ # The width of +obj+ / +sep+
+ attr_reader :width
+
+ # The number of spaces to indent.
+ #
+ # This is inferred from +q+ within PrettyPrint, passed in ::new
+ attr_reader :indent
+
+ # Render the String text of the objects that have been added to this
+ # Breakable object.
+ #
+ # Output the text to +out+, and increment the width to +output_width+
+ def output(out, output_width)
+ @group.breakables.shift
+ if @group.break?
+ out << @pp.newline
+ out << @pp.genspace.call(@indent)
+ @indent
else
- tails[-1][1] += @width
- tails << [group, 0]
+ @pp.group_queue.delete @group if @group.breakables.empty?
+ out << @obj
+ output_width + @width
end
end
- attr_reader :tail
-
- def singleline_width
- return @width
- end
-
- def singleline_output(out)
- out << @sep
- end
-
- def multiline_output(out, group, margin, width)
- out << @newline
- out << @genspace.call(@indent)
- return @indent
- end
end
- class Group
- def initialize
- @buf = []
- @singleline_width = nil
- end
-
- def <<(obj)
- @buf << obj
- end
-
- def update_tails(tails, group)
- @tail = tails[-1][1]
- len = 0
- @buf.reverse_each {|obj|
- obj.update_tails(tails, group + 1)
- len += obj.singleline_width
- }
- @singleline_width = len
- while group < tails[-1][0]
- tails[-2][1] += tails[-1][1]
- tails.pop
- end
- end
- attr_reader :tail
-
- def singleline_width
- return @singleline_width
- end
-
- def singleline_output(out)
- @buf.each {|obj| obj.singleline_output(out)}
- end
-
- def multiline_output(out, group, margin, width)
- if margin + singleline_width + @tail <= width
- singleline_output(out)
- margin += @singleline_width
+ # The Group class is used for making indentation easier.
+ #
+ # While this class does neither the breaking into newlines nor indentation,
+ # it is used in a stack (as well as a queue) within PrettyPrint, to group
+ # objects.
+ #
+ # For information on using groups, see PrettyPrint#group
+ #
+ # This class is intended for internal use of the PrettyPrint buffers.
+ class Group # :nodoc:
+ # Create a Group object
+ #
+ # Arguments:
+ # * +depth+ - this group's relation to previous groups
+ def initialize(depth)
+ @depth = depth
+ @breakables = []
+ @break = false
+ end
+
+ # This group's relation to previous groups
+ attr_reader :depth
+
+ # Array to hold the Breakable objects for this Group
+ attr_reader :breakables
+
+ # Makes a break for this Group, and returns true
+ def break
+ @break = true
+ end
+
+ # Boolean of whether this Group has made a break
+ def break?
+ @break
+ end
+
+ # Boolean of whether this Group has been queried for being first
+ #
+ # This is used as a predicate, and ought to be called first.
+ def first?
+ if defined? @first
+ false
else
- @buf.each {|obj|
- margin = obj.multiline_output(out, group + 1, margin, width)
- }
- end
- return margin
- end
- end
-
- class FillGroup < Group
- def multiline_output(out, group, margin, width)
- @buf.each {|obj|
- if margin + obj.singleline_width + obj.tail <= width
- obj.singleline_output(out)
- margin += obj.singleline_width
- else
- margin = obj.multiline_output(out, group + 1, margin, width)
- end
- }
- return margin
- end
- end
-end
-
-if __FILE__ == $0
- require 'runit/testcase'
- require 'runit/cui/testrunner'
-
- class WadlerExample < RUNIT::TestCase
- def setup
- @hello = PrettyPrint.new
- @hello.group {
- @hello.group {
- @hello.group {
- @hello.group {
- @hello.text 'hello'; @hello.breakable; @hello.text 'a'
- }
- @hello.breakable; @hello.text 'b'
- }
- @hello.breakable; @hello.text 'c'
- }
- @hello.breakable; @hello.text 'd'
- }
-
- @tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"),
- Tree.new("dd")),
- Tree.new("eee"),
- Tree.new("ffff", Tree.new("gg"),
- Tree.new("hhh"),
- Tree.new("ii")))
- end
-
- def test_hello_00_06
- expected = <<'End'.chomp
-hello
-a
-b
-c
-d
-End
- @hello.format(out='', 0); assert_equal(expected, out)
- @hello.format(out='', 6); assert_equal(expected, out)
- end
-
- def test_hello_07_08
- expected = <<'End'.chomp
-hello a
-b
-c
-d
-End
- @hello.format(out='', 7); assert_equal(expected, out)
- @hello.format(out='', 8); assert_equal(expected, out)
- end
-
- def test_hello_09_10
- expected = <<'End'.chomp
-hello a b
-c
-d
-End
- @hello.format(out='', 9); assert_equal(expected, out)
- @hello.format(out='', 10); assert_equal(expected, out)
- end
-
- def test_hello_11_12
- expected = <<'End'.chomp
-hello a b c
-d
-End
- @hello.format(out='', 11); assert_equal(expected, out)
- @hello.format(out='', 12); assert_equal(expected, out)
- end
-
- def test_hello_13
- expected = <<'End'.chomp
-hello a b c d
-End
- @hello.format(out='', 13); assert_equal(expected, out)
- end
-
- def test_tree_00_19
- pp = PrettyPrint.new
- @tree.show(pp)
- expected = <<'End'.chomp
-aaaa[bbbbb[ccc,
- dd],
- eee,
- ffff[gg,
- hhh,
- ii]]
-End
- pp.format(out='', 0); assert_equal(expected, out)
- pp.format(out='', 19); assert_equal(expected, out)
- end
-
- def test_tree_20_22
- pp = PrettyPrint.new
- @tree.show(pp)
- expected = <<'End'.chomp
-aaaa[bbbbb[ccc, dd],
- eee,
- ffff[gg,
- hhh,
- ii]]
-End
- pp.format(out='', 20); assert_equal(expected, out)
- pp.format(out='', 22); assert_equal(expected, out)
- end
-
- def test_tree_23_43
- pp = PrettyPrint.new
- @tree.show(pp)
- expected = <<'End'.chomp
-aaaa[bbbbb[ccc, dd],
- eee,
- ffff[gg, hhh, ii]]
-End
- pp.format(out='', 23); assert_equal(expected, out)
- pp.format(out='', 43); assert_equal(expected, out)
- end
-
- def test_tree_44
- pp = PrettyPrint.new
- @tree.show(pp)
- pp.format(out='', 44)
- assert_equal(<<'End'.chomp, out)
-aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]]
-End
- end
-
- def test_tree_alt_00_18
- pp = PrettyPrint.new
- @tree.altshow(pp)
- expected = <<'End'.chomp
-aaaa[
- bbbbb[
- ccc,
- dd
- ],
- eee,
- ffff[
- gg,
- hhh,
- ii
- ]
-]
-End
- pp.format(out='', 0); assert_equal(expected, out)
- pp.format(out='', 18); assert_equal(expected, out)
- end
-
- def test_tree_alt_19_20
- pp = PrettyPrint.new
- @tree.altshow(pp)
- expected = <<'End'.chomp
-aaaa[
- bbbbb[ ccc, dd ],
- eee,
- ffff[
- gg,
- hhh,
- ii
- ]
-]
-End
- pp.format(out='', 19); assert_equal(expected, out)
- pp.format(out='', 20); assert_equal(expected, out)
- end
-
- def test_tree_alt_20_49
- pp = PrettyPrint.new
- @tree.altshow(pp)
- expected = <<'End'.chomp
-aaaa[
- bbbbb[ ccc, dd ],
- eee,
- ffff[ gg, hhh, ii ]
-]
-End
- pp.format(out='', 21); assert_equal(expected, out)
- pp.format(out='', 49); assert_equal(expected, out)
- end
-
- def test_tree_alt_50
- pp = PrettyPrint.new
- @tree.altshow(pp)
- expected = <<'End'.chomp
-aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ]
-End
- pp.format(out='', 50); assert_equal(expected, out)
- end
-
- class Tree
- def initialize(string, *children)
- @string = string
- @children = children
- end
-
- def show(pp)
- pp.group {
- pp.text @string
- pp.nest(@string.length) {
- unless @children.empty?
- pp.text '['
- pp.nest(1) {
- first = true
- @children.each {|t|
- if first
- first = false
- else
- pp.text ','
- pp.breakable
- end
- t.show(pp)
- }
- }
- pp.text ']'
- end
- }
- }
+ @first = false
+ true
end
-
- def altshow(pp)
- pp.group {
- pp.text @string
- unless @children.empty?
- pp.text '['
- pp.nest(2) {
- pp.breakable
- first = true
- @children.each {|t|
- if first
- first = false
- else
- pp.text ','
- pp.breakable
- end
- t.altshow(pp)
- }
- }
- pp.breakable
- pp.text ']'
- end
- }
- end
-
- end
- end
-
- class StrictPrettyExample < RUNIT::TestCase
- def setup
- @pp = PrettyPrint.new
- @pp.group {
- @pp.group {@pp.nest(2) {
- @pp.text "if"; @pp.breakable;
- @pp.group {
- @pp.nest(2) {
- @pp.group {@pp.text "a"; @pp.breakable; @pp.text "=="}
- @pp.breakable; @pp.text "b"}}}}
- @pp.breakable
- @pp.group {@pp.nest(2) {
- @pp.text "then"; @pp.breakable;
- @pp.group {
- @pp.nest(2) {
- @pp.group {@pp.text "a"; @pp.breakable; @pp.text "<<"}
- @pp.breakable; @pp.text "2"}}}}
- @pp.breakable
- @pp.group {@pp.nest(2) {
- @pp.text "else"; @pp.breakable;
- @pp.group {
- @pp.nest(2) {
- @pp.group {@pp.text "a"; @pp.breakable; @pp.text "+"}
- @pp.breakable; @pp.text "b"}}}}}
- end
-
- def test_00_04
- expected = <<'End'.chomp
-if
- a
- ==
- b
-then
- a
- <<
- 2
-else
- a
- +
- b
-End
- @pp.format(out='', 0); assert_equal(expected, out)
- @pp.format(out='', 4); assert_equal(expected, out)
- end
-
- def test_05
- expected = <<'End'.chomp
-if
- a
- ==
- b
-then
- a
- <<
- 2
-else
- a +
- b
-End
- @pp.format(out='', 5); assert_equal(expected, out)
- end
-
- def test_06
- expected = <<'End'.chomp
-if
- a ==
- b
-then
- a <<
- 2
-else
- a +
- b
-End
- @pp.format(out='', 6); assert_equal(expected, out)
- end
-
- def test_07
- expected = <<'End'.chomp
-if
- a ==
- b
-then
- a <<
- 2
-else
- a + b
-End
- @pp.format(out='', 7); assert_equal(expected, out)
end
-
- def test_08
- expected = <<'End'.chomp
-if
- a == b
-then
- a << 2
-else
- a + b
-End
- @pp.format(out='', 8); assert_equal(expected, out)
- end
-
- def test_09
- expected = <<'End'.chomp
-if a == b
-then
- a << 2
-else
- a + b
-End
- @pp.format(out='', 9); assert_equal(expected, out)
- end
-
- def test_10
- expected = <<'End'.chomp
-if a == b
-then
- a << 2
-else a + b
-End
- @pp.format(out='', 10); assert_equal(expected, out)
- end
-
- def test_11_31
- expected = <<'End'.chomp
-if a == b
-then a << 2
-else a + b
-End
- @pp.format(out='', 11); assert_equal(expected, out)
- @pp.format(out='', 15); assert_equal(expected, out)
- @pp.format(out='', 31); assert_equal(expected, out)
- end
-
- def test_32
- expected = <<'End'.chomp
-if a == b then a << 2 else a + b
-End
- @pp.format(out='', 32); assert_equal(expected, out)
- end
-
end
- class TailGroup < RUNIT::TestCase
- def test_1
- pp = PrettyPrint.new
- pp.group {
- pp.group {
- pp.text "abc"
- pp.breakable
- pp.text "def"
- }
- pp.group {
- pp.text "ghi"
- pp.breakable
- pp.text "jkl"
- }
+ # The GroupQueue class is used for managing the queue of Group to be pretty
+ # printed.
+ #
+ # This queue groups the Group objects, based on their depth.
+ #
+ # This class is intended for internal use of the PrettyPrint buffers.
+ class GroupQueue # :nodoc:
+ # Create a GroupQueue object
+ #
+ # Arguments:
+ # * +groups+ - one or more PrettyPrint::Group objects
+ def initialize(*groups)
+ @queue = []
+ groups.each {|g| enq g}
+ end
+
+ # Enqueue +group+
+ #
+ # This does not strictly append the group to the end of the queue,
+ # but instead adds it in line, base on the +group.depth+
+ def enq(group)
+ depth = group.depth
+ @queue << [] until depth < @queue.length
+ @queue[depth] << group
+ end
+
+ # Returns the outer group of the queue
+ def deq
+ @queue.each {|gs|
+ (gs.length-1).downto(0) {|i|
+ unless gs[i].breakables.empty?
+ group = gs.slice!(i, 1).first
+ group.break
+ return group
+ end
+ }
+ gs.each {|group| group.break}
+ gs.clear
}
- pp.format(out='', 10)
- assert_equal("abc\ndefghi jkl", out)
- end
- end
-
- class NonString < RUNIT::TestCase
- def setup
- @pp = PrettyPrint.new('newline') {|n| "#{n} spaces"}
- @pp.text(3, 3)
- @pp.breakable(1, 1)
- @pp.text(3, 3)
+ return nil
end
- def test_6
- @pp.format(out=[], 6)
- assert_equal([3, "newline", "0 spaces", 3], out)
+ # Remote +group+ from this queue
+ def delete(group)
+ @queue[group.depth].delete(group)
end
-
- def test_7
- @pp.format(out=[], 7)
- assert_equal([3, 1, 3], out)
- end
-
end
- class Fill < RUNIT::TestCase
- def setup
- @pp = PrettyPrint.new
- @pp.fill_group {
- @pp.text 'abc'
- @pp.breakable
- @pp.text 'def'
- @pp.breakable
- @pp.text 'ghi'
- @pp.breakable
- @pp.text 'jkl'
- @pp.breakable
- @pp.text 'mno'
- @pp.breakable
- @pp.text 'pqr'
- @pp.breakable
- @pp.text 'stu'
- }
- end
-
- def test_0_6
- expected = <<'End'.chomp
-abc
-def
-ghi
-jkl
-mno
-pqr
-stu
-End
- @pp.format(out='', 0)
- assert_equal(expected, out)
- @pp.format(out='', 6)
- assert_equal(expected, out)
- end
-
- def test_7_10
- expected = <<'End'.chomp
-abc def
-ghi jkl
-mno pqr
-stu
-End
- @pp.format(out='', 7)
- assert_equal(expected, out)
- @pp.format(out='', 10)
- assert_equal(expected, out)
+ # PrettyPrint::SingleLine is used by PrettyPrint.singleline_format
+ #
+ # It is passed to be similar to a PrettyPrint object itself, by responding to:
+ # * #text
+ # * #breakable
+ # * #fill_breakable
+ # * #nest
+ # * #group
+ # * #group_sub
+ # * #flush
+ # * #first?
+ #
+ # but instead, the output has no line breaks
+ #
+ class SingleLine
+ # Create a PrettyPrint::SingleLine object
+ #
+ # Arguments:
+ # * +output+ - String (or similar) to store rendered text. Needs to respond to '<<'
+ # * +maxwidth+ - Argument position expected to be here for compatibility.
+ # This argument is a noop.
+ # * +newline+ - Argument position expected to be here for compatibility.
+ # This argument is a noop.
+ def initialize(output, maxwidth=nil, newline=nil)
+ @output = output
+ @first = [true]
+ end
+
+ # Add +obj+ to the text to be output.
+ #
+ # +width+ argument is here for compatibility. It is a noop argument.
+ def text(obj, width=nil)
+ @output << obj
+ end
+
+ # Appends +sep+ to the text to be output. By default +sep+ is ' '
+ #
+ # +width+ argument is here for compatibility. It is a noop argument.
+ def breakable(sep=' ', width=nil)
+ @output << sep
+ end
+
+ # Appends +sep+ to the text to be output. By default +sep+ is ' '
+ #
+ # +width+ argument is here for compatibility. It is a noop argument.
+ def fill_breakable(sep=' ', width=nil)
+ @output << sep
+ end
+
+ # Takes +indent+ arg, but does nothing with it.
+ #
+ # Yields to a block.
+ def nest(indent) # :nodoc:
+ yield
end
- def test_11_14
- expected = <<'End'.chomp
-abc def ghi
-jkl mno pqr
-stu
-End
- @pp.format(out='', 11)
- assert_equal(expected, out)
- @pp.format(out='', 14)
- assert_equal(expected, out)
+ # Opens a block for grouping objects to be pretty printed.
+ #
+ # Arguments:
+ # * +indent+ - noop argument. Present for compatibility.
+ # * +open_obj+ - text appended before the &blok. Default is ''
+ # * +close_obj+ - text appended after the &blok. Default is ''
+ # * +open_width+ - noop argument. Present for compatibility.
+ # * +close_width+ - noop argument. Present for compatibility.
+ def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
+ @first.push true
+ @output << open_obj
+ yield
+ @output << close_obj
+ @first.pop
end
- def test_15_18
- expected = <<'End'.chomp
-abc def ghi jkl
-mno pqr stu
-End
- @pp.format(out='', 15)
- assert_equal(expected, out)
- @pp.format(out='', 18)
- assert_equal(expected, out)
+ # Yields to a block for compatibility.
+ def group_sub # :nodoc:
+ yield
end
- def test_19_22
- expected = <<'End'.chomp
-abc def ghi jkl mno
-pqr stu
-End
- @pp.format(out='', 19)
- assert_equal(expected, out)
- @pp.format(out='', 22)
- assert_equal(expected, out)
+ # Method present for compatibility, but is a noop
+ def break_outmost_groups # :nodoc:
end
- def test_23_26
- expected = <<'End'.chomp
-abc def ghi jkl mno pqr
-stu
-End
- @pp.format(out='', 23)
- assert_equal(expected, out)
- @pp.format(out='', 26)
- assert_equal(expected, out)
+ # Method present for compatibility, but is a noop
+ def flush # :nodoc:
end
- def test_27
- expected = <<'End'.chomp
-abc def ghi jkl mno pqr stu
-End
- @pp.format(out='', 27)
- assert_equal(expected, out)
+ # This is used as a predicate, and ought to be called first.
+ def first?
+ result = @first[-1]
+ @first[-1] = false
+ result
end
-
end
-
- RUNIT::CUI::TestRunner.run(WadlerExample.suite)
- RUNIT::CUI::TestRunner.run(StrictPrettyExample.suite)
- RUNIT::CUI::TestRunner.run(TailGroup.suite)
- RUNIT::CUI::TestRunner.run(NonString.suite)
- RUNIT::CUI::TestRunner.run(Fill.suite)
end