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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Heredoc.



4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
# File 'lib/syntax_tree/node.rb', line 4857

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



4842
4843
4844
# File 'lib/syntax_tree/node.rb', line 4842

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4855
4856
4857
# File 'lib/syntax_tree/node.rb', line 4855

def comments
  @comments
end

#dedentObject (readonly)

Integer

how far to dedent the heredoc



4848
4849
4850
# File 'lib/syntax_tree/node.rb', line 4848

def dedent
  @dedent
end

#endingObject (readonly)

String

the ending of the heredoc



4845
4846
4847
# File 'lib/syntax_tree/node.rb', line 4845

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

heredoc string literal



4852
4853
4854
# File 'lib/syntax_tree/node.rb', line 4852

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



4873
4874
4875
# File 'lib/syntax_tree/node.rb', line 4873

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

#child_nodesObject Also known as: deconstruct



4877
4878
4879
# File 'lib/syntax_tree/node.rb', line 4877

def child_nodes
  [beginning, *parts]
end

#deconstruct_keys(_keys) ⇒ Object



4883
4884
4885
4886
4887
4888
4889
4890
4891
# File 'lib/syntax_tree/node.rb', line 4883

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

#format(q) ⇒ Object



4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
# File 'lib/syntax_tree/node.rb', line 4893

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