Skip to content

Commit eb40df3

Browse files
authored
Show full constant names on hover (#3457)
1 parent 28ddf36 commit eb40df3

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,10 @@ def generate_hover(name, location)
366366
# We should only show hover for private constants if the constant is defined in the same namespace as the
367367
# reference
368368
first_entry = entries.first #: as !nil
369-
return if first_entry.private? && first_entry.name != "#{@node_context.fully_qualified_name}::#{name}"
369+
full_name = first_entry.name
370+
return if first_entry.private? && full_name != "#{@node_context.fully_qualified_name}::#{name}"
370371

371-
categorized_markdown_from_index_entries(name, entries).each do |category, content|
372+
categorized_markdown_from_index_entries(full_name, entries).each do |category, content|
372373
@response_builder.push(content, category: category)
373374
end
374375
end

test/requests/hover_expectations_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,46 @@ def with_foo(foo)
971971
end
972972
end
973973

974+
def test_hovering_constants_shows_complete_name
975+
source = <<~RUBY
976+
# typed: ignore
977+
module Foo
978+
CONST = 123
979+
980+
module Bar
981+
class Baz; end
982+
983+
Baz
984+
end
985+
end
986+
987+
QUX = 42
988+
RUBY
989+
990+
with_server(source) do |server, uri|
991+
server.process_message(
992+
id: 1,
993+
method: "textDocument/hover",
994+
params: { textDocument: { uri: uri }, position: { character: 4, line: 7 } },
995+
)
996+
assert_match("```ruby\nFoo::Bar::Baz\n```", server.pop_response.response.contents.value)
997+
998+
server.process_message(
999+
id: 1,
1000+
method: "textDocument/hover",
1001+
params: { textDocument: { uri: uri }, position: { character: 2, line: 2 } },
1002+
)
1003+
assert_match("```ruby\nFoo::CONST\n```", server.pop_response.response.contents.value)
1004+
1005+
server.process_message(
1006+
id: 1,
1007+
method: "textDocument/hover",
1008+
params: { textDocument: { uri: uri }, position: { character: 0, line: 11 } },
1009+
)
1010+
assert_match("```ruby\nQUX\n```", server.pop_response.response.contents.value)
1011+
end
1012+
end
1013+
9741014
private
9751015

9761016
def create_hover_addon

0 commit comments

Comments
 (0)