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.



9455
9456
9457
9458
9459
9460
# File 'lib/syntax_tree.rb', line 9455

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



9444
9445
9446
# File 'lib/syntax_tree.rb', line 9444

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9453
9454
9455
# File 'lib/syntax_tree.rb', line 9453

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



9447
9448
9449
# File 'lib/syntax_tree.rb', line 9447

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



9450
9451
9452
# File 'lib/syntax_tree.rb', line 9450

def location
  @location
end

Instance Method Details

#child_nodesObject



9462
9463
9464
# File 'lib/syntax_tree.rb', line 9462

def child_nodes
  []
end

#format(q) ⇒ Object



9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
# File 'lib/syntax_tree.rb', line 9466

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



9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
# File 'lib/syntax_tree.rb', line 9485

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



9496
9497
9498
9499
9500
9501
9502
9503
# File 'lib/syntax_tree.rb', line 9496

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