Skip to content

Commit

Permalink
added test cases for hover, definition, and completion of class varia…
Browse files Browse the repository at this point in the history
…bles in different contexts
  • Loading branch information
rogancodes committed Dec 17, 2024
1 parent 80a7595 commit 28f2c68
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/type_inferrer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions test/requests/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/requests/definition_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 28f2c68

Please sign in to comment.