Class: SyntaxTree::Heredoc

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

Overview

Heredoc represents a heredoc string literal.

<<~DOC
  contents
DOC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Heredoc.



6417
6418
6419
6420
6421
6422
6423
# File 'lib/syntax_tree.rb', line 6417

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



6402
6403
6404
# File 'lib/syntax_tree.rb', line 6402

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6415
6416
6417
# File 'lib/syntax_tree.rb', line 6415

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the heredoc



6405
6406
6407
# File 'lib/syntax_tree.rb', line 6405

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



6412
6413
6414
# File 'lib/syntax_tree.rb', line 6412

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



6409
6410
6411
# File 'lib/syntax_tree.rb', line 6409

def parts
  @parts
end

Instance Method Details

#child_nodesObject



6425
6426
6427
# File 'lib/syntax_tree.rb', line 6425

def child_nodes
  [beginning, *parts]
end

#format(q) ⇒ Object



6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
# File 'lib/syntax_tree.rb', line 6429

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(priority: Formatter::HEREDOC_PRIORITY) 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



6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
# File 'lib/syntax_tree.rb', line 6460

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



6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
# File 'lib/syntax_tree.rb', line 6471

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