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.



8489
8490
8491
8492
8493
8494
# File 'lib/syntax_tree/node.rb', line 8489

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



8481
8482
8483
# File 'lib/syntax_tree/node.rb', line 8481

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8487
8488
8489
# File 'lib/syntax_tree/node.rb', line 8487

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



8484
8485
8486
# File 'lib/syntax_tree/node.rb', line 8484

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



8496
8497
8498
# File 'lib/syntax_tree/node.rb', line 8496

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

#child_nodesObject Also known as: deconstruct



8500
8501
8502
# File 'lib/syntax_tree/node.rb', line 8500

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



8506
8507
8508
8509
8510
8511
8512
8513
# File 'lib/syntax_tree/node.rb', line 8506

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

#format(q) ⇒ Object



8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
# File 'lib/syntax_tree/node.rb', line 8515

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