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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Words.



9673
9674
9675
9676
9677
9678
# File 'lib/syntax_tree/node.rb', line 9673

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



9665
9666
9667
# File 'lib/syntax_tree/node.rb', line 9665

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9671
9672
9673
# File 'lib/syntax_tree/node.rb', line 9671

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



9668
9669
9670
# File 'lib/syntax_tree/node.rb', line 9668

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9680
9681
9682
# File 'lib/syntax_tree/node.rb', line 9680

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

#child_nodesObject Also known as: deconstruct



9684
9685
9686
# File 'lib/syntax_tree/node.rb', line 9684

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



9690
9691
9692
9693
9694
9695
9696
9697
# File 'lib/syntax_tree/node.rb', line 9690

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

#format(q) ⇒ Object



9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
# File 'lib/syntax_tree/node.rb', line 9699

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