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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(beginning:, elements:, location:) ⇒ QWords

Returns a new instance of QWords.



8789
8790
8791
8792
8793
8794
# File 'lib/syntax_tree/node.rb', line 8789

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



8781
8782
8783
# File 'lib/syntax_tree/node.rb', line 8781

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8787
8788
8789
# File 'lib/syntax_tree/node.rb', line 8787

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8784
8785
8786
# File 'lib/syntax_tree/node.rb', line 8784

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8845
8846
8847
8848
# File 'lib/syntax_tree/node.rb', line 8845

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

#accept(visitor) ⇒ Object



8796
8797
8798
# File 'lib/syntax_tree/node.rb', line 8796

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

#child_nodesObject Also known as: deconstruct



8800
8801
8802
# File 'lib/syntax_tree/node.rb', line 8800

def child_nodes
  []
end

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



8804
8805
8806
8807
8808
8809
8810
# File 'lib/syntax_tree/node.rb', line 8804

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



8814
8815
8816
8817
8818
8819
8820
8821
# File 'lib/syntax_tree/node.rb', line 8814

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

#format(q) ⇒ Object



8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
# File 'lib/syntax_tree/node.rb', line 8823

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