Class: SyntaxTree::Heredoc
Overview
Heredoc represents a heredoc string literal.
<<~DOC
contents
DOC
Instance Attribute Summary collapse
-
#beginning ⇒ Object
readonly
- HeredocBeg
-
the opening of the heredoc.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#dedent ⇒ Object
readonly
- Integer
-
how far to dedent the heredoc.
-
#ending ⇒ Object
readonly
- String
-
the ending of the heredoc.
-
#parts ⇒ Object
readonly
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the heredoc string literal.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(beginning:, ending: nil, dedent: 0, parts: [], location:, comments: []) ⇒ Heredoc
constructor
A new instance of Heredoc.
Methods inherited from Node
Constructor Details
#initialize(beginning:, ending: nil, dedent: 0, parts: [], location:, comments: []) ⇒ Heredoc
Returns a new instance of Heredoc.
4743 4744 4745 4746 4747 4748 4749 4750 |
# File 'lib/syntax_tree/node.rb', line 4743 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
#beginning ⇒ Object (readonly)
- HeredocBeg
-
the opening of the heredoc
4728 4729 4730 |
# File 'lib/syntax_tree/node.rb', line 4728 def beginning @beginning end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4741 4742 4743 |
# File 'lib/syntax_tree/node.rb', line 4741 def comments @comments end |
#dedent ⇒ Object (readonly)
- Integer
-
how far to dedent the heredoc
4734 4735 4736 |
# File 'lib/syntax_tree/node.rb', line 4734 def dedent @dedent end |
#ending ⇒ Object (readonly)
- String
-
the ending of the heredoc
4731 4732 4733 |
# File 'lib/syntax_tree/node.rb', line 4731 def ending @ending end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
heredoc string literal
4738 4739 4740 |
# File 'lib/syntax_tree/node.rb', line 4738 def parts @parts end |
Instance Method Details
#accept(visitor) ⇒ Object
4752 4753 4754 |
# File 'lib/syntax_tree/node.rb', line 4752 def accept(visitor) visitor.visit_heredoc(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
4756 4757 4758 |
# File 'lib/syntax_tree/node.rb', line 4756 def child_nodes [beginning, *parts] end |
#deconstruct_keys(keys) ⇒ Object
4762 4763 4764 4765 4766 4767 4768 4769 4770 |
# File 'lib/syntax_tree/node.rb', line 4762 def deconstruct_keys(keys) { beginning: beginning, location: location, ending: ending, parts: parts, comments: comments } end |
#format(q) ⇒ Object
4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 |
# File 'lib/syntax_tree/node.rb', line 4772 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 |