Class: SyntaxTree::QWords

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

QWords represents a string literal array without 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: []) ⇒ QWords



6408
6409
6410
6411
6412
6413
# File 'lib/syntax_tree/node.rb', line 6408

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

Instance Attribute Details

#beginningObject (readonly)

QWordsBeg

the token that opens this array literal



6400
6401
6402
# File 'lib/syntax_tree/node.rb', line 6400

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6406
6407
6408
# File 'lib/syntax_tree/node.rb', line 6406

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



6403
6404
6405
# File 'lib/syntax_tree/node.rb', line 6403

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



6415
6416
6417
# File 'lib/syntax_tree/node.rb', line 6415

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

#child_nodesObject Also known as: deconstruct



6419
6420
6421
# File 'lib/syntax_tree/node.rb', line 6419

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6425
6426
6427
6428
6429
6430
6431
6432
# File 'lib/syntax_tree/node.rb', line 6425

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

#format(q) ⇒ Object



6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
# File 'lib/syntax_tree/node.rb', line 6434

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