Class: RubyLsp::Requests::Definition
- Inherits:
-
ExtensibleListener
- Object
- Listener
- ExtensibleListener
- RubyLsp::Requests::Definition
- 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, only jumping to classes, modules and required files is supported.
# 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
-
#_response ⇒ Object
readonly
Returns the value of attribute _response.
Instance Method Summary collapse
-
#initialize(uri, nesting, index, emitter, message_queue) ⇒ Definition
constructor
A new instance of Definition.
- #initialize_external_listener(addon) ⇒ Object
- #merge_response!(other) ⇒ Object
- #on_call(node) ⇒ Object
- #on_constant_path(node) ⇒ Object
- #on_constant_read(node) ⇒ Object
Methods inherited from Listener
Methods included from Support::Common
#create_code_lens, #markdown_from_index_entries, #range_from_location, #range_from_node, #visible?
Constructor Details
#initialize(uri, nesting, index, emitter, message_queue) ⇒ Definition
Returns a new instance of Definition.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruby_lsp/requests/definition.rb', line 38 def initialize(uri, nesting, index, emitter, ) @uri = uri @nesting = nesting @index = index @_response = T.let(nil, ResponseType) super(emitter, ) emitter.register(self, :on_call, :on_constant_read, :on_constant_path) end |
Instance Attribute Details
#_response ⇒ Object (readonly)
Returns the value of attribute _response.
27 28 29 |
# File 'lib/ruby_lsp/requests/definition.rb', line 27 def _response @_response end |
Instance Method Details
#initialize_external_listener(addon) ⇒ Object
49 50 51 |
# File 'lib/ruby_lsp/requests/definition.rb', line 49 def initialize_external_listener(addon) addon.create_definition_listener(@uri, @nesting, @index, @emitter, @message_queue) end |
#merge_response!(other) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_lsp/requests/definition.rb', line 54 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) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ruby_lsp/requests/definition.rb', line 70 def on_call(node) = node.name return unless == "require" || == "require_relative" arguments = node.arguments return unless arguments argument = arguments.arguments.first return unless argument.is_a?(YARP::StringNode) case when "require" entry = @index.search_require_paths(argument.content).find do |indexable_path| indexable_path.require_path == argument.content end if entry candidate = entry.full_path @_response = Interface::Location.new( uri: URI::Generic.from_path(path: candidate).to_s, range: Interface::Range.new( start: Interface::Position.new(line: 0, character: 0), end: Interface::Position.new(line: 0, character: 0), ), ) end when "require_relative" required_file = "#{argument.content}.rb" path = @uri.to_standardized_path current_folder = path ? Pathname.new(CGI.unescape(path)).dirname : Dir.pwd candidate = File.(File.join(current_folder, required_file)) @_response = Interface::Location.new( uri: URI::Generic.from_path(path: candidate).to_s, range: Interface::Range.new( start: Interface::Position.new(line: 0, character: 0), end: Interface::Position.new(line: 0, character: 0), ), ) end end |
#on_constant_path(node) ⇒ Object
114 115 116 |
# File 'lib/ruby_lsp/requests/definition.rb', line 114 def on_constant_path(node) find_in_index(node.slice) end |
#on_constant_read(node) ⇒ Object
119 120 121 |
# File 'lib/ruby_lsp/requests/definition.rb', line 119 def on_constant_read(node) find_in_index(node.slice) end |