Class: SyntaxTree::StringContent

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

StringContent represents the contents of a string-like value.

"string"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

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.



10075
10076
10077
10078
10079
# File 'lib/syntax_tree/node.rb', line 10075

def initialize(parts:, location:)
  @parts = parts
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10073
10074
10075
# File 'lib/syntax_tree/node.rb', line 10073

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string



10070
10071
10072
# File 'lib/syntax_tree/node.rb', line 10070

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



10102
10103
10104
# File 'lib/syntax_tree/node.rb', line 10102

def ===(other)
  other.is_a?(StringContent) && ArrayMatch.call(parts, other.parts)
end

#accept(visitor) ⇒ Object



10081
10082
10083
# File 'lib/syntax_tree/node.rb', line 10081

def accept(visitor)
  visitor.visit_string_content(self)
end

#child_nodesObject Also known as: deconstruct



10085
10086
10087
# File 'lib/syntax_tree/node.rb', line 10085

def child_nodes
  parts
end

#copy(parts: nil, location: nil) ⇒ Object



10089
10090
10091
10092
10093
10094
# File 'lib/syntax_tree/node.rb', line 10089

def copy(parts: nil, location: nil)
  StringContent.new(
    parts: parts || self.parts,
    location: location || self.location
  )
end

#deconstruct_keys(_keys) ⇒ Object



10098
10099
10100
# File 'lib/syntax_tree/node.rb', line 10098

def deconstruct_keys(_keys)
  { parts: parts, location: location }
end

#format(q) ⇒ Object



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
10131
# File 'lib/syntax_tree/node.rb', line 10106

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