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.



8528
8529
8530
8531
8532
8533
# File 'lib/syntax_tree/node.rb', line 8528

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



8520
8521
8522
# File 'lib/syntax_tree/node.rb', line 8520

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8526
8527
8528
# File 'lib/syntax_tree/node.rb', line 8526

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8523
8524
8525
# File 'lib/syntax_tree/node.rb', line 8523

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8588
8589
8590
8591
# File 'lib/syntax_tree/node.rb', line 8588

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

#accept(visitor) ⇒ Object



8535
8536
8537
# File 'lib/syntax_tree/node.rb', line 8535

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

#child_nodesObject Also known as: deconstruct



8539
8540
8541
# File 'lib/syntax_tree/node.rb', line 8539

def child_nodes
  []
end

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



8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
# File 'lib/syntax_tree/node.rb', line 8543

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



8557
8558
8559
8560
8561
8562
8563
8564
# File 'lib/syntax_tree/node.rb', line 8557

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

#format(q) ⇒ Object



8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
# File 'lib/syntax_tree/node.rb', line 8566

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