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, #pretty_print, #to_json

Constructor Details

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



10490
10491
10492
10493
10494
10495
# File 'lib/syntax_tree/node.rb', line 10490

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



10482
10483
10484
# File 'lib/syntax_tree/node.rb', line 10482

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10488
10489
10490
# File 'lib/syntax_tree/node.rb', line 10488

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



10485
10486
10487
# File 'lib/syntax_tree/node.rb', line 10485

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



10546
10547
10548
10549
# File 'lib/syntax_tree/node.rb', line 10546

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

#accept(visitor) ⇒ Object



10497
10498
10499
# File 'lib/syntax_tree/node.rb', line 10497

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

#child_nodesObject Also known as: deconstruct



10501
10502
10503
# File 'lib/syntax_tree/node.rb', line 10501

def child_nodes
  []
end

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



10505
10506
10507
10508
10509
10510
10511
# File 'lib/syntax_tree/node.rb', line 10505

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



10515
10516
10517
10518
10519
10520
10521
10522
# File 'lib/syntax_tree/node.rb', line 10515

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

#format(q) ⇒ Object



10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
# File 'lib/syntax_tree/node.rb', line 10524

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