Class: SyntaxTree::Words

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Words represents a string literal array with 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:) ⇒ Words

Returns a new instance of Words.



12022
12023
12024
12025
12026
12027
# File 'lib/syntax_tree/node.rb', line 12022

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

Instance Attribute Details

#beginningObject (readonly)

WordsBeg

the token that opens this array literal



12014
12015
12016
# File 'lib/syntax_tree/node.rb', line 12014

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12020
12021
12022
# File 'lib/syntax_tree/node.rb', line 12020

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



12017
12018
12019
# File 'lib/syntax_tree/node.rb', line 12017

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



12078
12079
12080
12081
# File 'lib/syntax_tree/node.rb', line 12078

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

#accept(visitor) ⇒ Object



12029
12030
12031
# File 'lib/syntax_tree/node.rb', line 12029

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

#child_nodesObject Also known as: deconstruct



12033
12034
12035
# File 'lib/syntax_tree/node.rb', line 12033

def child_nodes
  []
end

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



12037
12038
12039
12040
12041
12042
12043
# File 'lib/syntax_tree/node.rb', line 12037

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

#deconstruct_keys(_keys) ⇒ Object



12047
12048
12049
12050
12051
12052
12053
12054
# File 'lib/syntax_tree/node.rb', line 12047

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

#format(q) ⇒ Object



12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
# File 'lib/syntax_tree/node.rb', line 12056

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