Class: SyntaxTree::StringLiteral
Overview
StringLiteral represents a string literal.
"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 literal.
-
#quote ⇒ Object
readonly
- String
-
which quote was used by the 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(parts:, quote:, location:, comments: []) ⇒ StringLiteral
constructor
A new instance of StringLiteral.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(parts:, quote:, location:, comments: []) ⇒ StringLiteral
Returns a new instance of StringLiteral.
8432 8433 8434 8435 8436 8437 |
# File 'lib/syntax_tree/node.rb', line 8432 def initialize(parts:, quote:, location:, comments: []) @parts = parts @quote = quote @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
8430 8431 8432 |
# File 'lib/syntax_tree/node.rb', line 8430 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
8424 8425 8426 |
# File 'lib/syntax_tree/node.rb', line 8424 def parts @parts end |
#quote ⇒ Object (readonly)
- String
-
which quote was used by the string literal
8427 8428 8429 |
# File 'lib/syntax_tree/node.rb', line 8427 def quote @quote end |
Instance Method Details
#accept(visitor) ⇒ Object
8439 8440 8441 |
# File 'lib/syntax_tree/node.rb', line 8439 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
8443 8444 8445 |
# File 'lib/syntax_tree/node.rb', line 8443 def child_nodes parts end |
#deconstruct_keys(_keys) ⇒ Object
8449 8450 8451 |
# File 'lib/syntax_tree/node.rb', line 8449 def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 |
# File 'lib/syntax_tree/node.rb', line 8453 def format(q) if parts.empty? q.text("#{q.quote}#{q.quote}") return end opening_quote, closing_quote = if !Quotes.locked?(self, q.quote) [q.quote, q.quote] elsif quote.start_with?("%") [quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])] else [quote, quote] end q.group(0, opening_quote, closing_quote) do parts.each do |part| if part.is_a?(TStringContent) value = Quotes.normalize(part.value, closing_quote) separator = -> { q.breakable(force: true, indent: false) } q.seplist(value.split(/\r?\n/, -1), separator) do |text| q.text(text) end else q.format(part) end end end end |