Module: SyntaxTree::RemoveBreaks

Defined in:
lib/syntax_tree.rb

Overview

This module will remove any breakables from the list of contents so that no newlines are present in the output.

Class Method Summary collapse

Class Method Details

.call(doc) ⇒ Object



2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
# File 'lib/syntax_tree.rb', line 2445

def call(doc)
  marker = Object.new
  stack = [doc]

  while stack.any?
    doc = stack.pop

    if doc == marker
      stack.pop
      next
    end

    stack += [doc, marker]

    case doc
    when PrettyPrint::Align, PrettyPrint::Indent, PrettyPrint::Group
      doc.contents.map! { |child| remove_breaks(child) }
      stack += doc.contents.reverse
    when PrettyPrint::IfBreak
      doc.flat_contents.map! { |child| remove_breaks(child) }
      stack += doc.flat_contents.reverse
    end
  end
end