Class: SyntaxTree::QWords

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

Overview

QWords represents a string literal array without interpolation.

%w[one two three]

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(beginning:, elements:, location:, comments: []) ⇒ QWords

Returns a new instance of QWords.



7153
7154
7155
7156
7157
7158
# File 'lib/syntax_tree/node.rb', line 7153

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

Instance Attribute Details

#beginning ⇒ Object (readonly)

[QWordsBeg] the token that opens this array literal



7145
7146
7147
# File 'lib/syntax_tree/node.rb', line 7145

def beginning
  @beginning
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



7151
7152
7153
# File 'lib/syntax_tree/node.rb', line 7151

def comments
  @comments
end

#elements ⇒ Object (readonly)

[Array[ TStringContent ]] the elements of the array



7148
7149
7150
# File 'lib/syntax_tree/node.rb', line 7148

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



7160
7161
7162
# File 'lib/syntax_tree/node.rb', line 7160

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

#child_nodes ⇒ Object Also known as: deconstruct



7164
7165
7166
# File 'lib/syntax_tree/node.rb', line 7164

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



7170
7171
7172
7173
7174
7175
7176
7177
# File 'lib/syntax_tree/node.rb', line 7170

def deconstruct_keys(_keys)
  {
    beginning: beginning,
    elements: elements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
# File 'lib/syntax_tree/node.rb', line 7179

def format(q)
  opening, closing = "%w[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.group(0, opening, closing) do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end