Class: SyntaxTree::Symbols

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

Overview

Symbols represents a symbol array literal with interpolation.

%I[one two three]

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of Symbols.



10614
10615
10616
10617
10618
10619
# File 'lib/syntax_tree/node.rb', line 10614

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

Instance Attribute Details

#beginningObject (readonly)

SymbolsBeg

the token that opens this array literal



10606
10607
10608
# File 'lib/syntax_tree/node.rb', line 10606

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10612
10613
10614
# File 'lib/syntax_tree/node.rb', line 10612

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



10609
10610
10611
# File 'lib/syntax_tree/node.rb', line 10609

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



10670
10671
10672
10673
# File 'lib/syntax_tree/node.rb', line 10670

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

#accept(visitor) ⇒ Object



10621
10622
10623
# File 'lib/syntax_tree/node.rb', line 10621

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

#child_nodesObject Also known as: deconstruct



10625
10626
10627
# File 'lib/syntax_tree/node.rb', line 10625

def child_nodes
  []
end

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



10629
10630
10631
10632
10633
10634
10635
# File 'lib/syntax_tree/node.rb', line 10629

def copy(beginning: nil, elements: nil, location: nil)
  Symbols.new(
    beginning: beginning || self.beginning,
    elements: elements || self.elements,
    location: location || self.location
  )
end

#deconstruct_keys(_keys) ⇒ Object



10639
10640
10641
10642
10643
10644
10645
10646
# File 'lib/syntax_tree/node.rb', line 10639

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

#format(q) ⇒ Object



10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
# File 'lib/syntax_tree/node.rb', line 10648

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