Class: RubyLsp::Rails::FactoryBot::Completion

Inherits:
Object
  • Object
show all
Includes:
RubyLsp::Requests::Support::Common
Defined in:
lib/ruby_lsp/rails/factory_bot/completion.rb

Constant Summary collapse

FACTORY_BOT_METHODS =

TODO: Avoid duplication with other class

%i[
  create
  build
  build_stubbed
  attributes_for
].flat_map { |attr| [attr, :"#{attr}_list", :"#{attr}_pair"] }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, node_context, dispatcher, server_client) ⇒ Completion

Returns a new instance of Completion.



19
20
21
22
23
24
25
# File 'lib/ruby_lsp/rails/factory_bot/completion.rb', line 19

def initialize(response_builder, node_context, dispatcher, server_client)
  @response_builder = response_builder
  @node_context = node_context
  @server_client = server_client

  dispatcher.register self, :on_call_node_enter
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_lsp/rails/factory_bot/completion.rb', line 27

def on_call_node_enter(node)
  return unless FACTORY_BOT_METHODS.include?(node.name) ||
                FACTORY_BOT_METHODS.include?(@node_context.parent.name)

  case node.arguments
  in [Prism::SymbolNode => factory_name_node]
    handle_factory(factory_name_node, factory_name_node.value.to_s)

  in [Prism::SymbolNode => factory_name_node, *, Prism::SymbolNode => trait_node]
    handle_trait(factory_name_node.value.to_s, node, trait_node.value.to_s)

  in [Prism::SymbolNode => factory_name_node, *, Prism::CallNode => call_node]
    handle_attribute(factory_name_node.value.to_s, node, call_node.message)

  in [Prism::SymbolNode => factory_name_node, *, Prism::KeywordHashNode => kw_node]
    handle_attribute(
      factory_name_node.value.to_s, node, kw_node.elements.last.key.value&.to_s
    )
  in [Prism::SymbolNode => factory_name_node, *, Prism::HashNode => hash_node]
    handle_attribute(
      factory_name_node.value.to_s, node, hash_node.elements.last.key.value&.to_s
    )
  else
    $stderr.write node.arguments
    nil
  end
rescue => e
  $stderr.write(e, e.backtrace)
end