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.



7112
7113
7114
7115
7116
7117
# File 'lib/syntax_tree/node.rb', line 7112

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



7104
7105
7106
# File 'lib/syntax_tree/node.rb', line 7104

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7110
7111
7112
# File 'lib/syntax_tree/node.rb', line 7110

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



7107
7108
7109
# File 'lib/syntax_tree/node.rb', line 7107

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



7119
7120
7121
# File 'lib/syntax_tree/node.rb', line 7119

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

#child_nodesObject Also known as: deconstruct



7123
7124
7125
# File 'lib/syntax_tree/node.rb', line 7123

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



7129
7130
7131
7132
7133
7134
7135
7136
# File 'lib/syntax_tree/node.rb', line 7129

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

#format(q) ⇒ Object



7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
# File 'lib/syntax_tree/node.rb', line 7138

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