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.



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

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



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

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



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

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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