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

Returns a new instance of Symbols.



10455
10456
10457
10458
10459
10460
# File 'lib/syntax_tree/node.rb', line 10455

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



10447
10448
10449
# File 'lib/syntax_tree/node.rb', line 10447

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10453
10454
10455
# File 'lib/syntax_tree/node.rb', line 10453

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



10450
10451
10452
# File 'lib/syntax_tree/node.rb', line 10450

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



10511
10512
10513
10514
# File 'lib/syntax_tree/node.rb', line 10511

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

#accept(visitor) ⇒ Object



10462
10463
10464
# File 'lib/syntax_tree/node.rb', line 10462

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

#child_nodesObject Also known as: deconstruct



10466
10467
10468
# File 'lib/syntax_tree/node.rb', line 10466

def child_nodes
  []
end

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



10470
10471
10472
10473
10474
10475
10476
# File 'lib/syntax_tree/node.rb', line 10470

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



10480
10481
10482
10483
10484
10485
10486
10487
# File 'lib/syntax_tree/node.rb', line 10480

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

#format(q) ⇒ Object



10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
# File 'lib/syntax_tree/node.rb', line 10489

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