From 28f2c681ac6d93a4aff41e514bf0a443333e5c7a Mon Sep 17 00:00:00 2001 From: rogancodes Date: Tue, 17 Dec 2024 17:27:04 +0530 Subject: [PATCH] added test cases for hover, definition, and completion of class variables in different contexts --- lib/ruby_lsp/type_inferrer.rb | 2 +- test/requests/completion_test.rb | 38 ++++++++++++++++ test/requests/definition_expectations_test.rb | 39 ++++++++++++++++ test/requests/hover_expectations_test.rb | 44 +++++++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) diff --git a/lib/ruby_lsp/type_inferrer.rb b/lib/ruby_lsp/type_inferrer.rb index 85732c9af..d8f045b3f 100644 --- a/lib/ruby_lsp/type_inferrer.rb +++ b/lib/ruby_lsp/type_inferrer.rb @@ -24,7 +24,7 @@ def infer_receiver_type(node_context) Prism::SuperNode, Prism::ForwardingSuperNode self_receiver_handling(node_context) when Prism::ClassVariableAndWriteNode, Prism::ClassVariableWriteNode, Prism::ClassVariableOperatorWriteNode, - Prism::ClassVariableOrWriteNode, Prism::ClassVariableReadNode,Prism::ClassVariableTargetNode + Prism::ClassVariableOrWriteNode, Prism::ClassVariableReadNode, Prism::ClassVariableTargetNode infer_receiver_for_class_variables(node_context) end end diff --git a/test/requests/completion_test.rb b/test/requests/completion_test.rb index 62a0e43d1..5eeda13dd 100644 --- a/test/requests/completion_test.rb +++ b/test/requests/completion_test.rb @@ -1180,6 +1180,44 @@ def baz end end + def test_completion_for_class_variables_in_different_context + source = <<~RUBY + class Foo + @@a = 1 + + class << self + @@b = 2 + + def foo + @@c = 3 + end + end + + def bar + @ + end + + def baz + @@ = 4 + end + + def self.foobar + @@d = 5 + end + end + RUBY + + with_server(source, stub_no_typechecker: true) do |server, uri| + server.process_message(id: 1, method: "textDocument/completion", params: { + textDocument: { uri: uri }, + position: { line: 12, character: 5 }, + }) + + result = server.pop_response.response + assert_equal(["@@a", "@@b", "@@c", "@@d"], result.map(&:label)) + end + end + def test_completion_for_instance_variables source = +<<~RUBY class Foo diff --git a/test/requests/definition_expectations_test.rb b/test/requests/definition_expectations_test.rb index ff125d66d..eb703e89c 100644 --- a/test/requests/definition_expectations_test.rb +++ b/test/requests/definition_expectations_test.rb @@ -810,6 +810,45 @@ def do_something end end + def test_definition_for_class_variables_in_different_context + source = <<~RUBY + class Foo + @@a = 1 + + class << self + @@a = 2 + + def foo + @@a = 3 + end + end + + def bar + @@a = 4 + end + + def self.baz + @@a = 5 + end + end + RUBY + + with_server(source) do |server, uri| + server.process_message( + id: 1, + method: "textDocument/definition", + params: { textDocument: { uri: uri }, position: { character: 4, line: 1 } }, + ) + response = server.pop_response.response + + assert_equal(1, response[0].range.start.line) + assert_equal(4, response[1].range.start.line) + assert_equal(7, response[2].range.start.line) + assert_equal(12, response[3].range.start.line) + assert_equal(16, response[4].range.start.line) + end + end + def test_definition_for_instance_variables source = <<~RUBY class Foo diff --git a/test/requests/hover_expectations_test.rb b/test/requests/hover_expectations_test.rb index d8086f72e..cadd6892a 100644 --- a/test/requests/hover_expectations_test.rb +++ b/test/requests/hover_expectations_test.rb @@ -500,6 +500,50 @@ def do_something end end + def test_hovering_for_class_variables_in_different_context + source = <<~RUBY + class Foo + # comment 1 + @@a = 1 + + class << self + # comment 2 + @@a = 2 + + def foo + # comment 3 + @@a = 3 + end + end + + def bar + # comment 4 + @@a = 4 + end + + def self.baz + # comment 5 + @@a = 5 + end + end + RUBY + + with_server(source) do |server, uri| + server.process_message( + id: 1, + method: "textDocument/hover", + params: { textDocument: { uri: uri }, position: { character: 4, line: 2 } }, + ) + + contents = server.pop_response.response.contents.value + assert_match("comment 1", contents) + assert_match("comment 2", contents) + assert_match("comment 3", contents) + assert_match("comment 4", contents) + assert_match("comment 5", contents) + end + end + def test_hovering_over_inherited_methods source = <<~RUBY module Foo