Class: RubyLsp::Rake::IndexingEnhancement

Inherits:
RubyIndexer::Enhancement
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_rake/indexing_enhancement.rb

Instance Method Summary collapse

Constructor Details

#initialize(listener) ⇒ IndexingEnhancement

: (RubyIndexer::DeclarationListener listener) -> void



8
9
10
11
12
# File 'lib/ruby_lsp/ruby_lsp_rake/indexing_enhancement.rb', line 8

def initialize(listener)
  super(listener)
  @namespace_stack = []
  @last_desc = nil
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode node) -> void



16
17
18
19
20
21
22
23
24
25
26
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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_lsp/ruby_lsp_rake/indexing_enhancement.rb', line 16

def on_call_node_enter(node) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  @last_desc = nil unless node.name == :task

  return unless @listener.current_owner.nil?
  return unless i[task desc namespace].include? node.name

  arguments = node.arguments&.arguments
  return unless arguments

  arg = arguments.first
  name = case arg
         when Prism::StringNode
           arg.content
         when Prism::SymbolNode
           arg.value
         when Prism::KeywordHashNode
           kh = arg.child_nodes.first
           case kh
           when Prism::AssocNode
             k = kh.key
             case k
             when Prism::StringNode
               k.content
             when Prism::SymbolNode
               k.value
             end
           end
         end

  return if name.nil?

  if node.name == :namespace
    @namespace_stack << name
    return
  end

  if node.name == :desc
    @last_desc = name
    return
  end

  ary = [*@namespace_stack, name]
  (1..(ary.size)).each do |i|
    @listener.add_method(
      "task:#{ary[-i..]&.join(":")}",
      node.location,
      [],
      comments: @last_desc
    )
  end

  @last_desc = nil
end

#on_call_node_leave(node) ⇒ Object

: (Prism::CallNode node) -> void



72
73
74
75
76
77
# File 'lib/ruby_lsp/ruby_lsp_rake/indexing_enhancement.rb', line 72

def on_call_node_leave(node)
  return unless @listener.current_owner.nil?
  return unless node.name == :namespace

  @namespace_stack.pop
end