Class: RubyLsp::Rails::FactoryBot::Hover

Inherits:
Object
  • Object
show all
Includes:
RubyLsp::Requests::Support::Common
Defined in:
lib/ruby_lsp/rails/factory_bot/hover.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, ruby_index) ⇒ Hover

Returns a new instance of Hover.



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

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

  dispatcher.register self, :on_symbol_node_enter
end

Instance Method Details

#on_symbol_node_enter(symbol_node) ⇒ Object



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
56
57
58
# File 'lib/ruby_lsp/rails/factory_bot/hover.rb', line 28

def on_symbol_node_enter(symbol_node)
  parent = @node_context.parent
  call_node = @node_context.call_node

  return unless call_node

  # "parent" isn't strictly speaking the immediate parent as in the AST - the
  # element it refers to is a bit opinionated... in this case, it is always the call node,
  # whether the symbol is an argument in the call_node args, or a symbol in a kw hash :/
  unless parent.is_a?(Prism::CallNode) || FACTORY_BOT_METHODS.include?(call_node.message.to_sym)
    return
  end

  case call_node.arguments.arguments
  in [^symbol_node, *]
    handle_factory(symbol_node)
  in [Prism::SymbolNode => factory_node, *, ^symbol_node]
    handle_trait(symbol_node, factory_node)
  in [Prism::SymbolNode => factory_node, ^symbol_node, *]
    handle_trait(symbol_node, factory_node)
  in [Prism::SymbolNode => factory_node, Integer, ^symbol_node, *]
    handle_trait(symbol_node, factory_node)
  in [Prism::SymbolNode => factory_node, *, Prism::KeywordHashNode => kw_hash] if kw_hash.elements.any? { |e| e.key == symbol_node }
    handle_attribute(symbol_node, factory_node)
  in [Prism::SymbolNode => factory_node, *, Prism::HashNode => kw_hash] if kw_hash.elements.any? { |e| e.key == symbol_node }
    handle_attribute(symbol_node, factory_node)
  else
    $stderr.write "nope fact_or_trait", call_node.arguments.arguments
    nil
  end
end