Class: SyntaxTree::QSymbols

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

Overview

QSymbols represents a symbol literal array without interpolation.

i[one two three]

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of QSymbols.



6308
6309
6310
6311
6312
6313
# File 'lib/syntax_tree/node.rb', line 6308

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

Instance Attribute Details

#beginningObject (readonly)

QSymbolsBeg

the token that opens this array literal



6300
6301
6302
# File 'lib/syntax_tree/node.rb', line 6300

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6306
6307
6308
# File 'lib/syntax_tree/node.rb', line 6306

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



6303
6304
6305
# File 'lib/syntax_tree/node.rb', line 6303

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



6315
6316
6317
# File 'lib/syntax_tree/node.rb', line 6315

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

#child_nodesObject Also known as: deconstruct



6319
6320
6321
# File 'lib/syntax_tree/node.rb', line 6319

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6325
6326
6327
6328
6329
6330
6331
6332
# File 'lib/syntax_tree/node.rb', line 6325

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

#format(q) ⇒ Object



6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
# File 'lib/syntax_tree/node.rb', line 6334

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

  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