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.



10042
10043
10044
10045
10046
# File 'lib/syntax_tree/node.rb', line 10042

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10040
10041
10042
# File 'lib/syntax_tree/node.rb', line 10040

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string



10037
10038
10039
# File 'lib/syntax_tree/node.rb', line 10037

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



10048
10049
10050
# File 'lib/syntax_tree/node.rb', line 10048

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

#child_nodesObject Also known as: deconstruct



10052
10053
10054
# File 'lib/syntax_tree/node.rb', line 10052

def child_nodes
  parts
end

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



10056
10057
10058
10059
10060
10061
# File 'lib/syntax_tree/node.rb', line 10056

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

#deconstruct_keys(_keys) ⇒ Object



10065
10066
10067
# File 'lib/syntax_tree/node.rb', line 10065

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

#format(q) ⇒ Object



10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
# File 'lib/syntax_tree/node.rb', line 10073

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