Class: SyntaxTree::Heredoc

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

Overview

Heredoc represents a heredoc string literal.

"contents\n"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Heredoc.



4344
4345
4346
4347
4348
4349
4350
# File 'lib/syntax_tree/node.rb', line 4344

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



4332
4333
4334
# File 'lib/syntax_tree/node.rb', line 4332

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4342
4343
4344
# File 'lib/syntax_tree/node.rb', line 4342

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the heredoc



4335
4336
4337
# File 'lib/syntax_tree/node.rb', line 4335

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



4339
4340
4341
# File 'lib/syntax_tree/node.rb', line 4339

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



4352
4353
4354
# File 'lib/syntax_tree/node.rb', line 4352

def accept(visitor)
  visitor.visit_heredoc(self)
end

#child_nodesObject Also known as: deconstruct



4356
4357
4358
# File 'lib/syntax_tree/node.rb', line 4356

def child_nodes
  [beginning, *parts]
end

#deconstruct_keys(keys) ⇒ Object



4362
4363
4364
4365
4366
4367
4368
4369
4370
# File 'lib/syntax_tree/node.rb', line 4362

def deconstruct_keys(keys)
  {
    beginning: beginning,
    location: location,
    ending: ending,
    parts: parts,
    comments: comments
  }
end

#format(q) ⇒ Object



4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
# File 'lib/syntax_tree/node.rb', line 4372

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