Class: SyntaxTree::Heredoc

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Heredoc represents a heredoc string literal.

"contents\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beginning:, ending: nil, parts: [], location:, comments: []) ⇒ Heredoc

Returns a new instance of Heredoc.



6038
6039
6040
6041
6042
6043
6044
# File 'lib/syntax_tree.rb', line 6038

def initialize(beginning:, ending: nil, parts: [], location:, comments: [])
  @beginning = beginning
  @ending = ending
  @parts = parts
  @location = location
  @comments = comments
end

Instance Attribute Details

#beginningObject (readonly)

HeredocBeg

the opening of the heredoc



6023
6024
6025
# File 'lib/syntax_tree.rb', line 6023

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6036
6037
6038
# File 'lib/syntax_tree.rb', line 6036

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the heredoc



6026
6027
6028
# File 'lib/syntax_tree.rb', line 6026

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



6033
6034
6035
# File 'lib/syntax_tree.rb', line 6033

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



6030
6031
6032
# File 'lib/syntax_tree.rb', line 6030

def parts
  @parts
end

Instance Method Details

#child_nodesObject



6046
6047
6048
# File 'lib/syntax_tree.rb', line 6046

def child_nodes
  [beginning, *parts]
end

#format(q) ⇒ Object



6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
# File 'lib/syntax_tree.rb', line 6050

def format(q)
  # This is a very specific behavior that should probably be included in the
  # prettyprint module. It's when you want to force a newline, but don't
  # want to force the break parent.
  breakable = -> do
    q.target <<
      PrettyPrint::Breakable.new(" ", 1, indent: false, force: true)
  end

  q.group do
    q.format(beginning)

    q.line_suffix do
      q.group do
        breakable.call

        parts.each do |part|
          if part.is_a?(TStringContent)
            texts = part.value.split(/\r?\n/, -1)
            q.seplist(texts, breakable) { |text| q.text(text) }
          else
            q.format(part)
          end
        end

        q.text(ending)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
# File 'lib/syntax_tree.rb', line 6081

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("heredoc")

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
# File 'lib/syntax_tree.rb', line 6092

def to_json(*opts)
  {
    type: :heredoc,
    beging: beginning,
    ending: ending,
    parts: parts,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end