Class: RubyLsp::Requests::Definition

Inherits:
Listener
  • 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, 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

Instance Method Summary collapse

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, nesting, index, emitter, message_queue) ⇒ Definition

Returns a new instance of Definition.



38
39
40
41
42
43
44
45
46
# File 'lib/ruby_lsp/requests/definition.rb', line 38

def initialize(uri, nesting, index, emitter, message_queue)
  super(emitter, message_queue)

  @uri = uri
  @nesting = nesting
  @index = index
  @response = T.let(nil, ResponseType)
  emitter.register(self, :on_command, :on_const, :on_const_path_ref)
end

Instance Attribute Details

#responseObject (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

#on_command(node) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_lsp/requests/definition.rb', line 60

def on_command(node)
  message = node.message.value
  return unless message == "require" || message == "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 message
  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.expand_path(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

#on_const(node) ⇒ Object



55
56
57
# File 'lib/ruby_lsp/requests/definition.rb', line 55

def on_const(node)
  find_in_index(node.value)
end

#on_const_path_ref(node) ⇒ Object



49
50
51
52
# File 'lib/ruby_lsp/requests/definition.rb', line 49

def on_const_path_ref(node)
  name = full_constant_name(node)
  find_in_index(name)
end