Skip to content

Improve support for hovering over a gem in Gemfile #3344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/ruby_lsp/listeners/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so

#: (Prism::StringNode node) -> void
def on_string_node_enter(node)
if @path && File.basename(@path) == GEMFILE_NAME
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same check as in on_call_node_enter. It could be extracted out, e.g. is_gemfile?.

parent = @node_context.parent
if parent.is_a?(Prism::CallNode) && parent.name == :gem && parent.arguments&.arguments&.first == node
generate_gem_hover(parent)
return
end
end

generate_heredoc_hover(node)
end

Expand Down
47 changes: 40 additions & 7 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,12 @@ class A
end
end

def test_hovering_over_gemfile_dependency
def test_hovering_over_gemfile_dependency_using_gem_call
source = <<~RUBY
gem 'rake'
RUBY

# We need to pretend that Sorbet is not a dependency or else we can't properly test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, I think these were leftover from a copy and paste.

with_server(source, URI("file:///Gemfile"), stub_no_typechecker: true) do |server, uri|
with_server(source, URI("file:///Gemfile")) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/hover",
Expand All @@ -295,13 +294,48 @@ def test_hovering_over_gemfile_dependency
end
end

def test_hovering_over_gemfile_dependency_using_gem_name
source = <<~RUBY
gem 'rake'
RUBY

with_server(source, URI("file:///Gemfile")) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/hover",
params: { textDocument: { uri: uri }, position: { character: 5, line: 0 } },
)

response = server.pop_response.response

assert_includes(response.contents.value, "rake")
end
end

def test_hovering_over_gemfile_dependency_triggers_only_for_first_arg
source = <<~RUBY
gem 'rake', '~> 1.0'
RUBY

with_server(source, URI("file:///Gemfile")) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/hover",
params: { textDocument: { uri: uri }, position: { character: 13, line: 0 } },
)

response = server.pop_response.response

assert_nil(response)
end
end

def test_hovering_over_gemfile_dependency_with_missing_argument
source = <<~RUBY
gem()
RUBY

# We need to pretend that Sorbet is not a dependency or else we can't properly test
with_server(source, URI("file:///Gemfile"), stub_no_typechecker: true) do |server, uri|
with_server(source, URI("file:///Gemfile")) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/hover",
Expand All @@ -317,8 +351,7 @@ def test_hovering_over_gemfile_dependency_with_non_gem_argument
gem(method_call)
RUBY

# We need to pretend that Sorbet is not a dependency or else we can't properly test
with_server(source, URI("file:///Gemfile"), stub_no_typechecker: true) do |server, uri|
with_server(source, URI("file:///Gemfile")) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/hover",
Expand Down
Loading