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)

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



4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
# File 'lib/syntax_tree/node.rb', line 4977

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



4962
4963
4964
# File 'lib/syntax_tree/node.rb', line 4962

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4975
4976
4977
# File 'lib/syntax_tree/node.rb', line 4975

def comments
  @comments
end

#dedentObject (readonly)

Integer

how far to dedent the heredoc



4968
4969
4970
# File 'lib/syntax_tree/node.rb', line 4968

def dedent
  @dedent
end

#endingObject (readonly)

HeredocEnd

the ending of the heredoc



4965
4966
4967
# File 'lib/syntax_tree/node.rb', line 4965

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



4972
4973
4974
# File 'lib/syntax_tree/node.rb', line 4972

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



4993
4994
4995
# File 'lib/syntax_tree/node.rb', line 4993

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

#child_nodesObject Also known as: deconstruct



4997
4998
4999
# File 'lib/syntax_tree/node.rb', line 4997

def child_nodes
  [beginning, *parts, ending]
end

#deconstruct_keys(_keys) ⇒ Object



5003
5004
5005
5006
5007
5008
5009
5010
5011
# File 'lib/syntax_tree/node.rb', line 5003

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

#format(q) ⇒ Object



5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
# File 'lib/syntax_tree/node.rb', line 5017

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