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.



9109
9110
9111
9112
9113
9114
# File 'lib/syntax_tree/node.rb', line 9109

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



9101
9102
9103
# File 'lib/syntax_tree/node.rb', line 9101

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9107
9108
9109
# File 'lib/syntax_tree/node.rb', line 9107

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



9104
9105
9106
# File 'lib/syntax_tree/node.rb', line 9104

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9116
9117
9118
# File 'lib/syntax_tree/node.rb', line 9116

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

#child_nodesObject Also known as: deconstruct



9120
9121
9122
# File 'lib/syntax_tree/node.rb', line 9120

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



9126
9127
9128
9129
9130
9131
9132
9133
# File 'lib/syntax_tree/node.rb', line 9126

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

#format(q) ⇒ Object



9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
# File 'lib/syntax_tree/node.rb', line 9135

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