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.



9786
9787
9788
9789
9790
9791
# File 'lib/syntax_tree/node.rb', line 9786

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



9778
9779
9780
# File 'lib/syntax_tree/node.rb', line 9778

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9784
9785
9786
# File 'lib/syntax_tree/node.rb', line 9784

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



9781
9782
9783
# File 'lib/syntax_tree/node.rb', line 9781

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9793
9794
9795
# File 'lib/syntax_tree/node.rb', line 9793

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

#child_nodesObject Also known as: deconstruct



9797
9798
9799
# File 'lib/syntax_tree/node.rb', line 9797

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



9803
9804
9805
9806
9807
9808
9809
9810
# File 'lib/syntax_tree/node.rb', line 9803

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

#format(q) ⇒ Object



9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
# File 'lib/syntax_tree/node.rb', line 9812

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