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

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(beginning:, elements:, location:) ⇒ QSymbols

Returns a new instance of QSymbols.



8554
8555
8556
8557
8558
8559
# File 'lib/syntax_tree/node.rb', line 8554

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

Instance Attribute Details

#beginningObject (readonly)

QSymbolsBeg

the token that opens this array literal



8546
8547
8548
# File 'lib/syntax_tree/node.rb', line 8546

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8552
8553
8554
# File 'lib/syntax_tree/node.rb', line 8552

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8549
8550
8551
# File 'lib/syntax_tree/node.rb', line 8549

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8614
8615
8616
8617
# File 'lib/syntax_tree/node.rb', line 8614

def ===(other)
  other.is_a?(QSymbols) && beginning === other.beginning &&
    ArrayMatch.call(elements, other.elements)
end

#accept(visitor) ⇒ Object



8561
8562
8563
# File 'lib/syntax_tree/node.rb', line 8561

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

#child_nodesObject Also known as: deconstruct



8565
8566
8567
# File 'lib/syntax_tree/node.rb', line 8565

def child_nodes
  []
end

#copy(beginning: nil, elements: nil, location: nil) ⇒ Object



8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
# File 'lib/syntax_tree/node.rb', line 8569

def copy(beginning: nil, elements: nil, location: nil)
  node =
    QSymbols.new(
      beginning: beginning || self.beginning,
      elements: elements || self.elements,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



8583
8584
8585
8586
8587
8588
8589
8590
# File 'lib/syntax_tree/node.rb', line 8583

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

#format(q) ⇒ Object



8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
# File 'lib/syntax_tree/node.rb', line 8592

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

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

  q.text(opening)
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(
        elements,
        ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
      ) { |element| q.format(element) }
    end
    q.breakable_empty
  end
  q.text(closing)
end