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.



9908
9909
9910
9911
9912
9913
# File 'lib/syntax_tree/node.rb', line 9908

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

Instance Attribute Details

#beginning ⇒ Object (readonly)

[WordsBeg] the token that opens this array literal



9900
9901
9902
# File 'lib/syntax_tree/node.rb', line 9900

def beginning
  @beginning
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



9906
9907
9908
# File 'lib/syntax_tree/node.rb', line 9906

def comments
  @comments
end

#elements ⇒ Object (readonly)

[Array[ Word ]] the elements of this array



9903
9904
9905
# File 'lib/syntax_tree/node.rb', line 9903

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9915
9916
9917
# File 'lib/syntax_tree/node.rb', line 9915

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

#child_nodes ⇒ Object Also known as: deconstruct



9919
9920
9921
# File 'lib/syntax_tree/node.rb', line 9919

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



9925
9926
9927
9928
9929
9930
9931
9932
# File 'lib/syntax_tree/node.rb', line 9925

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

#format(q) ⇒ Object



9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
# File 'lib/syntax_tree/node.rb', line 9934

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