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

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(beginning:, elements:, location:, comments: []) ⇒ QWords



7413
7414
7415
7416
7417
7418
# File 'lib/syntax_tree/node.rb', line 7413

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



7405
7406
7407
# File 'lib/syntax_tree/node.rb', line 7405

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7411
7412
7413
# File 'lib/syntax_tree/node.rb', line 7411

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



7408
7409
7410
# File 'lib/syntax_tree/node.rb', line 7408

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



7420
7421
7422
# File 'lib/syntax_tree/node.rb', line 7420

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

#child_nodesObject Also known as: deconstruct



7424
7425
7426
# File 'lib/syntax_tree/node.rb', line 7424

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



7430
7431
7432
7433
7434
7435
7436
7437
# File 'lib/syntax_tree/node.rb', line 7430

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

#format(q) ⇒ Object



7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
# File 'lib/syntax_tree/node.rb', line 7439

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