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



10223
10224
10225
10226
10227
10228
# File 'lib/syntax_tree/node.rb', line 10223

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



10215
10216
10217
# File 'lib/syntax_tree/node.rb', line 10215

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10221
10222
10223
# File 'lib/syntax_tree/node.rb', line 10221

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



10218
10219
10220
# File 'lib/syntax_tree/node.rb', line 10218

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



10230
10231
10232
# File 'lib/syntax_tree/node.rb', line 10230

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

#child_nodesObject Also known as: deconstruct



10234
10235
10236
# File 'lib/syntax_tree/node.rb', line 10234

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



10240
10241
10242
10243
10244
10245
10246
10247
# File 'lib/syntax_tree/node.rb', line 10240

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

#format(q) ⇒ Object



10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
# File 'lib/syntax_tree/node.rb', line 10249

def format(q)
  opening, closing = "%W[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.text(opening)
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(
        elements,
        ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
      ) { |element| q.format(element) }
    end
    q.breakable_empty
  end
  q.text(closing)
end