class SyntaxTree::Heredoc

Heredoc represents a heredoc string literal.

<<~DOC
  contents
DOC

Constants

SEPARATOR

This is a very specific behavior where you want to force a newline, but don’t want to force the break parent.

Attributes

beginning[R]
HeredocBeg

the opening of the heredoc

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

dedent[R]
Integer

how far to dedent the heredoc

ending[R]
HeredocEnd

the ending of the heredoc

parts[R]
Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal

Public Class Methods

new(beginning:, location:, ending: nil, dedent: 0, parts: []) click to toggle source
# File lib/syntax_tree/node.rb, line 5791
def initialize(beginning:, location:, ending: nil, dedent: 0, parts: [])
  @beginning = beginning
  @ending = ending
  @dedent = dedent
  @parts = parts
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 5873
def ===(other)
  other.is_a?(Heredoc) && beginning === other.beginning &&
    ending === other.ending && ArrayMatch.call(parts, other.parts)
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 5800
def accept(visitor)
  visitor.visit_heredoc(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 5804
def child_nodes
  [beginning, *parts, ending]
end
Also aliased as: deconstruct
copy(beginning: nil, location: nil, ending: nil, parts: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 5808
def copy(beginning: nil, location: nil, ending: nil, parts: nil)
  node =
    Heredoc.new(
      beginning: beginning || self.beginning,
      location: location || self.location,
      ending: ending || self.ending,
      parts: parts || self.parts
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 5823
def deconstruct_keys(_keys)
  {
    beginning: beginning,
    location: location,
    ending: ending,
    parts: parts,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 5838
def format(q)
  q.group do
    q.format(beginning)

    q.line_suffix(priority: Formatter::HEREDOC_PRIORITY) do
      q.group do
        q.target << SEPARATOR

        parts.each do |part|
          if part.is_a?(TStringContent)
            value = part.value
            first = true

            value.each_line(chomp: true) do |line|
              if first
                first = false
              else
                q.target << SEPARATOR
              end

              q.text(line)
            end

            q.target << SEPARATOR if value.end_with?("\n")
          else
            q.format(part)
          end
        end

        q.format(ending)
      end
    end
  end
end