Class: RubyLsp::Requests::Definition

Inherits:
ExtensibleListener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/definition.rb

Overview

![Definition demo](../../definition.gif)

The [definition request](microsoft.github.io/language-server-protocol/specification#textDocument_definition) jumps to the definition of the symbol under the cursor.

Currently supported targets:

  • Classes

  • Modules

  • Constants

  • Require paths

  • Methods invoked on self only

# Example

“‘ruby require “some_gem/file” # <- Request go to definition on this string will take you to the file Product.new # <- Request go to definition on this class name will take you to its declaration. “`

Constant Summary collapse

ResponseType =
type_member { { fixed: T.nilable(T.any(T::Array[Interface::Location], Interface::Location)) } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#response

Methods included from Support::Common

#create_code_lens, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(uri, nesting, index, dispatcher, message_queue) ⇒ Definition

Returns a new instance of Definition.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_lsp/requests/definition.rb', line 43

def initialize(uri, nesting, index, dispatcher, message_queue)
  @uri = uri
  @nesting = nesting
  @index = index
  @_response = T.let(nil, ResponseType)

  super(dispatcher, message_queue)

  dispatcher.register(
    self,
    :on_call_node_enter,
    :on_constant_read_node_enter,
    :on_constant_path_node_enter,
  )
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



32
33
34
# File 'lib/ruby_lsp/requests/definition.rb', line 32

def _response
  @_response
end

Instance Method Details

#initialize_external_listener(addon) ⇒ Object



60
61
62
# File 'lib/ruby_lsp/requests/definition.rb', line 60

def initialize_external_listener(addon)
  addon.create_definition_listener(@uri, @nesting, @index, @dispatcher, @message_queue)
end

#merge_response!(other) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_lsp/requests/definition.rb', line 65

def merge_response!(other)
  other_response = other._response

  case @_response
  when Interface::Location
    @_response = [@_response, *other_response]
  when Array
    @_response.concat(Array(other_response))
  when nil
    @_response = other_response
  end

  self
end

#on_call_node_enter(node) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/ruby_lsp/requests/definition.rb', line 81

def on_call_node_enter(node)
  message = node.name

  if message == :require || message == :require_relative
    handle_require_definition(node)
  else
    handle_method_definition(node)
  end
end

#on_constant_path_node_enter(node) ⇒ Object



92
93
94
# File 'lib/ruby_lsp/requests/definition.rb', line 92

def on_constant_path_node_enter(node)
  find_in_index(node.slice)
end

#on_constant_read_node_enter(node) ⇒ Object



97
98
99
# File 'lib/ruby_lsp/requests/definition.rb', line 97

def on_constant_read_node_enter(node)
  find_in_index(node.slice)
end