Class: SyntaxTree::StringContent
Overview
StringContent represents the contents of a string-like value.
"string"
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#parts ⇒ Object
readonly
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the string.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(parts: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(parts:, location:) ⇒ StringContent
constructor
A new instance of StringContent.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(parts:, location:) ⇒ StringContent
Returns a new instance of StringContent.
10074 10075 10076 10077 10078 |
# File 'lib/syntax_tree/node.rb', line 10074 def initialize(parts:, location:) @parts = parts @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
10072 10073 10074 |
# File 'lib/syntax_tree/node.rb', line 10072 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string
10069 10070 10071 |
# File 'lib/syntax_tree/node.rb', line 10069 def parts @parts end |
Instance Method Details
#===(other) ⇒ Object
10101 10102 10103 |
# File 'lib/syntax_tree/node.rb', line 10101 def ===(other) other.is_a?(StringContent) && ArrayMatch.call(parts, other.parts) end |
#accept(visitor) ⇒ Object
10080 10081 10082 |
# File 'lib/syntax_tree/node.rb', line 10080 def accept(visitor) visitor.visit_string_content(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
10084 10085 10086 |
# File 'lib/syntax_tree/node.rb', line 10084 def child_nodes parts end |
#copy(parts: nil, location: nil) ⇒ Object
10088 10089 10090 10091 10092 10093 |
# File 'lib/syntax_tree/node.rb', line 10088 def copy(parts: nil, location: nil) StringContent.new( parts: parts || self.parts, location: location || self.location ) end |
#deconstruct_keys(_keys) ⇒ Object
10097 10098 10099 |
# File 'lib/syntax_tree/node.rb', line 10097 def deconstruct_keys(_keys) { parts: parts, location: location } end |
#format(q) ⇒ Object
10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 |
# File 'lib/syntax_tree/node.rb', line 10105 def format(q) q.text(q.quote) q.group do parts.each do |part| if part.is_a?(TStringContent) value = Quotes.normalize(part.value, q.quote) first = true value.each_line(chomp: true) do |line| if first first = false else q.breakable_return end q.text(line) end q.breakable_return if value.end_with?("\n") else q.format(part) end end end q.text(q.quote) end |