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.



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

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



10623
10624
10625
# File 'lib/syntax_tree/node.rb', line 10623

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10629
10630
10631
# File 'lib/syntax_tree/node.rb', line 10629

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



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

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



10687
10688
10689
10690
# File 'lib/syntax_tree/node.rb', line 10687

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

#accept(visitor) ⇒ Object



10638
10639
10640
# File 'lib/syntax_tree/node.rb', line 10638

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

#child_nodesObject Also known as: deconstruct



10642
10643
10644
# File 'lib/syntax_tree/node.rb', line 10642

def child_nodes
  []
end

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



10646
10647
10648
10649
10650
10651
10652
# File 'lib/syntax_tree/node.rb', line 10646

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



10656
10657
10658
10659
10660
10661
10662
10663
# File 'lib/syntax_tree/node.rb', line 10656

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

#format(q) ⇒ Object



10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
# File 'lib/syntax_tree/node.rb', line 10665

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