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
Constructor Details
#initialize(parts:, quote:, location:, comments: []) ⇒ StringLiteral
Returns a new instance of StringLiteral.
7619 7620 7621 7622 7623 7624 |
# File 'lib/syntax_tree/node.rb', line 7619 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
7617 7618 7619 |
# File 'lib/syntax_tree/node.rb', line 7617 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
7611 7612 7613 |
# File 'lib/syntax_tree/node.rb', line 7611 def parts @parts end |
#quote ⇒ Object (readonly)
- String
-
which quote was used by the string literal
7614 7615 7616 |
# File 'lib/syntax_tree/node.rb', line 7614 def quote @quote end |
Instance Method Details
#accept(visitor) ⇒ Object
7626 7627 7628 |
# File 'lib/syntax_tree/node.rb', line 7626 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
7630 7631 7632 |
# File 'lib/syntax_tree/node.rb', line 7630 def child_nodes parts end |
#deconstruct_keys(keys) ⇒ Object
7636 7637 7638 |
# File 'lib/syntax_tree/node.rb', line 7636 def deconstruct_keys(keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 |
# File 'lib/syntax_tree/node.rb', line 7640 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] 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 |