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



8772
8773
8774
8775
8776
8777
# File 'lib/syntax_tree/node.rb', line 8772

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



8764
8765
8766
# File 'lib/syntax_tree/node.rb', line 8764

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8770
8771
8772
# File 'lib/syntax_tree/node.rb', line 8770

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8767
8768
8769
# File 'lib/syntax_tree/node.rb', line 8767

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



8828
8829
8830
8831
# File 'lib/syntax_tree/node.rb', line 8828

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

#accept(visitor) ⇒ Object



8779
8780
8781
# File 'lib/syntax_tree/node.rb', line 8779

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

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



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

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



8797
8798
8799
8800
8801
8802
8803
8804
# File 'lib/syntax_tree/node.rb', line 8797

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

#format(q) ⇒ Object



8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
# File 'lib/syntax_tree/node.rb', line 8806

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