Class: SyntaxTree::QSymbols

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

Overview

QSymbols represents a symbol literal array without interpolation.

%i[one two three]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of QSymbols.



9531
9532
9533
9534
9535
9536
# File 'lib/syntax_tree.rb', line 9531

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

Instance Attribute Details

#beginningObject (readonly)

QSymbolsBeg

the token that opens this array literal



9520
9521
9522
# File 'lib/syntax_tree.rb', line 9520

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9529
9530
9531
# File 'lib/syntax_tree.rb', line 9529

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



9523
9524
9525
# File 'lib/syntax_tree.rb', line 9523

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



9526
9527
9528
# File 'lib/syntax_tree.rb', line 9526

def location
  @location
end

Instance Method Details

#child_nodesObject



9538
9539
9540
# File 'lib/syntax_tree.rb', line 9538

def child_nodes
  []
end

#format(q) ⇒ Object



9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
# File 'lib/syntax_tree.rb', line 9542

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

  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



9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
# File 'lib/syntax_tree.rb', line 9561

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

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

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

#to_json(*opts) ⇒ Object



9572
9573
9574
9575
9576
9577
9578
9579
# File 'lib/syntax_tree.rb', line 9572

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