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:, comments: []) ⇒ Symbols

Returns a new instance of Symbols.



8566
8567
8568
8569
8570
8571
# File 'lib/syntax_tree/node.rb', line 8566

def initialize(beginning:, elements:, location:, comments: [])
  @beginning = beginning
  @elements = elements
  @location = location
  @comments = comments
end

Instance Attribute Details

#beginningObject (readonly)

SymbolsBeg

the token that opens this array literal



8558
8559
8560
# File 'lib/syntax_tree/node.rb', line 8558

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8564
8565
8566
# File 'lib/syntax_tree/node.rb', line 8564

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



8561
8562
8563
# File 'lib/syntax_tree/node.rb', line 8561

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



8573
8574
8575
# File 'lib/syntax_tree/node.rb', line 8573

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

#child_nodesObject Also known as: deconstruct



8577
8578
8579
# File 'lib/syntax_tree/node.rb', line 8577

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



8583
8584
8585
8586
8587
8588
8589
8590
# File 'lib/syntax_tree/node.rb', line 8583

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

#format(q) ⇒ Object



8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
# File 'lib/syntax_tree/node.rb', line 8592

def format(q)
  opening, closing = "%I[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.group(0, opening, closing) do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end