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.



10059
10060
10061
10062
10063
# File 'lib/syntax_tree/node.rb', line 10059

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string



10054
10055
10056
# File 'lib/syntax_tree/node.rb', line 10054

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



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

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

#deconstruct_keys(_keys) ⇒ Object



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

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

#format(q) ⇒ Object



10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
# File 'lib/syntax_tree/node.rb', line 10090

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