Class: SyntaxTree::QSymbols

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

QSymbols represents a symbol literal array without 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: []) ⇒ QSymbols

Returns a new instance of QSymbols.



7027
7028
7029
7030
7031
7032
# File 'lib/syntax_tree/node.rb', line 7027

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

Instance Attribute Details

#beginningObject (readonly)

QSymbolsBeg

the token that opens this array literal



7019
7020
7021
# File 'lib/syntax_tree/node.rb', line 7019

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7025
7026
7027
# File 'lib/syntax_tree/node.rb', line 7025

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



7022
7023
7024
# File 'lib/syntax_tree/node.rb', line 7022

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



7034
7035
7036
# File 'lib/syntax_tree/node.rb', line 7034

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

#child_nodesObject Also known as: deconstruct



7038
7039
7040
# File 'lib/syntax_tree/node.rb', line 7038

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



7044
7045
7046
7047
7048
7049
7050
7051
# File 'lib/syntax_tree/node.rb', line 7044

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

#format(q) ⇒ Object



7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
# File 'lib/syntax_tree/node.rb', line 7053

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