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

Constructor Details

#initialize(beginning:, elements:, location:, comments: []) ⇒ QWords

Returns a new instance of QWords.



7042
7043
7044
7045
7046
7047
# File 'lib/syntax_tree/node.rb', line 7042

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

Instance Attribute Details

#beginningObject (readonly)

QWordsBeg

the token that opens this array literal



7034
7035
7036
# File 'lib/syntax_tree/node.rb', line 7034

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7040
7041
7042
# File 'lib/syntax_tree/node.rb', line 7040

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



7037
7038
7039
# File 'lib/syntax_tree/node.rb', line 7037

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



7049
7050
7051
# File 'lib/syntax_tree/node.rb', line 7049

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

#child_nodesObject Also known as: deconstruct



7053
7054
7055
# File 'lib/syntax_tree/node.rb', line 7053

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



7059
7060
7061
7062
7063
7064
7065
7066
# File 'lib/syntax_tree/node.rb', line 7059

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

#format(q) ⇒ Object



7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
# File 'lib/syntax_tree/node.rb', line 7068

def format(q)
  opening, closing = "%w[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.group(0, opening, closing) do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end