Skip to content

Commit

Permalink
Add go-to-definition for global variables (#2673)
Browse files Browse the repository at this point in the history
feat: add go-to-definition for global variables
  • Loading branch information
snutij authored Oct 8, 2024
1 parent 2dbb722 commit f21413e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/ruby_lsp/listeners/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def initialize(response_builder, global_state, language_id, uri, node_context, d
:on_block_argument_node_enter,
:on_constant_read_node_enter,
:on_constant_path_node_enter,
:on_global_variable_read_node_enter,
:on_instance_variable_read_node_enter,
:on_instance_variable_write_node_enter,
:on_instance_variable_and_write_node_enter,
Expand Down Expand Up @@ -120,6 +121,25 @@ def on_constant_read_node_enter(node)
find_in_index(name)
end

sig { params(node: Prism::GlobalVariableReadNode).void }
def on_global_variable_read_node_enter(node)
entries = @index[node.name.to_s]

return unless entries

entries.each do |entry|
location = entry.location

@response_builder << Interface::Location.new(
uri: URI::Generic.from_path(path: entry.file_path).to_s,
range: Interface::Range.new(
start: Interface::Position.new(line: location.start_line - 1, character: location.start_column),
end: Interface::Position.new(line: location.end_line - 1, character: location.end_column),
),
)
end
end

sig { params(node: Prism::InstanceVariableReadNode).void }
def on_instance_variable_read_node_enter(node)
handle_instance_variable_definition(node.name.to_s)
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_lsp/requests/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def initialize(document, global_state, position, dispatcher, sorbet_level)
Prism::ConstantReadNode,
Prism::ConstantPathNode,
Prism::BlockArgumentNode,
Prism::GlobalVariableReadNode,
Prism::InstanceVariableReadNode,
Prism::InstanceVariableAndWriteNode,
Prism::InstanceVariableOperatorWriteNode,
Expand Down
21 changes: 21 additions & 0 deletions test/requests/definition_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,27 @@ def baz
end
end

def test_definition_for_global_variables
source = <<~RUBY
$DEBUG
RUBY

with_server(source) do |server, uri|
index = server.instance_variable_get(:@global_state).index
RubyIndexer::RBSIndexer.new(index).index_ruby_core

server.process_message(
id: 1,
method: "textDocument/definition",
params: { textDocument: { uri: uri }, position: { character: 1, line: 0 } },
)
response = server.pop_response.response.first
assert_match(%r{/gems/rbs-.*/core/global_variables.rbs}, response.uri)
assert_equal(response.range.start.line, response.range.end.line)
assert_operator(response.range.start.character, :<, response.range.end.character)
end
end

def test_definition_for_instance_variables
source = <<~RUBY
class Foo
Expand Down

0 comments on commit f21413e

Please sign in to comment.