class SyntaxTree::Translation::Parser::HeredocBuilder

for handling the translation.
they are represented in the Syntax Tree AST. This class is responsible
Heredocs are represented very differently in the parser gem from how

def <<(segment)

def <<(segment)
  if segment.type == :str && segments.last &&
       segments.last.type == :str &&
       !segments.last.children.first.end_with?("\n")
    segments.last.children.first << segment.children.first
  else
    segments << segment
  end
end

def initialize(node)

def initialize(node)
  @node = node
  @segments = []
end

def trim!

def trim!
  return unless node.beginning.value[2] == "~"
  lines = [Line.new(+"", [])]
  segments.each do |segment|
    lines.last.segments << segment
    if segment.type == :str
      lines.last.value << segment.children.first
      lines << Line.new(+"", []) if lines.last.value.end_with?("\n")
    end
  end
  lines.pop if lines.last.value.empty?
  return if lines.empty?
  segments.clear
  lines.each do |line|
    remaining = node.dedent
    line.segments.each do |segment|
      if segment.type == :str
        if remaining > 0
          whitespace = segment.children.first[/^\s{0,#{remaining}}/]
          segment.children.first.sub!(/^#{whitespace}/, "")
          remaining -= whitespace.length
        end
        if node.beginning.value[3] != "'" && segments.any? &&
             segments.last.type == :str &&
             segments.last.children.first.end_with?("\\\n")
          segments.last.children.first.gsub!(/\\\n\z/, "")
          segments.last.children.first.concat(segment.children.first)
        elsif !segment.children.first.empty?
          segments << segment
        end
      else
        segments << segment
      end
    end
  end
end