diff options
Diffstat (limited to 'lib/prettyprint.rb')
| -rw-r--r-- | lib/prettyprint.rb | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/lib/prettyprint.rb b/lib/prettyprint.rb index 5b1da330dd..44ca5e816f 100644 --- a/lib/prettyprint.rb +++ b/lib/prettyprint.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true +# # This class implements a pretty printing algorithm. It finds line breaks and # nice indentations for grouped structure. # @@ -21,16 +23,19 @@ # # == References # Christian Lindig, Strictly Pretty, March 2000, -# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty +# https://lindig.github.io/papers/strictly-pretty-2000.pdf # # Philip Wadler, A prettier printer, March 1998, -# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier +# https://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier # # == Author # Tanaka Akira <akr@fsij.org> # class PrettyPrint + # The version string + VERSION = "0.2.0" + # This is a convenience method which is same as follows: # # begin @@ -40,7 +45,7 @@ class PrettyPrint # output # end # - def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n}) + def PrettyPrint.format(output=''.dup, maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n}) q = PrettyPrint.new(output, maxwidth, newline, &genspace) yield q q.flush @@ -54,7 +59,7 @@ class PrettyPrint # 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='', maxwidth=nil, newline=nil, genspace=nil) + def PrettyPrint.singleline_format(output=''.dup, maxwidth=nil, newline=nil, genspace=nil) q = SingleLine.new(output) yield q output @@ -77,7 +82,7 @@ class PrettyPrint # The block is used to generate spaces. {|width| ' ' * width} is used if it # is not given. # - def initialize(output='', maxwidth=79, newline="\n", &genspace) + def initialize(output=''.dup, maxwidth=79, newline="\n", &genspace) @output = output @maxwidth = maxwidth @newline = newline @@ -100,7 +105,7 @@ class PrettyPrint # The maximum width of a line, before it is separated in to a newline # - # This defaults to 79, and should be a Fixnum + # This defaults to 79, and should be an Integer attr_reader :maxwidth # The value that is appended to +output+ to add a new line. @@ -108,7 +113,7 @@ class PrettyPrint # This defaults to "\n", and should be String attr_reader :newline - # A lambda or Proc, that takes one argument, of a Fixnum, and returns + # A lambda or Proc, that takes one argument, of an Integer, and returns # the corresponding number of spaces. # # By default this is: @@ -154,28 +159,6 @@ class PrettyPrint @group_stack.last end - # first? is a predicate to test the call is a first call to first? with - # current group. - # - # It is useful to format comma separated values as: - # - # q.group(1, '[', ']') { - # xxx.each {|yyy| - # unless q.first? - # q.text ',' - # q.breakable - # end - # ... pretty printing yyy ... - # } - # } - # - # first? is obsoleted in 1.8.2. - # - def first? - warn "PrettyPrint#first? is obsoleted at 1.8.2." - current_group.first? - end - # Breaks the buffer into lines that are shorter than #maxwidth def break_outmost_groups while @maxwidth < @output_width + @buffer_width @@ -360,7 +343,7 @@ class PrettyPrint # # Arguments: # * +sep+ String of the separator - # * +width+ Fixnum width of the +sep+ + # * +width+ Integer width of the +sep+ # * +q+ parent PrettyPrint object, to base from def initialize(sep, width, q) @obj = sep @@ -504,8 +487,10 @@ class PrettyPrint # It is passed to be similar to a PrettyPrint object itself, by responding to: # * #text # * #breakable + # * #fill_breakable # * #nest # * #group + # * #group_sub # * #flush # * #first? # @@ -539,6 +524,13 @@ class PrettyPrint @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. @@ -562,6 +554,15 @@ class PrettyPrint @first.pop end + # Yields to a block for compatibility. + def group_sub # :nodoc: + yield + end + + # Method present for compatibility, but is a noop + def break_outmost_groups # :nodoc: + end + # Method present for compatibility, but is a noop def flush # :nodoc: end |
