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.



9947
9948
9949
9950
9951
9952
# File 'lib/syntax_tree/node.rb', line 9947

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



9939
9940
9941
# File 'lib/syntax_tree/node.rb', line 9939

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9945
9946
9947
# File 'lib/syntax_tree/node.rb', line 9945

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



9942
9943
9944
# File 'lib/syntax_tree/node.rb', line 9942

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9954
9955
9956
# File 'lib/syntax_tree/node.rb', line 9954

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

#child_nodesObject Also known as: deconstruct



9958
9959
9960
# File 'lib/syntax_tree/node.rb', line 9958

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



9964
9965
9966
9967
9968
9969
9970
9971
# File 'lib/syntax_tree/node.rb', line 9964

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

#format(q) ⇒ Object



9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
# File 'lib/syntax_tree/node.rb', line 9973

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