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.



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

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



8511
8512
8513
# File 'lib/syntax_tree/node.rb', line 8511

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8517
8518
8519
# File 'lib/syntax_tree/node.rb', line 8517

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8514
8515
8516
# File 'lib/syntax_tree/node.rb', line 8514

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8579
8580
8581
8582
# File 'lib/syntax_tree/node.rb', line 8579

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

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



8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
# File 'lib/syntax_tree/node.rb', line 8534

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



8548
8549
8550
8551
8552
8553
8554
8555
# File 'lib/syntax_tree/node.rb', line 8548

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

#format(q) ⇒ Object



8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
# File 'lib/syntax_tree/node.rb', line 8557

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