Class: RubyLsp::Requests::Definition

Inherits:
Request
  • 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 Method Summary collapse

Constructor Details

#initialize(document, index, position, dispatcher, typechecker_enabled) ⇒ Definition

Returns a new instance of Definition.



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

def initialize(document, index, position, dispatcher, typechecker_enabled)
  super()
  target, parent, nesting = document.locate_node(
    position,
    node_types: [Prism::CallNode, Prism::ConstantReadNode, Prism::ConstantPathNode],
  )

  target = parent if target.is_a?(Prism::ConstantReadNode) && parent.is_a?(Prism::ConstantPathNode)

  @listeners = T.let(
    [Listeners::Definition.new(document.uri, nesting, index, dispatcher, typechecker_enabled)],
    T::Array[Listener[T.nilable(T.any(T::Array[Interface::Location], Interface::Location))]],
  )
  Addon.addons.each do |addon|
    addon_listener = addon.create_definition_listener(document.uri, nesting, index, dispatcher)
    @listeners << addon_listener if addon_listener
  end

  @target = T.let(target, T.nilable(Prism::Node))
  @dispatcher = dispatcher
end

Instance Method Details

#performObject



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

def perform
  @dispatcher.dispatch_once(@target)
  result = []

  @listeners.each do |listener|
    res = listener.response
    case res
    when Interface::Location
      result << res
    when Array
      result.concat(res)
    end
  end

  result if result.any?
end