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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Words.



11654
11655
11656
11657
11658
11659
# File 'lib/syntax_tree/node.rb', line 11654

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



11643
11644
11645
# File 'lib/syntax_tree/node.rb', line 11643

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11652
11653
11654
# File 'lib/syntax_tree/node.rb', line 11652

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



11646
11647
11648
# File 'lib/syntax_tree/node.rb', line 11646

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



11649
11650
11651
# File 'lib/syntax_tree/node.rb', line 11649

def location
  @location
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11661
11662
11663
# File 'lib/syntax_tree/node.rb', line 11661

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



11667
11668
11669
11670
11671
11672
11673
11674
# File 'lib/syntax_tree/node.rb', line 11667

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

#format(q) ⇒ Object



11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
# File 'lib/syntax_tree/node.rb', line 11676

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



11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
# File 'lib/syntax_tree/node.rb', line 11695

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



11706
11707
11708
11709
11710
# File 'lib/syntax_tree/node.rb', line 11706

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