Class: RubyLsp::Requests::Definition
- Extended by:
- T::Generic, T::Sig
- Defined in:
- lib/ruby_lsp/requests/definition.rb
Overview

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 required files is supported.
# Example
“‘ruby require “some_gem/file” # <- Request go to definition on this string will take you to the file “`
Constant Summary collapse
- ResponseType =
type_member { { fixed: T.nilable(Interface::Location) } }
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#initialize(uri, emitter, message_queue) ⇒ Definition
constructor
A new instance of Definition.
- #on_command(node) ⇒ Object
Methods inherited from Listener
#merge_external_listeners_responses!, #merge_response!
Methods included from Support::Common
#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?
Constructor Details
#initialize(uri, emitter, message_queue) ⇒ Definition
Returns a new instance of Definition.
29 30 31 32 33 34 35 |
# File 'lib/ruby_lsp/requests/definition.rb', line 29 def initialize(uri, emitter, ) super(emitter, ) @uri = uri @response = T.let(nil, ResponseType) emitter.register(self, :on_command) end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
26 27 28 |
# File 'lib/ruby_lsp/requests/definition.rb', line 26 def response @response end |
Instance Method Details
#on_command(node) ⇒ Object
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 |
# File 'lib/ruby_lsp/requests/definition.rb', line 38 def on_command(node) = node..value return unless == "require" || == "require_relative" argument = node.arguments.parts.first return unless argument.is_a?(SyntaxTree::StringLiteral) string = argument.parts.first return unless string.is_a?(SyntaxTree::TStringContent) required_file = "#{string.value}.rb" case when "require" candidate = find_file_in_load_path(required_file) if candidate @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" path = @uri.to_standardized_path current_folder = path ? Pathname.new(CGI.unescape(path)).dirname : Dir.pwd candidate = File.(File.join(current_folder, required_file)) if candidate @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 end |