1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
# typed: true
# frozen_string_literal: true
require "test_helper"
class CompletionResolveTest < Minitest::Test
include RubyLsp::Requests::Support::Common
Interface = LanguageServer::Protocol::Interface
Constant = LanguageServer::Protocol::Constant
def test_completion_resolve_for_constant
source = +<<~RUBY
module Foo
# This is a class that does things
class Bar
end
end
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
existing_item = {
label: "Foo::Bar",
insertText: "Bar",
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
expected = existing_item.merge(
documentation: Interface::MarkupContent.new(
kind: "markdown",
value: markdown_from_index_entries(
"Foo::Bar",
server.global_state.index["Foo::Bar"], #: as !nil
),
),
)
assert_match(/This is a class that does things/, result[:documentation].value)
assert_equal(expected.to_json, result.to_json)
refute(result.key?(:labelDetails))
end
end
def test_completion_resolve_with_owner_present
source = +<<~RUBY
class Bar
def initialize
# Bar!
@a = 1
end
end
class Foo
def initialize
# Foo!
@a = 1
end
end
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
existing_item = {
label: "@a",
kind: RubyLsp::Constant::CompletionItemKind::FIELD,
data: { owner_name: "Foo" },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_match(/Foo/, result[:documentation].value)
existing_item = {
label: "@a",
kind: 5,
data: { owner_name: "Bar" },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_match(/Bar/, result[:documentation].value)
end
end
def test_inserts_method_parameters_in_label_details
source = +<<~RUBY
class Bar
def foo(a, b, c)
end
def bar
f
end
end
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
existing_item = {
label: "foo",
kind: RubyLsp::Constant::CompletionItemKind::METHOD,
data: { owner_name: "Bar" },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_match("(a, b, c)", result[:documentation].value)
end
end
def test_indicates_signature_count_in_label_details
source = +<<~RUBY
String.try_convert
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
index = server.instance_variable_get(:@global_state).index
RubyIndexer::RBSIndexer.new(index).index_ruby_core
existing_item = {
label: "try_convert",
kind: RubyLsp::Constant::CompletionItemKind::METHOD,
data: { owner_name: "String::<Class:String>" },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_match("try_convert(object)", result[:documentation].value)
assert_match("(+2 overloads)", result[:documentation].value)
end
end
def test_resolve_handles_method_aliases
with_server("", stub_no_typechecker: true) do |server, _uri|
index = server.instance_variable_get(:@global_state).index
RubyIndexer::RBSIndexer.new(index).index_ruby_core
# This is initially an unresolved method alias. In regular operations, completion runs first, resolves the alias
# and then completionResolve doesn't have to do it. For the test, we need to do it manually
index.resolve_method("kind_of?", "Kernel")
existing_item = {
label: "kind_of?",
kind: RubyLsp::Constant::CompletionItemKind::METHOD,
data: { owner_name: "Kernel" },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_match("**Definitions**: [kernel.rbs]", result[:documentation].value)
end
end
def test_completion_documentation_for_guessed_types
source = +<<~RUBY
class User
def foo(a, b, c)
end
end
user.f
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
existing_item = {
label: "foo",
kind: RubyLsp::Constant::CompletionItemKind::METHOD,
data: { owner_name: "User", guessed_type: "User" },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_match("Guessed receiver: User", result[:documentation].value)
assert_match("Learn more about guessed types", result[:documentation].value)
end
end
def test_resolve_for_keywords
source = +<<~RUBY
def foo
yield
end
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
existing_item = {
label: "yield",
kind: RubyLsp::Constant::CompletionItemKind::KEYWORD,
data: { keyword: true },
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
contents = result[:documentation].value
assert_match("```ruby\nyield\n```", contents)
assert_match(
RubyLsp::KEYWORD_DOCS["yield"], #: as !nil
contents,
)
expected_uri = URI::Generic.from_path(path: File.join(RubyLsp::STATIC_DOCS_PATH, "yield.md"))
assert_match("[Read more](#{expected_uri})", contents)
end
end
def test_resolve_for_require_completion
source = +<<~RUBY
require ""
RUBY
with_server(source, stub_no_typechecker: true) do |server, _uri|
existing_item = {
label: "foo",
kind: RubyLsp::Constant::CompletionItemKind::FILE,
data: {},
}
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
result = server.pop_response.response
assert_nil(result[:documentation])
end
end
end
|