Class: Solargraph::ApiMap::Source

Inherits:
Object
  • Object
show all
Includes:
NodeMethods
Defined in:
lib/solargraph/api_map/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeMethods

#const_from, #infer_literal_node_type, #pack_name, #resolve_node_signature, #unpack_name

Constructor Details

#initialize(code, node, comments, filename) ⇒ Source

Returns a new instance of Source.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solargraph/api_map/source.rb', line 20

def initialize code, node, comments, filename
  @code = code
  root = AST::Node.new(:source, [filename])
  root = root.append node
  @node = root
  @comments = comments
  @docstring_hash = associate_comments(node, comments)
  @filename = filename
  @namespace_nodes = {}
  @all_nodes = []
  @node_stack = []
  @node_tree = {}
  inner_map_node @node
end

Instance Attribute Details

#code ⇒ String (readonly)

Returns:

  • (String)


7
8
9
# File 'lib/solargraph/api_map/source.rb', line 7

def code
  @code
end

#comments ⇒ Array (readonly)

Returns:

  • (Array)


13
14
15
# File 'lib/solargraph/api_map/source.rb', line 13

def comments
  @comments
end

#filename ⇒ String (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/solargraph/api_map/source.rb', line 16

def filename
  @filename
end

#node ⇒ Parser::AST::Node (readonly)

Returns:

  • (Parser::AST::Node)


10
11
12
# File 'lib/solargraph/api_map/source.rb', line 10

def node
  @node
end

Class Method Details

.beginning_of_line_from(str, i) ⇒ Object



327
328
329
330
331
332
333
334
335
# File 'lib/solargraph/api_map/source.rb', line 327

def beginning_of_line_from str, i
  while i > 0 and str[i] != "\n"
    i -= 1
  end
  if i > 0 and str[i..-1].strip == ''
    i = beginning_of_line_from str, i -1
  end
  i
end

.fix(filename, code, cursor = nil) ⇒ Solargraph::ApiMap::Source



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/solargraph/api_map/source.rb', line 274

def fix filename, code, cursor = nil
  tries = 0
  code.gsub!(/\r/, '')
  tmp = code
  cursor = CodeMap.get_offset(code, cursor[0], cursor[1]) if cursor.kind_of?(Array)
  fixed_cursor = false
  begin
    # HACK: The current file is parsed with a trailing underscore to fix
    # incomplete trees resulting from short scripts (e.g., a lone variable
    # assignment).
    node, comments = Parser::CurrentRuby.parse_with_comments(tmp + "\n_")
    Source.new(code, node, comments, filename)
  rescue Parser::SyntaxError => e
    if tries < 10
      tries += 1
      if tries == 10 and e.message.include?('token $end')
        tmp += "\nend"
      else
        if !fixed_cursor and !cursor.nil? and cursor >= 2 and (e.message.include?('token $end') or tmp[cursor - 1] == '$')
          fixed_cursor = true
          spot = cursor - 2
          if tmp[cursor - 1] == '.' or tmp[cursor - 1] == '$'
            repl = ';'
          else
            repl = '#'
          end
        else
          spot = e.diagnostic.location.begin_pos
          repl = '_'
          if tmp[spot] == '@' or tmp[spot] == ':'
            # Stub unfinished instance variables and symbols
            spot -= 1
          elsif tmp[spot - 1] == '.'
            # Stub unfinished method calls
            repl = '#' if spot == tmp.length or tmp[spot] == '\n'
            spot -= 2
          else
            # Stub the whole line
            spot = beginning_of_line_from(tmp, spot)
            repl = '#'
            if tmp[spot+1..-1].rstrip == 'end'
              repl= 'end;end'
            end
          end
        end
        tmp = tmp[0..spot] + repl + tmp[spot+repl.length+1..-1].to_s
      end
      retry
    end
    raise e
  end
end

.load(filename) ⇒ Solargraph::ApiMap::Source



262
263
264
265
# File 'lib/solargraph/api_map/source.rb', line 262

def load filename
  code = File.read(filename).gsub(/\r/, '')
  Source.virtual(filename, code)
end

.virtual(filename, code) ⇒ Solargraph::ApiMap::Source



268
269
270
271
# File 'lib/solargraph/api_map/source.rb', line 268

def virtual filename, code
  node, comments = Parser::CurrentRuby.parse_with_comments(code)
  Source.new(code, node, comments, filename)
end

Instance Method Details

#attribute_pins ⇒ Object



55
56
57
# File 'lib/solargraph/api_map/source.rb', line 55

def attribute_pins
  @attribute_pins ||= []
end

#class_variable_pins ⇒ Object



63
64
65
# File 'lib/solargraph/api_map/source.rb', line 63

def class_variable_pins
  @class_variable_pins ||= []
end

#code_for(node) ⇒ Object



91
92
93
94
95
96
# File 'lib/solargraph/api_map/source.rb', line 91

def code_for node
  b = node.location.expression.begin.begin_pos
  e = node.location.expression.end.end_pos
  frag = code[b..e-1].to_s
  frag.strip.gsub(/,$/, '')
end

#constant_pins ⇒ Object



75
76
77
# File 'lib/solargraph/api_map/source.rb', line 75

def constant_pins
  @constant_pins ||= []
end

#docstring_for(node) ⇒ Object



87
88
89
# File 'lib/solargraph/api_map/source.rb', line 87

def docstring_for node
  @docstring_hash[node.loc]
end

#global_variable_pins ⇒ Object



71
72
73
# File 'lib/solargraph/api_map/source.rb', line 71

def global_variable_pins
  @global_variable_pins ||= []
end

#include?(node) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/solargraph/api_map/source.rb', line 102

def include? node
  @all_nodes.include? node
end

#instance_variable_pins ⇒ Object



59
60
61
# File 'lib/solargraph/api_map/source.rb', line 59

def instance_variable_pins
  @instance_variable_pins ||= []
end

#local_variable_pins ⇒ Object



67
68
69
# File 'lib/solargraph/api_map/source.rb', line 67

def local_variable_pins
  @local_variable_pins ||= []
end

#method_pins ⇒ Object



51
52
53
# File 'lib/solargraph/api_map/source.rb', line 51

def method_pins
  @method_pins ||= []
end

#namespace_includes ⇒ Object



43
44
45
# File 'lib/solargraph/api_map/source.rb', line 43

def namespace_includes
  @namespace_includes ||= {}
end

#namespace_nodes ⇒ Object



39
40
41
# File 'lib/solargraph/api_map/source.rb', line 39

def namespace_nodes
  @namespace_nodes
end

#namespaces ⇒ Object



35
36
37
# File 'lib/solargraph/api_map/source.rb', line 35

def namespaces
  @namespace_nodes.keys
end

#required ⇒ Object



83
84
85
# File 'lib/solargraph/api_map/source.rb', line 83

def required
  @required ||= []
end

#superclasses ⇒ Object



47
48
49
# File 'lib/solargraph/api_map/source.rb', line 47

def superclasses
  @superclasses ||= {}
end

#symbol_pins ⇒ Object



79
80
81
# File 'lib/solargraph/api_map/source.rb', line 79

def symbol_pins
  @symbol_pins ||= []
end

#tree_for(node) ⇒ Object



98
99
100
# File 'lib/solargraph/api_map/source.rb', line 98

def tree_for node
  @node_tree[node] || []
end