Class: Orbacle::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/orbacle/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Engine



5
6
7
# File 'lib/orbacle/engine.rb', line 5

def initialize(logger)
  @logger = logger
end

Instance Attribute Details

#stats_recorderObject (readonly)

Returns the value of attribute stats_recorder.



9
10
11
# File 'lib/orbacle/engine.rb', line 9

def stats_recorder
  @stats_recorder
end

Instance Method Details

#completions_for_call_under_position(file_content, position) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/orbacle/engine.rb', line 46

def completions_for_call_under_position(file_content, position)
  result = FindCallUnderPosition.new(RubyParser.new).process_file(file_content, position)
  case result
  when FindCallUnderPosition::SelfResult
    filtered_methods_from_class_name(result.nesting.to_scope.to_const_name.to_string, result.message_name)
  when FindCallUnderPosition::IvarResult
    ivar_node = @graph.get_ivar_definition_node(result.nesting.to_scope, result.ivar_name)
    ivar_type = @state.type_of(ivar_node)
    methods = []
    ivar_type.each_possible_type do |type|
      methods.concat(filtered_methods_from_class_name(type.name, result.message_name))
    end
    methods
  else
    []
  end
end

#get_type_information(filepath, searched_position) ⇒ Object



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

def get_type_information(filepath, searched_position)
  relevant_nodes = @graph
    .vertices
    .select {|n| n.location && n.location.uri.eql?(filepath) && n.location.position_range.include_position?(searched_position) }
    .sort_by {|n| n.location.span }

  pretty_print_type(@state.type_of(relevant_nodes.at(0)))
end

#index(project_root) ⇒ Object



11
12
13
14
15
# File 'lib/orbacle/engine.rb', line 11

def index(project_root)
  @stats_recorder = Indexer::StatsRecorder.new
  service = Indexer.new(logger, stats_recorder)
  @state, @graph, @worklist = service.(project_root: project_root)
end

#locations_for_definition_under_position(file_path, file_content, position) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/orbacle/engine.rb', line 26

def locations_for_definition_under_position(file_path, file_content, position)
  result = find_definition_under_position(file_content, position.line, position.character)
  case result
  when FindDefinitionUnderPosition::ConstantResult
    constants = @state.solve_reference2(result.const_ref)
    definitions_locations(constants)
  when FindDefinitionUnderPosition::MessageResult
    caller_type = get_type_of_caller_from_message_send(file_path, result.position_range)
    methods_definitions = get_methods_definitions_for_type(caller_type, result.name)
    methods_definitions = @state.get_methods(result.name) if methods_definitions.empty?
    definitions_locations(methods_definitions)
  when FindDefinitionUnderPosition::SuperResult
    method_surrounding_super = @state.find_method_including_position(file_path, result.keyword_position_range.start)
    return [] if method_surrounding_super.nil?
    super_method = @state.find_super_method(method_surrounding_super.id)
    return definitions_locations(@state.get_methods(method_surrounding_super.name) - [method_surrounding_super]) if super_method.nil?
    definitions_locations([super_method])
  end
end