Class: RubyLsp::Rake::Definition

Inherits:
Object
  • Object
show all
Includes:
Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby_lsp_rake/definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, node_context, index, dispatcher) ⇒ Definition

: (RubyLsp::ResponseBuilders::CollectionResponseBuilder response_builder, NodeContext node_context, RubyIndexer::Index index, Prism::Dispatcher dispatcher) -> void



10
11
12
13
14
15
16
17
# File 'lib/ruby_lsp/ruby_lsp_rake/definition.rb', line 10

def initialize(response_builder, node_context, index, dispatcher)
  @response_builder = response_builder
  @node_context = node_context
  @nesting = node_context.nesting
  @index = index

  dispatcher.register(self, :on_symbol_node_enter, :on_string_node_enter)
end

Instance Method Details

#handle_prerequisite(node) ⇒ Object

: ((Prism::SymbolNode | Prism::StringNode) node) -> void



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_lsp/ruby_lsp_rake/definition.rb', line 30

def handle_prerequisite(node) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  call_node_name = @node_context.call_node&.name
  return unless call_node_name == :task

  name = case node
         when Prism::StringNode
           node.content
         when Prism::SymbolNode
           node.value
         end

  arg = @node_context.call_node&.arguments&.arguments&.first
  case arg
  when Prism::KeywordHashNode
    kh = arg.child_nodes.first
    case kh
    when Prism::AssocNode
      v = kh.value
      case v
      when Prism::StringNode
        return unless name == v.content
      when Prism::SymbolNode
        return unless name == v.value
      when Prism::ArrayNode
        return unless v.elements.find do |n|
          name == case n # rubocop:disable Metrics/BlockNesting
                  when Prism::StringNode
                    n.content
                  when Prism::SymbolNode
                    n.value
                  end
        end
      end
    end
  else
    return
  end

  task_name = "task:#{name}"
  return unless @index.indexed? task_name

  entries = @index[task_name]

  # refer to: https://github.com/Shopify/ruby-lsp-rails/blob/b7791290fb59b06dc99e28a991ee0607e3931a1e/lib/ruby_lsp/ruby_lsp_rails/definition.rb#L141-L152
  entries.each do |entry|
    loc = entry.location
    uri = URI::Generic.from_path(
      path: entry.file_path,
      fragment: "L#{loc.start_line},#{loc.start_column + 1}-#{loc.end_line},#{loc.end_column + 1}"
    )
    @response_builder << Interface::Location.new(
      uri: uri,
      range: range_from_location(entry.location)
    )
  end
end

#on_string_node_enter(node) ⇒ Object

: (Prism::StringNode node) -> void



25
26
27
# File 'lib/ruby_lsp/ruby_lsp_rake/definition.rb', line 25

def on_string_node_enter(node)
  handle_prerequisite(node)
end

#on_symbol_node_enter(node) ⇒ Object

: (Prism::SymbolNode node) -> void



20
21
22
# File 'lib/ruby_lsp/ruby_lsp_rake/definition.rb', line 20

def on_symbol_node_enter(node)
  handle_prerequisite(node)
end