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.



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

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

Instance Attribute Details

#beginning ⇒ Object (readonly)

[SymbolsBeg] the token that opens this array literal



8592
8593
8594
# File 'lib/syntax_tree/node.rb', line 8592

def beginning
  @beginning
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



8598
8599
8600
# File 'lib/syntax_tree/node.rb', line 8598

def comments
  @comments
end

#elements ⇒ Object (readonly)

[Array[ Word ]] the words in the symbol array literal



8595
8596
8597
# File 'lib/syntax_tree/node.rb', line 8595

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodes ⇒ Object Also known as: deconstruct



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

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



8617
8618
8619
8620
8621
8622
8623
8624
# File 'lib/syntax_tree/node.rb', line 8617

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

#format(q) ⇒ Object



8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
# File 'lib/syntax_tree/node.rb', line 8626

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