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

Constant Summary collapse

SEPARATOR =

This is a very specific behavior where you want to force a newline, but don’t want to force the break parent.

PrettierPrint::Breakable.new(" ", 1, indent: false, force: true).freeze

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:) ⇒ Heredoc

Returns a new instance of Heredoc.



5683
5684
5685
5686
5687
5688
5689
5690
# File 'lib/syntax_tree/node.rb', line 5683

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

Instance Attribute Details

#beginningObject (readonly)

HeredocBeg

the opening of the heredoc



5668
5669
5670
# File 'lib/syntax_tree/node.rb', line 5668

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5681
5682
5683
# File 'lib/syntax_tree/node.rb', line 5681

def comments
  @comments
end

#dedentObject (readonly)

Integer

how far to dedent the heredoc



5674
5675
5676
# File 'lib/syntax_tree/node.rb', line 5674

def dedent
  @dedent
end

#endingObject (readonly)

HeredocEnd

the ending of the heredoc



5671
5672
5673
# File 'lib/syntax_tree/node.rb', line 5671

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



5678
5679
5680
# File 'lib/syntax_tree/node.rb', line 5678

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



5765
5766
5767
5768
# File 'lib/syntax_tree/node.rb', line 5765

def ===(other)
  other.is_a?(Heredoc) && beginning === other.beginning &&
    ending === other.ending && ArrayMatch.call(parts, other.parts)
end

#accept(visitor) ⇒ Object



5692
5693
5694
# File 'lib/syntax_tree/node.rb', line 5692

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

#child_nodesObject Also known as: deconstruct



5696
5697
5698
# File 'lib/syntax_tree/node.rb', line 5696

def child_nodes
  [beginning, *parts, ending]
end

#copy(beginning: nil, location: nil, ending: nil, parts: nil) ⇒ Object



5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
# File 'lib/syntax_tree/node.rb', line 5700

def copy(beginning: nil, location: nil, ending: nil, parts: nil)
  node =
    Heredoc.new(
      beginning: beginning || self.beginning,
      location: location || self.location,
      ending: ending || self.ending,
      parts: parts || self.parts
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



5715
5716
5717
5718
5719
5720
5721
5722
5723
# File 'lib/syntax_tree/node.rb', line 5715

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

#format(q) ⇒ Object



5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
# File 'lib/syntax_tree/node.rb', line 5730

def format(q)
  q.group do
    q.format(beginning)

    q.line_suffix(priority: Formatter::HEREDOC_PRIORITY) do
      q.group do
        q.target << SEPARATOR

        parts.each do |part|
          if part.is_a?(TStringContent)
            value = part.value
            first = true

            value.each_line(chomp: true) do |line|
              if first
                first = false
              else
                q.target << SEPARATOR
              end

              q.text(line)
            end

            q.target << SEPARATOR if value.end_with?("\n")
          else
            q.format(part)
          end
        end

        q.format(ending)
      end
    end
  end
end