Class: SyntaxTree::Words

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

Overview

Words represents a string literal array with interpolation.

%W[one two three]

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(beginning:, elements:, location:) ⇒ Words

Returns a new instance of Words.



11901
11902
11903
11904
11905
11906
# File 'lib/syntax_tree/node.rb', line 11901

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

Instance Attribute Details

#beginningObject (readonly)

WordsBeg

the token that opens this array literal



11893
11894
11895
# File 'lib/syntax_tree/node.rb', line 11893

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11899
11900
11901
# File 'lib/syntax_tree/node.rb', line 11899

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



11896
11897
11898
# File 'lib/syntax_tree/node.rb', line 11896

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



11957
11958
11959
11960
# File 'lib/syntax_tree/node.rb', line 11957

def ===(other)
  other.is_a?(Words) && beginning === other.beginning &&
    ArrayMatch.call(elements, other.elements)
end

#accept(visitor) ⇒ Object



11908
11909
11910
# File 'lib/syntax_tree/node.rb', line 11908

def accept(visitor)
  visitor.visit_words(self)
end

#child_nodesObject Also known as: deconstruct



11912
11913
11914
# File 'lib/syntax_tree/node.rb', line 11912

def child_nodes
  []
end

#copy(beginning: nil, elements: nil, location: nil) ⇒ Object



11916
11917
11918
11919
11920
11921
11922
# File 'lib/syntax_tree/node.rb', line 11916

def copy(beginning: nil, elements: nil, location: nil)
  Words.new(
    beginning: beginning || self.beginning,
    elements: elements || self.elements,
    location: location || self.location
  )
end

#deconstruct_keys(_keys) ⇒ Object



11926
11927
11928
11929
11930
11931
11932
11933
# File 'lib/syntax_tree/node.rb', line 11926

def deconstruct_keys(_keys)
  {
    beginning: beginning,
    elements: elements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
# File 'lib/syntax_tree/node.rb', line 11935

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

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.text(opening)
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(
        elements,
        ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
      ) { |element| q.format(element) }
    end
    q.breakable_empty
  end
  q.text(closing)
end