Module: Solargraph::Parser::Legacy::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#chain(*args) ⇒ Object



83
84
85
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 83

def chain *args
  NodeChainer.chain *args
end

#chain_string(*args) ⇒ Object



87
88
89
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 87

def chain_string *args
  NodeChainer.load_string *args
end

#infer_literal_node_type(node) ⇒ Object



95
96
97
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 95

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:



74
75
76
77
78
79
80
81
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 74

def inner_node_references name, top
  result = []
  if top.is_a?(AST::Node) && 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
  result
end

#is_ast_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 103

def is_ast_node? node
  node.is_a?(::Parser::AST::Node)
end

#map(source) ⇒ Object



42
43
44
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 42

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

#node_range(node) ⇒ Object



107
108
109
110
111
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 107

def node_range node
  st = Position.new(node.loc.line, node.loc.column)
  en = Position.new(node.loc.last_line, node.loc.last_column)
  Range.new(st, en)
end

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

Parameters:

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

Returns:

  • (Parser::AST::Node)


24
25
26
27
28
29
30
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 24

def parse code, filename = nil, line = 0
  buffer = ::Parser::Source::Buffer.new(filename, line)
  buffer.source = code
  parser.parse(buffer)
rescue ::Parser::SyntaxError => e
  raise Parser::SyntaxError, e.message
end

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

Parameters:

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

Returns:

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


10
11
12
13
14
15
16
17
18
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 10

def parse_with_comments code, filename = nil
  buffer = ::Parser::Source::Buffer.new(filename, 0)
  buffer.source = code
  node = parser.parse(buffer)
  comments = CommentRipper.new(code, filename, 0).parse
  [node, comments]
rescue ::Parser::SyntaxError => e
  raise Parser::SyntaxError, e.message
end

#parserParser::Base

Returns:

  • (Parser::Base)


33
34
35
36
37
38
39
40
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 33

def parser
  # @todo Consider setting an instance variable. We might not need to
  #   recreate the parser every time we use it.
  parser = ::Parser::CurrentRuby.new(FlawedBuilder.new)
  parser.diagnostics.all_errors_are_fatal = true
  parser.diagnostics.ignore_warnings      = true
  parser
end

#process_node(*args) ⇒ Object



91
92
93
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 91

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

#references(source, name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 50

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

#returns_from(node) ⇒ Object



46
47
48
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 46

def returns_from node
  NodeMethods.returns_from(node)
end

#string_ranges(node) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 113

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)
  end
  node.children.each do |child|
    result.concat string_ranges(child)
  end
  if node.type == :dstr && node.children.last.nil?
    # result.push Range.new(result.last.ending, result.last.ending)
    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



99
100
101
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 99

def version
  parser.version
end