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.



8741
8742
8743
8744
8745
8746
# File 'lib/syntax_tree/node.rb', line 8741

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



8733
8734
8735
# File 'lib/syntax_tree/node.rb', line 8733

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8739
8740
8741
# File 'lib/syntax_tree/node.rb', line 8739

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



8736
8737
8738
# File 'lib/syntax_tree/node.rb', line 8736

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



8748
8749
8750
# File 'lib/syntax_tree/node.rb', line 8748

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

#child_nodesObject Also known as: deconstruct



8752
8753
8754
# File 'lib/syntax_tree/node.rb', line 8752

def child_nodes
  []
end

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



8756
8757
8758
8759
8760
8761
8762
# File 'lib/syntax_tree/node.rb', line 8756

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



8766
8767
8768
8769
8770
8771
8772
8773
# File 'lib/syntax_tree/node.rb', line 8766

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

#format(q) ⇒ Object



8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
# File 'lib/syntax_tree/node.rb', line 8775

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