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.



8611
8612
8613
8614
8615
8616
# File 'lib/syntax_tree/node.rb', line 8611

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



8603
8604
8605
# File 'lib/syntax_tree/node.rb', line 8603

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8609
8610
8611
# File 'lib/syntax_tree/node.rb', line 8609

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



8606
8607
8608
# File 'lib/syntax_tree/node.rb', line 8606

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



8618
8619
8620
# File 'lib/syntax_tree/node.rb', line 8618

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

#child_nodesObject Also known as: deconstruct



8622
8623
8624
# File 'lib/syntax_tree/node.rb', line 8622

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



8628
8629
8630
8631
8632
8633
8634
8635
# File 'lib/syntax_tree/node.rb', line 8628

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

#format(q) ⇒ Object



8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
# File 'lib/syntax_tree/node.rb', line 8637

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