Class: SyntaxTree::Heredoc
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
-
#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
- HeredocEnd
-
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
#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
#beginning ⇒ Object (readonly)
- HeredocBeg
-
the opening of the heredoc
4962 4963 4964 |
# File 'lib/syntax_tree/node.rb', line 4962 def beginning @beginning end |
#comments ⇒ Object (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 |
#dedent ⇒ Object (readonly)
- Integer
-
how far to dedent the heredoc
4968 4969 4970 |
# File 'lib/syntax_tree/node.rb', line 4968 def dedent @dedent end |
#ending ⇒ Object (readonly)
- HeredocEnd
-
the ending of the heredoc
4965 4966 4967 |
# File 'lib/syntax_tree/node.rb', line 4965 def ending @ending end |
#parts ⇒ Object (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_nodes ⇒ Object 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 |