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.



11931
11932
11933
11934
11935
11936
# File 'lib/syntax_tree/node.rb', line 11931

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



11923
11924
11925
# File 'lib/syntax_tree/node.rb', line 11923

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11929
11930
11931
# File 'lib/syntax_tree/node.rb', line 11929

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



11926
11927
11928
# File 'lib/syntax_tree/node.rb', line 11926

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



11987
11988
11989
11990
# File 'lib/syntax_tree/node.rb', line 11987

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

#accept(visitor) ⇒ Object



11938
11939
11940
# File 'lib/syntax_tree/node.rb', line 11938

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

#child_nodesObject Also known as: deconstruct



11942
11943
11944
# File 'lib/syntax_tree/node.rb', line 11942

def child_nodes
  []
end

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



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

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



11956
11957
11958
11959
11960
11961
11962
11963
# File 'lib/syntax_tree/node.rb', line 11956

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

#format(q) ⇒ Object



11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
# File 'lib/syntax_tree/node.rb', line 11965

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