Module: Solargraph::Parser::Rubyvm::ClassMethods

Defined in:
lib/solargraph/parser/rubyvm/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#chain(*args) ⇒ Object



82
83
84
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 82

def chain *args
  NodeChainer.chain *args
end

#chain_string(*args) ⇒ Object



86
87
88
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 86

def chain_string *args
  NodeChainer.load_string *args
end

#infer_literal_node_type(node) ⇒ Object



94
95
96
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 94

def infer_literal_node_type node
  NodeMethods.infer_literal_node_type node
end

#inner_node_references(name, top) ⇒ Array<AST::Node>

Parameters:

  • name (String)
  • top (AST::Node)

Returns:

  • (Array<AST::Node>)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 61

def inner_node_references name, top
  result = []
  if Parser.rubyvm?
    if Parser.is_ast_node?(top)
      result.push top if match_rubyvm_node_to_ref(top, name)
      top.children.each { |c| result.concat inner_node_references(name, c) }
    end
  else
    if Parser.is_ast_node?(top) && top.to_s.include?(":#{name}")
      result.push top if top.children.any? { |c| c.to_s == name }
      top.children.each { |c| result.concat inner_node_references(name, c) }
    end
  end
  result
end

#is_ast_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 102

def is_ast_node? node
  if Parser.rubyvm?
    node.is_a?(RubyVM::AbstractSyntaxTree::Node)
  else
    node.is_a?(::Parser::AST::Node)
  end
end

#map(source) ⇒ Object



33
34
35
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 33

def map source
  NodeProcessor.process(source.node, Region.new(source: source))
end

#match_rubyvm_node_to_ref(top, name) ⇒ Object



77
78
79
80
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 77

def match_rubyvm_node_to_ref(top, name)
  top.children.select { |c| c.is_a?(Symbol) }.any? { |c| c.to_s == name } ||
    top.children.select { |c| c.is_a?(Array) }.any? { |c| c.include?(name.to_sym) }
end

#node_range(node) ⇒ Object



110
111
112
113
114
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 110

def node_range node
  st = Position.new(node.first_lineno - 1, node.first_column)
  en = Position.new(node.last_lineno - 1, node.last_column)
  Range.new(st, en)
end

#parse(code, filename = nil, line = 0) ⇒ Parser::AST::Node

@sg-ignore

Parameters:

  • code (String)
  • filename (String, nil) (defaults to: nil)
  • line (Integer) (defaults to: 0)

Returns:

  • (Parser::AST::Node)


26
27
28
29
30
31
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 26

def parse code, filename = nil, line = 0
  node = RubyVM::AbstractSyntaxTree.parse(code).children[2]
  node and RubyVM::AbstractSyntaxTree::NodeWrapper.from(node, code.lines)
rescue ::SyntaxError => e
  raise Parser::SyntaxError, e.message
end

#parse_with_comments(code, filename = nil) ⇒ Array(Parser::AST::Node, Array<Parser::Source::Comment>)

@sg-ignore

Parameters:

  • code (String)
  • filename (String) (defaults to: nil)

Returns:

  • (Array(Parser::AST::Node, Array<Parser::Source::Comment>))


12
13
14
15
16
17
18
19
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 12

def parse_with_comments code, filename = nil
  node = RubyVM::AbstractSyntaxTree.parse(code).children[2]
  node &&= RubyVM::AbstractSyntaxTree::NodeWrapper.from(node, code.lines)
  comments = CommentRipper.new(code).parse
  [node, comments]
rescue ::SyntaxError => e
  raise Parser::SyntaxError, e.message
end

#process_node(*args) ⇒ Object



90
91
92
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 90

def process_node *args
  Solargraph::Parser::NodeProcessor.process *args
end

#recipient_node(tree) ⇒ Object



116
117
118
119
120
121
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 116

def recipient_node tree
  tree.each_with_index do |node, idx|
    return tree[idx + 1] if [:ARRAY, :ZARRAY, :LIST].include?(node.type) && tree[idx + 1] && [:FCALL, :VCALL, :CALL].include?(tree[idx + 1].type)
  end
  nil
end

#references(source, name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 37

def references source, name
  if name.end_with?("=")
    reg = /#{Regexp.escape name[0..-2]}\s*=/
    extract_offset = ->(code, offset) { reg.match(code, offset).offset(0) }
  else
    extract_offset = ->(code, offset) { [soff = code.index(name, offset), soff + name.length] }
  end
  inner_node_references(name, source.node).map do |n|
    rng = Range.from_node(n)
    offset = Position.to_offset(source.code, rng.start)
    soff, eoff = extract_offset[source.code, offset]
    Location.new(
      source.filename,
      Range.new(
        Position.from_offset(source.code, soff),
        Position.from_offset(source.code, eoff)
      )
    )
  end
end

#string_ranges(node) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 123

def string_ranges node
  return [] unless is_ast_node?(node)
  result = []
  if node.type == :STR
    result.push Range.from_node(node)
  elsif node.type == :DSTR
    here = Range.from_node(node)
    there = Range.from_node(node.children[1])
    result.push Range.new(here.start, there&.start || here.ending)
  end
  node.children.each do |child|
    result.concat string_ranges(child)
  end
  if node.type == :DSTR && node.children.last.nil?
    last = node.children[-2]
    unless last.nil?
      rng = Range.from_node(last)
      pos = Position.new(rng.ending.line, rng.ending.column - 1)
      result.push Range.new(pos, pos)
    end
  end
  result
end

#versionObject



98
99
100
# File 'lib/solargraph/parser/rubyvm/class_methods.rb', line 98

def version
  Ruby::VERSION
end