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.



10583
10584
10585
10586
10587
10588
# File 'lib/syntax_tree/node.rb', line 10583

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



10575
10576
10577
# File 'lib/syntax_tree/node.rb', line 10575

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10581
10582
10583
# File 'lib/syntax_tree/node.rb', line 10581

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



10578
10579
10580
# File 'lib/syntax_tree/node.rb', line 10578

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



10590
10591
10592
# File 'lib/syntax_tree/node.rb', line 10590

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

#child_nodesObject Also known as: deconstruct



10594
10595
10596
# File 'lib/syntax_tree/node.rb', line 10594

def child_nodes
  []
end

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



10598
10599
10600
10601
10602
10603
10604
# File 'lib/syntax_tree/node.rb', line 10598

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



10608
10609
10610
10611
10612
10613
10614
10615
# File 'lib/syntax_tree/node.rb', line 10608

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

#format(q) ⇒ Object



10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
# File 'lib/syntax_tree/node.rb', line 10617

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