Class: SyntaxTree::StringEmbExpr

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

Overview

StringEmbExpr represents interpolated content. It can be contained within a couple of different parent nodes, including regular expressions, strings, and dynamic symbols.

"string #{expression}"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(statements:, location:, comments: []) ⇒ StringEmbExpr

Returns a new instance of StringEmbExpr.



8105
8106
8107
8108
8109
# File 'lib/syntax_tree/node.rb', line 8105

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8103
8104
8105
# File 'lib/syntax_tree/node.rb', line 8103

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



8100
8101
8102
# File 'lib/syntax_tree/node.rb', line 8100

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



8111
8112
8113
# File 'lib/syntax_tree/node.rb', line 8111

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

#child_nodesObject Also known as: deconstruct



8115
8116
8117
# File 'lib/syntax_tree/node.rb', line 8115

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



8121
8122
8123
# File 'lib/syntax_tree/node.rb', line 8121

def deconstruct_keys(keys)
  { statements: statements, location: location, comments: comments }
end

#format(q) ⇒ Object



8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
# File 'lib/syntax_tree/node.rb', line 8125

def format(q)
  if location.start_line == location.end_line
    # If the contents of this embedded expression were originally on the
    # same line in the source, then we're going to leave them in place and
    # assume that's the way the developer wanted this expression
    # represented.
    doc = q.group(0, '#{', "}") { q.format(statements) }
    RemoveBreaks.call(doc)
  else
    q.group do
      q.text('#{')
      q.indent do
        q.breakable("")
        q.format(statements)
      end
      q.breakable("")
      q.text("}")
    end
  end
end