Class: SyntaxTree::Heredoc

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

Overview

Heredoc represents a heredoc string literal.

<<~DOC
  contents
DOC

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Heredoc.



4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
# File 'lib/syntax_tree/node.rb', line 4785

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

Instance Attribute Details

#beginningObject (readonly)

HeredocBeg

the opening of the heredoc



4770
4771
4772
# File 'lib/syntax_tree/node.rb', line 4770

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4783
4784
4785
# File 'lib/syntax_tree/node.rb', line 4783

def comments
  @comments
end

#dedentObject (readonly)

Integer

how far to dedent the heredoc



4776
4777
4778
# File 'lib/syntax_tree/node.rb', line 4776

def dedent
  @dedent
end

#endingObject (readonly)

String

the ending of the heredoc



4773
4774
4775
# File 'lib/syntax_tree/node.rb', line 4773

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



4780
4781
4782
# File 'lib/syntax_tree/node.rb', line 4780

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



4801
4802
4803
# File 'lib/syntax_tree/node.rb', line 4801

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

#child_nodesObject Also known as: deconstruct



4805
4806
4807
# File 'lib/syntax_tree/node.rb', line 4805

def child_nodes
  [beginning, *parts]
end

#deconstruct_keys(_keys) ⇒ Object



4811
4812
4813
4814
4815
4816
4817
4818
4819
# File 'lib/syntax_tree/node.rb', line 4811

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

#format(q) ⇒ Object



4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
# File 'lib/syntax_tree/node.rb', line 4821

def format(q)
  # This is a very specific behavior where you want to force a newline, but
  # don't want to force the break parent.
  breakable = -> { q.breakable(indent: false, force: :skip_break_parent) }

  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