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.



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

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



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

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8802
8803
8804
# File 'lib/syntax_tree/node.rb', line 8802

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



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

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8860
8861
8862
8863
# File 'lib/syntax_tree/node.rb', line 8860

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

#accept(visitor) ⇒ Object



8811
8812
8813
# File 'lib/syntax_tree/node.rb', line 8811

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

#child_nodesObject Also known as: deconstruct



8815
8816
8817
# File 'lib/syntax_tree/node.rb', line 8815

def child_nodes
  []
end

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



8819
8820
8821
8822
8823
8824
8825
# File 'lib/syntax_tree/node.rb', line 8819

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



8829
8830
8831
8832
8833
8834
8835
8836
# File 'lib/syntax_tree/node.rb', line 8829

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

#format(q) ⇒ Object



8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
# File 'lib/syntax_tree/node.rb', line 8838

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