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.



9786
9787
9788
9789
9790
9791
# File 'lib/syntax_tree.rb', line 9786

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



9775
9776
9777
# File 'lib/syntax_tree.rb', line 9775

def beginning
  @beginning
end

#commentsObject (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



9784
9785
9786
# File 'lib/syntax_tree.rb', line 9784

def comments
  @comments
end

#elementsObject (readonly)

[Array[ TStringContent ]] the elements of the array



9778
9779
9780
# File 'lib/syntax_tree.rb', line 9778

def elements
  @elements
end

#locationObject (readonly)

[Location] the location of this node



9781
9782
9783
# File 'lib/syntax_tree.rb', line 9781

def location
  @location
end

Instance Method Details

#child_nodesObject



9793
9794
9795
# File 'lib/syntax_tree.rb', line 9793

def child_nodes
  []
end

#format(q) ⇒ Object



9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
# File 'lib/syntax_tree.rb', line 9797

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



9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
# File 'lib/syntax_tree.rb', line 9816

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



9827
9828
9829
9830
9831
# File 'lib/syntax_tree.rb', line 9827

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