From e46ce5f8090922421e51487e92f2e00bebf70d2d Mon Sep 17 00:00:00 2001 From: snutij Date: Mon, 21 Oct 2024 23:29:45 +0200 Subject: [PATCH] fix: avoid duplicated completions names --- lib/ruby_lsp/listeners/completion.rb | 2 +- test/requests/completion_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/ruby_lsp/listeners/completion.rb b/lib/ruby_lsp/listeners/completion.rb index 1b9087e24..84898bb15 100644 --- a/lib/ruby_lsp/listeners/completion.rb +++ b/lib/ruby_lsp/listeners/completion.rb @@ -311,7 +311,7 @@ def handle_global_variable_completion(name, location) range = range_from_location(location) - candidates.flatten.each do |entry| + candidates.flatten.uniq(&:name).each do |entry| entry_name = entry.name @response_builder << Interface::CompletionItem.new( diff --git a/test/requests/completion_test.rb b/test/requests/completion_test.rb index 3bd2b640a..66b5e34bb 100644 --- a/test/requests/completion_test.rb +++ b/test/requests/completion_test.rb @@ -1065,6 +1065,28 @@ def test_completion_for_global_variables end end + def test_completion_for_global_variables_show_only_uniq_entries + source = <<~RUBY + $qar &&= 1 + $qar += 1 + $qar ||= 1 + $q + RUBY + + with_server(source) do |server, uri| + index = server.instance_variable_get(:@global_state).index + RubyIndexer::RBSIndexer.new(index).index_ruby_core + + server.process_message(id: 1, method: "textDocument/completion", params: { + textDocument: { uri: uri }, + position: { line: 3, character: 2 }, + }) + + result = server.pop_response.response + assert_equal(["$qar"], result.map(&:label)) + end + end + def test_completion_for_instance_variables source = +<<~RUBY class Foo