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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of Heredoc.



5788
5789
5790
5791
5792
5793
5794
5795
# File 'lib/syntax_tree/node.rb', line 5788

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

Instance Attribute Details

#beginningObject (readonly)

HeredocBeg

the opening of the heredoc



5773
5774
5775
# File 'lib/syntax_tree/node.rb', line 5773

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5786
5787
5788
# File 'lib/syntax_tree/node.rb', line 5786

def comments
  @comments
end

#dedentObject (readonly)

Integer

how far to dedent the heredoc



5779
5780
5781
# File 'lib/syntax_tree/node.rb', line 5779

def dedent
  @dedent
end

#endingObject (readonly)

HeredocEnd

the ending of the heredoc



5776
5777
5778
# File 'lib/syntax_tree/node.rb', line 5776

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



5783
5784
5785
# File 'lib/syntax_tree/node.rb', line 5783

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



5870
5871
5872
5873
# File 'lib/syntax_tree/node.rb', line 5870

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

#accept(visitor) ⇒ Object



5797
5798
5799
# File 'lib/syntax_tree/node.rb', line 5797

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

#child_nodesObject Also known as: deconstruct



5801
5802
5803
# File 'lib/syntax_tree/node.rb', line 5801

def child_nodes
  [beginning, *parts, ending]
end

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



5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
# File 'lib/syntax_tree/node.rb', line 5805

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



5820
5821
5822
5823
5824
5825
5826
5827
5828
# File 'lib/syntax_tree/node.rb', line 5820

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

#format(q) ⇒ Object



5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
# File 'lib/syntax_tree/node.rb', line 5835

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