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.



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

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



11884
11885
11886
# File 'lib/syntax_tree/node.rb', line 11884

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11890
11891
11892
# File 'lib/syntax_tree/node.rb', line 11890

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



11887
11888
11889
# File 'lib/syntax_tree/node.rb', line 11887

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



11948
11949
11950
11951
# File 'lib/syntax_tree/node.rb', line 11948

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

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



11907
11908
11909
11910
11911
11912
11913
# File 'lib/syntax_tree/node.rb', line 11907

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



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

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

#format(q) ⇒ Object



11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
# File 'lib/syntax_tree/node.rb', line 11926

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