refine previous change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e50f2d285f
commit
48fbacb11c
@ -98,13 +98,19 @@ class PrettyPrint
|
|||||||
@group_queue = GroupQueue.new(root_group)
|
@group_queue = GroupQueue.new(root_group)
|
||||||
@indent = 0
|
@indent = 0
|
||||||
end
|
end
|
||||||
|
attr_reader :output, :maxwidth, :newline, :genspace
|
||||||
|
attr_reader :indent, :group_queue
|
||||||
|
|
||||||
|
def current_group
|
||||||
|
@group_stack.last
|
||||||
|
end
|
||||||
|
|
||||||
def break_outmost_groups
|
def break_outmost_groups
|
||||||
while @maxwidth < @output_width + @buffer_width
|
while @maxwidth < @output_width + @buffer_width
|
||||||
return unless group = @group_queue.deq
|
return unless group = @group_queue.deq
|
||||||
until group.breakables.empty?
|
until group.breakables.empty?
|
||||||
data = @buffer.shift
|
data = @buffer.shift
|
||||||
@output_width = data.output(@output, @output_width, @newline, @genspace)
|
@output_width = data.output(@output, @output_width)
|
||||||
@buffer_width -= data.width
|
@buffer_width -= data.width
|
||||||
end
|
end
|
||||||
while !@buffer.empty? && Text === @buffer.first
|
while !@buffer.empty? && Text === @buffer.first
|
||||||
@ -140,7 +146,7 @@ class PrettyPrint
|
|||||||
@output_width = @indent
|
@output_width = @indent
|
||||||
@buffer_width = 0
|
@buffer_width = 0
|
||||||
else
|
else
|
||||||
@buffer << Breakable.new(sep, width, @indent, group)
|
@buffer << Breakable.new(sep, width, self)
|
||||||
@buffer_width += width
|
@buffer_width += width
|
||||||
break_outmost_groups
|
break_outmost_groups
|
||||||
end
|
end
|
||||||
@ -154,6 +160,9 @@ class PrettyPrint
|
|||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
@group_stack.pop
|
@group_stack.pop
|
||||||
|
if group.breakables.empty?
|
||||||
|
@group_queue.delete group
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -168,7 +177,7 @@ class PrettyPrint
|
|||||||
|
|
||||||
def flush
|
def flush
|
||||||
@buffer.each {|data|
|
@buffer.each {|data|
|
||||||
@output_width = data.output(@output, @output_width, @newline, @genspace)
|
@output_width = data.output(@output, @output_width)
|
||||||
}
|
}
|
||||||
@buffer.clear
|
@buffer.clear
|
||||||
@buffer_width = 0
|
@buffer_width = 0
|
||||||
@ -181,7 +190,7 @@ class PrettyPrint
|
|||||||
end
|
end
|
||||||
attr_reader :width
|
attr_reader :width
|
||||||
|
|
||||||
def output(out, output_width, newline=nil, genspace=nil)
|
def output(out, output_width)
|
||||||
@objs.each {|obj| out << obj}
|
@objs.each {|obj| out << obj}
|
||||||
output_width + @width
|
output_width + @width
|
||||||
end
|
end
|
||||||
@ -193,22 +202,24 @@ class PrettyPrint
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Breakable
|
class Breakable
|
||||||
def initialize(sep, width, indent, group)
|
def initialize(sep, width, pp)
|
||||||
@obj = sep
|
@obj = sep
|
||||||
@width = width
|
@width = width
|
||||||
@indent = indent
|
@pp = pp
|
||||||
@group = group
|
@indent = pp.indent
|
||||||
|
@group = pp.current_group
|
||||||
@group.breakables.push self
|
@group.breakables.push self
|
||||||
end
|
end
|
||||||
attr_reader :obj, :width, :indent
|
attr_reader :obj, :width, :indent
|
||||||
|
|
||||||
def output(out, output_width, newline, genspace)
|
def output(out, output_width)
|
||||||
@group.breakables.shift
|
@group.breakables.shift
|
||||||
if @group.break?
|
if @group.break?
|
||||||
out << newline
|
out << @pp.newline
|
||||||
out << genspace.call(@indent)
|
out << @pp.genspace.call(@indent)
|
||||||
@indent
|
@indent
|
||||||
else
|
else
|
||||||
|
@pp.group_queue.delete @group if @group.breakables.empty?
|
||||||
out << @obj
|
out << @obj
|
||||||
output_width + @width
|
output_width + @width
|
||||||
end
|
end
|
||||||
@ -258,6 +269,10 @@ class PrettyPrint
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def delete(group)
|
||||||
|
@queue[group.depth].delete(group)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user