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.



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

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



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

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



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

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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