Class: Gelauto::MethodIndex
- Inherits:
-
Object
- Object
- Gelauto::MethodIndex
- Defined in:
- lib/gelauto/method_index.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#sig_index ⇒ Object
readonly
Returns the value of attribute sig_index.
Instance Method Summary collapse
- #annotate(path, code) ⇒ Object
- #each ⇒ Object
- #each_method_in(path) ⇒ Object
- #find(path, lineno) ⇒ Object
- #index_methods_in(path, ast, nesting = []) ⇒ Object
-
#initialize ⇒ MethodIndex
constructor
A new instance of MethodIndex.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ MethodIndex
Returns a new instance of MethodIndex.
5 6 7 8 |
# File 'lib/gelauto/method_index.rb', line 5 def initialize @index = Hash.new { |h, k| h[k] = {} } @sig_index = Hash.new { |h, k| h[k] = {} } end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
3 4 5 |
# File 'lib/gelauto/method_index.rb', line 3 def index @index end |
#sig_index ⇒ Object (readonly)
Returns the value of attribute sig_index.
3 4 5 |
# File 'lib/gelauto/method_index.rb', line 3 def sig_index @sig_index end |
Instance Method Details
#annotate(path, code) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gelauto/method_index.rb', line 77 def annotate(path, code) lines = code.split(/\r?\n/) mds = index[path] sigs = sig_index[path] [].tap do |annotated| lines.each_with_index do |line, idx| lineno = idx + 1 md = mds[lineno] if md.is_a?(MethodDef) && !sigs[idx] indent = line[0...line.index(/[^\s]/)] annotated << "#{indent}#{md.to_sig}" end annotated << line end end.join("\n") end |
#each ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/gelauto/method_index.rb', line 59 def each return to_enum(__method__) unless block_given? index.each_pair do |path, line_index| line_index.each_pair do |lineno, md| yield(path, lineno, md) if md.is_a?(MethodDef) end end end |
#each_method_in(path) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/gelauto/method_index.rb', line 69 def each_method_in(path) return to_enum(__method__, path) unless block_given? index[path].each_pair do |lineno, md| yield lineno, md if md.is_a?(MethodDef) end end |
#find(path, lineno) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/gelauto/method_index.rb', line 50 def find(path, lineno) if md = index[path][lineno] return md if md.is_a?(MethodDef) # md is actually an index pointing to another line index[path][md] end end |
#index_methods_in(path, ast, nesting = []) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gelauto/method_index.rb', line 10 def index_methods_in(path, ast, nesting = []) return unless ast case ast.type when :def, :defs name = nil args = nil if ast.type == :def name = ast.children[0] args = ast.children[1].children.each_with_object([]) do |c, memo| # ignore anonymous args like **, etc memo << c.children.first if c.children.first end else name = ast.children[1] args = ast.children[2].children.each_with_object([]) do |c, memo| # ignore anonymous args like **, etc memo << c.children.first if c.children.first end end md = MethodDef.new(name, args, nesting) index[path][ast.location.name.line] = md # point to start of method index[path][ast.location.end.line] = ast.location.name.line when :class, :module const_name = ast.children.first.children.last return visit_children(path, ast, nesting + [Namespace.new(const_name, ast.type)]) when :block if ast.children.first.children.last == :sig sig_index[path][ast.location.line] = true end end visit_children(path, ast, nesting) end |
#reset ⇒ Object
97 98 99 |
# File 'lib/gelauto/method_index.rb', line 97 def reset index.clear end |