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

#pretty_print, #to_json

Constructor Details

#initialize(beginning:, elements:, location:, comments: []) ⇒ Symbols

Returns a new instance of Symbols.



8384
8385
8386
8387
8388
8389
# File 'lib/syntax_tree/node.rb', line 8384

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



8376
8377
8378
# File 'lib/syntax_tree/node.rb', line 8376

def beginning
  @beginning
end

#comments ⇒ Object (readonly)

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



8382
8383
8384
# File 'lib/syntax_tree/node.rb', line 8382

def comments
  @comments
end

#elements ⇒ Object (readonly)

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



8379
8380
8381
# File 'lib/syntax_tree/node.rb', line 8379

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



8391
8392
8393
# File 'lib/syntax_tree/node.rb', line 8391

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

#child_nodes ⇒ Object Also known as: deconstruct



8395
8396
8397
# File 'lib/syntax_tree/node.rb', line 8395

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



8401
8402
8403
8404
8405
8406
8407
8408
# File 'lib/syntax_tree/node.rb', line 8401

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

#format(q) ⇒ Object



8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
# File 'lib/syntax_tree/node.rb', line 8410

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