class Solargraph::Parser::Rubyvm::NodeChainer

A factory for generating chains from nodes.

Constants

Chain

Public Class Methods

chain(node, filename = nil, in_block = false) click to toggle source

@param node [Parser::AST::Node] @param filename [String] @return [Source::Chain]

# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 31
def chain node, filename = nil, in_block = false
  NodeChainer.new(node, filename, in_block).chain
end
load_string(code) click to toggle source

@param code [String] @return [Source::Chain]

# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 37
def load_string(code)
  node = Parser.parse(code.sub(/\.$/, ''))
  chain = NodeChainer.new(node).chain
  chain.links.push(Chain::Link.new) if code.end_with?('.')
  chain
end
new(node, filename = nil, in_block = false) click to toggle source

@param node [Parser::AST::Node] @param filename [String]

# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 15
def initialize node, filename = nil, in_block = false
  @node = node
  @filename = filename
  @in_block = in_block ? 1 : 0
end

Public Instance Methods

chain() click to toggle source

@return [Source::Chain]

# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 22
def chain
  links = generate_links(@node)
  Chain.new(links, @node, (Parser.is_ast_node?(@node) && @node.type == :SPLAT))
end

Private Instance Methods

block_passed?(node) click to toggle source
# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 130
def block_passed? node
  node.children.last.is_a?(RubyVM::AbstractSyntaxTree::Node) && node.children.last.type == :BLOCK_PASS
end
hash_is_splatted?(node) click to toggle source
# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 123
def hash_is_splatted? node
  return false unless Parser.is_ast_node?(node.children[0]) && node.children[0].type == :LIST
  list = node.children[0].children
  eol = list.rindex(&:nil?)
  eol && Parser.is_ast_node?(list[eol + 1])
end
node_to_argchains(node) click to toggle source
# File lib/solargraph/parser/rubyvm/node_chainer.rb, line 134
def node_to_argchains node
  return [] unless Parser.is_ast_node?(node)
  if [:ZARRAY, :ARRAY, :LIST].include?(node.type)
    node.children[0..-2].map { |c| NodeChainer.chain(c) }
  elsif node.type == :SPLAT
    [NodeChainer.chain(node)]
  elsif node.type == :ARGSPUSH
    result = node_to_argchains(node.children[0])
    result.push NodeChainer.chain(node.children[1]) if Parser.is_ast_node?(node.children[1])
  elsif node.type == :ARGSCAT
    result = node.children[0].children[0..-2].map { |c| NodeChainer.chain(c) }
    result.push NodeChainer.chain(node.children[1])
    # @todo Smelly instance variable access
    result.last.instance_variable_set(:@splat, true)
    result
  elsif node.type == :BLOCK_PASS
    result = node_to_argchains(node.children[0])
    result.push Chain.new([Chain::BlockVariable.new("&#{node.children[1].children[0].to_s}")])
    result
  else
    []
  end
end