Skip to content
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
17 changes: 13 additions & 4 deletions lib/typeprof/core/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,20 @@ def type_definitions(path, pos)
if node.ret
ty_defs = []
node.ret.types.map do |ty, _source|
if ty.is_a?(Type::Instance)
ty.mod.module_decls.each do |mdecl|
# TODO
mod = case ty
when Type::Instance, Type::Singleton
ty.mod
else
base = ty.base_type(@genv)
base.mod if base.is_a?(Type::Instance)
end

if mod
mod.module_decls.each do |mdecl|
decl_path = mdecl.lenv.path
ty_defs << [decl_path, mdecl.code_range] if decl_path
end
ty.mod.module_defs.each do |mdef_node|
mod.module_defs.each do |mdef_node|
ty_defs << [mdef_node.lenv.path, mdef_node.code_range]
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/type_definition/sig/test.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Foo
end
5 changes: 5 additions & 0 deletions test/fixtures/type_definition/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo
end

foo = Foo.new
foo
5 changes: 5 additions & 0 deletions test/fixtures/type_definition/typeprof.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"typeprof_version": "experimental",
"analysis_unit_dirs": ["."],
"sig_dirs": ["sig"]
}
74 changes: 73 additions & 1 deletion test/lsp/lsp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,78 @@ def test(x)
end
end

# TODO: add a test for definition (for crossing files)
def test_type_definition_for_class_constant
init("type_definition")

notify(
"textDocument/didOpen",
textDocument: { uri: @folder + "test.rb", version: 0, text: <<-END },
class Foo
end

foo = Foo.new
foo
END
)

expect_notification("typeprof.enableToggleButton") {|json| }
expect_request("workspace/codeLens/refresh") {|json| }

id = request(
"textDocument/typeDefinition",
textDocument: { uri: @folder + "test.rb" },
position: { line: 3, character: 6 },
)

expect_response(id) do |json|
assert_equal(2, json.size)

rbs_result = json.find { |r| r[:uri].end_with?(".rbs") }
rb_result = json.find { |r| r[:uri].end_with?(".rb") }

assert_not_nil(rbs_result, "RBS definition should be found")
assert_not_nil(rb_result, "Ruby definition should be found")

assert(rbs_result[:uri].end_with?("sig/test.rbs"))
assert(rb_result[:uri].end_with?("test.rb"))
end
end

def test_type_definition_for_local_variable
init("type_definition")

notify(
"textDocument/didOpen",
textDocument: { uri: @folder + "test.rb", version: 0, text: <<-END },
class Foo
end

foo = Foo.new
foo
END
)

expect_notification("typeprof.enableToggleButton") {|json| }
expect_request("workspace/codeLens/refresh") {|json| }

id = request(
"textDocument/typeDefinition",
textDocument: { uri: @folder + "test.rb" },
position: { line: 4, character: 0 },
)

expect_response(id) do |json|
assert_equal(2, json.size)

rbs_result = json.find { |r| r[:uri].end_with?(".rbs") }
rb_result = json.find { |r| r[:uri].end_with?(".rb") }

assert_not_nil(rbs_result, "RBS definition should be found")
assert_not_nil(rb_result, "Ruby definition should be found")

assert(rbs_result[:uri].end_with?("sig/test.rbs"))
assert(rb_result[:uri].end_with?("test.rb"))
end
end
end
end