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

Constructor Details

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

Returns a new instance of Words.



11198
11199
11200
11201
11202
11203
# File 'lib/syntax_tree/node.rb', line 11198

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

Instance Attribute Details

#beginningObject (readonly)

WordsBeg

the token that opens this array literal



11190
11191
11192
# File 'lib/syntax_tree/node.rb', line 11190

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11196
11197
11198
# File 'lib/syntax_tree/node.rb', line 11196

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



11193
11194
11195
# File 'lib/syntax_tree/node.rb', line 11193

def elements
  @elements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11205
11206
11207
# File 'lib/syntax_tree/node.rb', line 11205

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



11211
11212
11213
11214
11215
11216
11217
11218
# File 'lib/syntax_tree/node.rb', line 11211

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

#format(q) ⇒ Object



11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
# File 'lib/syntax_tree/node.rb', line 11220

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

#pretty_print(q) ⇒ Object



11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
# File 'lib/syntax_tree/node.rb', line 11239

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("words")

    q.breakable
    q.group(2, "(", ")") { q.seplist(elements) { |element| q.pp(element) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



11250
11251
11252
11253
11254
# File 'lib/syntax_tree/node.rb', line 11250

def to_json(*opts)
  { type: :words, elems: elements, loc: location, cmts: comments }.to_json(
    *opts
  )
end