Class: SyntaxTree::QWords

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

Overview

QWords represents a string literal array without interpolation.

%w[one two three]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of QWords.



9654
9655
9656
9657
9658
9659
# File 'lib/syntax_tree.rb', line 9654

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



9643
9644
9645
# File 'lib/syntax_tree.rb', line 9643

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9652
9653
9654
# File 'lib/syntax_tree.rb', line 9652

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



9646
9647
9648
# File 'lib/syntax_tree.rb', line 9646

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



9649
9650
9651
# File 'lib/syntax_tree.rb', line 9649

def location
  @location
end

Instance Method Details

#child_nodesObject



9661
9662
9663
# File 'lib/syntax_tree.rb', line 9661

def child_nodes
  []
end

#format(q) ⇒ Object



9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
# File 'lib/syntax_tree.rb', line 9665

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



9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
# File 'lib/syntax_tree.rb', line 9684

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

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

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

#to_json(*opts) ⇒ Object



9695
9696
9697
9698
9699
# File 'lib/syntax_tree.rb', line 9695

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