Class: Orbacle::FindCallUnderPosition

Inherits:
Parser::AST::Processor
  • Object
show all
Includes:
AstUtils
Defined in:
lib/orbacle/find_call_under_position.rb

Defined Under Namespace

Classes: IvarResult, SelfResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AstUtils

#build_position_range_from_ast, #build_position_range_from_parser_range, const_prename_and_name_to_string, const_to_string, get_nesting, prename

Constructor Details

#initialize(parser) ⇒ FindCallUnderPosition

Returns a new instance of FindCallUnderPosition.



12
13
14
# File 'lib/orbacle/find_call_under_position.rb', line 12

def initialize(parser)
  @parser = parser
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



27
28
29
# File 'lib/orbacle/find_call_under_position.rb', line 27

def parser
  @parser
end

Instance Method Details

#on_class(ast) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/orbacle/find_call_under_position.rb', line 50

def on_class(ast)
  klass_name_ast, _ = ast.children
  klass_name_ref = ConstRef.from_ast(klass_name_ast, @current_nesting)
  with_new_nesting(@current_nesting.increase_nesting_const(klass_name_ref)) do
    super
  end
  nil
end

#on_module(ast) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/orbacle/find_call_under_position.rb', line 59

def on_module(ast)
  module_name_ast, _ = ast.children
  module_name_ref = ConstRef.from_ast(module_name_ast, @current_nesting)
  with_new_nesting(@current_nesting.increase_nesting_const(module_name_ref)) do
    super
  end
  nil
end

#on_send(ast) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/orbacle/find_call_under_position.rb', line 29

def on_send(ast)
  if ast.loc.selector && build_position_range_from_parser_range(ast.loc.selector).include_position?(@searched_position)
    message_name = ast.children.fetch(1)
    selector_position_range = build_position_range_from_parser_range(ast.loc.selector)
    @result = if ast.children[0] == nil
      SelfResult.new(message_name, @current_nesting)
    else
      case ast.children[0].type
      when :self
        SelfResult.new(message_name, @current_nesting)
      when :ivar
        IvarResult.new(message_name, ast.children[0].children[0], @current_nesting)
      else
      end
    end
  else
    super
  end
  nil
end

#process_file(file_content, searched_position) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/orbacle/find_call_under_position.rb', line 16

def process_file(file_content, searched_position)
  ast = parser.parse(file_content)

  @current_nesting = Nesting.empty
  @searched_position = searched_position

  process(ast)

  @result
end

#with_new_nesting(new_nesting) ⇒ Object



68
69
70
71
72
73
# File 'lib/orbacle/find_call_under_position.rb', line 68

def with_new_nesting(new_nesting)
  previous_nesting = @current_nesting
  @current_nesting = new_nesting
  yield
  @current_nesting = previous_nesting
end