Class: RubyLsp::Ree::MissingImportsFormatter

Inherits:
BaseFormatter show all
Includes:
ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/formatters/missing_imports_formatter.rb

Constant Summary collapse

IGNORE_METHOD_CALL_NAMES =
['build_dto', 'schema']

Constants included from ReeLspUtils

ReeLspUtils::Entry

Instance Method Summary collapse

Methods included from ReeLspUtils

#camelize, #find_local_file_path, #get_range_for_fn_insert, #get_ree_type, #get_uri_path, #package_name_from_spec_uri, #package_name_from_uri, #package_path_from_uri, #parameter_name, #path_from_package_folder, #signature_params_from_node, #spec_relative_file_path_from_uri, #underscore

Methods inherited from BaseFormatter

call, #initialize

Constructor Details

This class inherits a constructor from RubyLsp::Ree::BaseFormatter

Instance Method Details

#call(source, uri) ⇒ Object



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
# File 'lib/ruby_lsp/ruby_lsp_ree/formatters/missing_imports_formatter.rb', line 11

def call(source, uri)
  return source unless @index

  parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_source(source)
  return source if !parsed_doc || !parsed_doc.has_root_class?

  finder = ReeObjectFinder.new(@index)
  editor = RubyLsp::Ree::ReeSourceEditor.new(source)

  current_package = package_name_from_uri(uri)

  method_calls = parsed_doc.parse_method_calls
  filtered_method_calls = filter_method_calls(parsed_doc, method_calls)
  objects_to_add = filtered_method_calls.map{ |fn_call|
    ree_objects = finder.find_objects(fn_call.name.to_s)
    choose_object_to_add(ree_objects, current_package)
  }.compact

  objects_to_add.uniq!{ |obj| obj.name }
  objects_to_add.reject!{ |obj| IGNORE_METHOD_CALL_NAMES.include?(obj.name) }
  objects_to_add.reject!{ |obj| parsed_doc.includes_linked_object?(obj.name) }
  return editor.source if objects_to_add.size == 0
  
  editor.add_links(parsed_doc, objects_to_add, current_package)
  editor.source
end