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.



7220
7221
7222
7223
7224
7225
# File 'lib/syntax_tree/node.rb', line 7220

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



7212
7213
7214
# File 'lib/syntax_tree/node.rb', line 7212

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7218
7219
7220
# File 'lib/syntax_tree/node.rb', line 7218

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



7215
7216
7217
# File 'lib/syntax_tree/node.rb', line 7215

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



7227
7228
7229
# File 'lib/syntax_tree/node.rb', line 7227

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

#child_nodesObject Also known as: deconstruct



7231
7232
7233
# File 'lib/syntax_tree/node.rb', line 7231

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



7237
7238
7239
7240
7241
7242
7243
7244
# File 'lib/syntax_tree/node.rb', line 7237

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

#format(q) ⇒ Object



7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
# File 'lib/syntax_tree/node.rb', line 7246

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