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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Symbols.



8881
8882
8883
8884
8885
8886
# File 'lib/syntax_tree/node.rb', line 8881

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



8873
8874
8875
# File 'lib/syntax_tree/node.rb', line 8873

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8879
8880
8881
# File 'lib/syntax_tree/node.rb', line 8879

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



8876
8877
8878
# File 'lib/syntax_tree/node.rb', line 8876

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



8888
8889
8890
# File 'lib/syntax_tree/node.rb', line 8888

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

#child_nodesObject Also known as: deconstruct



8892
8893
8894
# File 'lib/syntax_tree/node.rb', line 8892

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



8898
8899
8900
8901
8902
8903
8904
8905
# File 'lib/syntax_tree/node.rb', line 8898

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

#format(q) ⇒ Object



8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
# File 'lib/syntax_tree/node.rb', line 8907

def format(q)
  opening, closing = "%I[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.text(opening)
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(
        elements,
        ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
      ) { |element| q.format(element) }
    end
    q.breakable_empty
  end
  q.text(closing)
end