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



9578
9579
9580
9581
9582
9583
# File 'lib/syntax_tree.rb', line 9578

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



9567
9568
9569
# File 'lib/syntax_tree.rb', line 9567

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9576
9577
9578
# File 'lib/syntax_tree.rb', line 9576

def comments
  @comments
end

#elementsObject (readonly)

Array[ TStringContent ]

the elements of the array



9570
9571
9572
# File 'lib/syntax_tree.rb', line 9570

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



9573
9574
9575
# File 'lib/syntax_tree.rb', line 9573

def location
  @location
end

Instance Method Details

#child_nodesObject



9585
9586
9587
# File 'lib/syntax_tree.rb', line 9585

def child_nodes
  []
end

#format(q) ⇒ Object



9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
# File 'lib/syntax_tree.rb', line 9589

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



9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
# File 'lib/syntax_tree.rb', line 9608

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



9619
9620
9621
9622
9623
# File 'lib/syntax_tree.rb', line 9619

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