Skip to content

Commit

Permalink
feat: add hover for global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
snutij committed Oct 8, 2024
1 parent f21413e commit 2763454
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/ruby_lsp/listeners/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Hover
Prism::ConstantReadNode,
Prism::ConstantWriteNode,
Prism::ConstantPathNode,
Prism::GlobalVariableReadNode,
Prism::InstanceVariableReadNode,
Prism::InstanceVariableAndWriteNode,
Prism::InstanceVariableOperatorWriteNode,
Expand Down Expand Up @@ -62,6 +63,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
:on_constant_write_node_enter,
:on_constant_path_node_enter,
:on_call_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 @@ -128,6 +130,18 @@ def on_call_node_enter(node)
handle_method_hover(message)
end

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

return unless entries

categorized_markdown_from_index_entries(global_variable, entries).each do |category, content|
@response_builder.push(content, category: category)
end
end

sig { params(node: Prism::InstanceVariableReadNode).void }
def on_instance_variable_read_node_enter(node)
handle_instance_variable_hover(node.name.to_s)
Expand Down
19 changes: 19 additions & 0 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ def test_hovering_on_erb
end
end

def test_hovering_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/hover",
params: { textDocument: { uri: uri }, position: { line: 0, character: 1 } },
)

assert_match("The debug flag", server.pop_response.response.contents.value)
end
end

def test_hovering_precision
source = <<~RUBY
module Foo
Expand Down

0 comments on commit 2763454

Please sign in to comment.