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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of QSymbols.



6850
6851
6852
6853
6854
6855
# File 'lib/syntax_tree/node.rb', line 6850

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



6842
6843
6844
# File 'lib/syntax_tree/node.rb', line 6842

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6848
6849
6850
# File 'lib/syntax_tree/node.rb', line 6848

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



6845
6846
6847
# File 'lib/syntax_tree/node.rb', line 6845

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



6857
6858
6859
# File 'lib/syntax_tree/node.rb', line 6857

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

#child_nodesObject Also known as: deconstruct



6861
6862
6863
# File 'lib/syntax_tree/node.rb', line 6861

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6867
6868
6869
6870
6871
6872
6873
6874
# File 'lib/syntax_tree/node.rb', line 6867

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

#format(q) ⇒ Object



6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
# File 'lib/syntax_tree/node.rb', line 6876

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