@@ -267,7 +267,7 @@ class A
267267 private_constant(:CONST)
268268 end
269269
270- A::CONST
270+ A::CONST # invalid private reference
271271 RUBY
272272
273273 with_server ( source , stub_no_typechecker : true ) do |server , uri |
@@ -277,8 +277,141 @@ class A
277277 params : { textDocument : { uri : uri } , position : { character : 3 , line : 5 } } ,
278278 )
279279
280- # TODO: once we have visibility exposed from Rubydex, let's show that the constant is private
281- assert_match ( "A::CONST" , server . pop_response . response . contents . value )
280+ assert_nil ( server . pop_response . response )
281+ end
282+ end
283+
284+ def test_hovering_over_private_method_with_implicit_self
285+ source = <<~RUBY
286+ class A
287+ def bar
288+ foo
289+ end
290+
291+ private
292+
293+ # foo docs
294+ def foo; end
295+ end
296+ RUBY
297+
298+ with_server ( source , stub_no_typechecker : true ) do |server , uri |
299+ server . process_message (
300+ id : 1 ,
301+ method : "textDocument/hover" ,
302+ params : { textDocument : { uri : uri } , position : { character : 4 , line : 2 } } ,
303+ )
304+
305+ assert_match ( "foo docs" , server . pop_response . response . contents . value )
306+ end
307+ end
308+
309+ def test_does_not_hover_over_private_method_called_with_explicit_external_receiver
310+ source = <<~RUBY
311+ class A
312+ private
313+
314+ # foo docs
315+ def foo; end
316+ end
317+
318+ class B
319+ def bar
320+ a = A.new
321+ a.foo
322+ end
323+ end
324+ RUBY
325+
326+ with_server ( source , stub_no_typechecker : true ) do |server , uri |
327+ server . process_message (
328+ id : 1 ,
329+ method : "textDocument/hover" ,
330+ params : { textDocument : { uri : uri } , position : { character : 6 , line : 10 } } ,
331+ )
332+
333+ assert_nil ( server . pop_response . response )
334+ end
335+ end
336+
337+ def test_hovers_protected_method_inside_same_class
338+ source = <<~RUBY
339+ class A
340+ def bar
341+ self.foo
342+ end
343+
344+ protected
345+
346+ # foo docs
347+ def foo; end
348+ end
349+ RUBY
350+
351+ with_server ( source , stub_no_typechecker : true ) do |server , uri |
352+ server . process_message (
353+ id : 1 ,
354+ method : "textDocument/hover" ,
355+ params : { textDocument : { uri : uri } , position : { character : 9 , line : 2 } } ,
356+ )
357+
358+ assert_match ( "foo docs" , server . pop_response . response . contents . value )
359+ end
360+ end
361+
362+ def test_hovers_protected_method_called_from_subclass
363+ source = <<~RUBY
364+ class A
365+ protected
366+
367+ # foo docs
368+ def foo; end
369+ end
370+
371+ class B < A
372+ def bar
373+ a = A.new
374+ a.foo
375+ end
376+ end
377+ RUBY
378+
379+ with_server ( source , stub_no_typechecker : true ) do |server , uri |
380+ server . process_message (
381+ id : 1 ,
382+ method : "textDocument/hover" ,
383+ params : { textDocument : { uri : uri } , position : { character : 6 , line : 10 } } ,
384+ )
385+
386+ assert_match ( "foo docs" , server . pop_response . response . contents . value )
387+ end
388+ end
389+
390+ def test_does_not_hover_protected_method_from_unrelated_class
391+ source = <<~RUBY
392+ class A
393+ protected
394+
395+ # foo docs
396+ def foo; end
397+ end
398+
399+ class C
400+ def bar
401+ a = A.new
402+ a.foo
403+ end
404+ end
405+ RUBY
406+
407+ with_server ( source , stub_no_typechecker : true ) do |server , uri |
408+ server . process_message (
409+ id : 1 ,
410+ method : "textDocument/hover" ,
411+ params : { textDocument : { uri : uri } , position : { character : 6 , line : 10 } } ,
412+ )
413+
414+ assert_nil ( server . pop_response . response )
282415 end
283416 end
284417
0 commit comments