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.



6370
6371
6372
6373
6374
6375
6376
# File 'lib/syntax_tree.rb', line 6370

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



6355
6356
6357
# File 'lib/syntax_tree.rb', line 6355

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6368
6369
6370
# File 'lib/syntax_tree.rb', line 6368

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the heredoc



6358
6359
6360
# File 'lib/syntax_tree.rb', line 6358

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



6365
6366
6367
# File 'lib/syntax_tree.rb', line 6365

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



6362
6363
6364
# File 'lib/syntax_tree.rb', line 6362

def parts
  @parts
end

Instance Method Details

#child_nodesObject



6378
6379
6380
# File 'lib/syntax_tree.rb', line 6378

def child_nodes
  [beginning, *parts]
end

#format(q) ⇒ Object



6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
# File 'lib/syntax_tree.rb', line 6382

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



6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
# File 'lib/syntax_tree.rb', line 6413

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



6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
# File 'lib/syntax_tree.rb', line 6424

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