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:) ⇒ QWords

Returns a new instance of QWords.



8641
8642
8643
8644
8645
8646
# File 'lib/syntax_tree/node.rb', line 8641

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

Instance Attribute Details

#beginningObject (readonly)

QWordsBeg

the token that opens this array literal



8633
8634
8635
# File 'lib/syntax_tree/node.rb', line 8633

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8639
8640
8641
# File 'lib/syntax_tree/node.rb', line 8639

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8636
8637
8638
# File 'lib/syntax_tree/node.rb', line 8636

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8697
8698
8699
8700
# File 'lib/syntax_tree/node.rb', line 8697

def ===(other)
  other.is_a?(QWords) && beginning === other.beginning &&
    ArrayMatch.call(elements, other.elements)
end

#accept(visitor) ⇒ Object



8648
8649
8650
# File 'lib/syntax_tree/node.rb', line 8648

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

#child_nodesObject Also known as: deconstruct



8652
8653
8654
# File 'lib/syntax_tree/node.rb', line 8652

def child_nodes
  []
end

#copy(beginning: nil, elements: nil, location: nil) ⇒ Object



8656
8657
8658
8659
8660
8661
8662
# File 'lib/syntax_tree/node.rb', line 8656

def copy(beginning: nil, elements: nil, location: nil)
  QWords.new(
    beginning: beginning || self.beginning,
    elements: elements || self.elements,
    location: location || self.location
  )
end

#deconstruct_keys(_keys) ⇒ Object



8666
8667
8668
8669
8670
8671
8672
8673
# File 'lib/syntax_tree/node.rb', line 8666

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

#format(q) ⇒ Object



8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
# File 'lib/syntax_tree/node.rb', line 8675

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