Class: SyntaxTree::Words

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

Overview

Words represents a string literal array with interpolation.

%W[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: []) ⇒ Words

Returns a new instance of Words.



9863
9864
9865
9866
9867
9868
# File 'lib/syntax_tree/node.rb', line 9863

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

Instance Attribute Details

#beginningObject (readonly)

WordsBeg

the token that opens this array literal



9855
9856
9857
# File 'lib/syntax_tree/node.rb', line 9855

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9861
9862
9863
# File 'lib/syntax_tree/node.rb', line 9861

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



9858
9859
9860
# File 'lib/syntax_tree/node.rb', line 9858

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9870
9871
9872
# File 'lib/syntax_tree/node.rb', line 9870

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

#child_nodesObject Also known as: deconstruct



9874
9875
9876
# File 'lib/syntax_tree/node.rb', line 9874

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



9880
9881
9882
9883
9884
9885
9886
9887
# File 'lib/syntax_tree/node.rb', line 9880

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

#format(q) ⇒ Object



9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
# File 'lib/syntax_tree/node.rb', line 9889

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

  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