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.



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

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



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

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10645
10646
10647
# File 'lib/syntax_tree/node.rb', line 10645

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



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

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



10703
10704
10705
10706
# File 'lib/syntax_tree/node.rb', line 10703

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

#accept(visitor) ⇒ Object



10654
10655
10656
# File 'lib/syntax_tree/node.rb', line 10654

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

#child_nodesObject Also known as: deconstruct



10658
10659
10660
# File 'lib/syntax_tree/node.rb', line 10658

def child_nodes
  []
end

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



10662
10663
10664
10665
10666
10667
10668
# File 'lib/syntax_tree/node.rb', line 10662

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



10672
10673
10674
10675
10676
10677
10678
10679
# File 'lib/syntax_tree/node.rb', line 10672

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

#format(q) ⇒ Object



10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
# File 'lib/syntax_tree/node.rb', line 10681

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