From 04407eb5c22bee0829f1a4691a48f3818da92d22 Mon Sep 17 00:00:00 2001 From: Siddarth Challa Date: Thu, 12 Dec 2024 00:12:59 -0500 Subject: [PATCH] Clean up some of the logic around checking for existing args/values --- lib/ruby_lsp/ruby_lsp_rails/completion.rb | 23 +++-------------------- test/ruby_lsp_rails/completion_test.rb | 8 ++++---- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/completion.rb b/lib/ruby_lsp/ruby_lsp_rails/completion.rb index 450c28ed..db8d518b 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/completion.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/completion.rb @@ -48,14 +48,13 @@ def handle_active_record_where_completions(node) existing_args = T.let({}, T::Hash[String, T::Boolean]) if arguments&.is_a?(Array) - return if node_is_argument_value?(node: node, arguments: arguments) - existing_args = get_call_node_arguments(arguments: arguments) + return if existing_args.values.any? { |v| v == node } end resolved_class[:columns].each do |column| next unless column[0].start_with?(node.name.to_s) - next if existing_args[column[0]] + next if existing_args.key?(column[0]) @response_builder << Interface::CompletionItem.new( label: column[0], @@ -69,22 +68,6 @@ def handle_active_record_where_completions(node) end end - sig { params(node: Prism::Node, arguments: T::Array[Prism::Node]).returns(T::Boolean) } - def node_is_argument_value?(node:, arguments:) - arguments.any? do |argument| - next unless argument.is_a?(Prism::KeywordHashNode) - - argument.elements.any? do |e| - next unless e.is_a?(Prism::AssocNode) - - value = e.value - if value.is_a?(Prism::CallNode) - value == node - end - end - end - end - sig { params(arguments: T::Array[Prism::Node]).returns(T::Hash[String, T::Boolean]) } def get_call_node_arguments(arguments:) existing_args = {} @@ -96,7 +79,7 @@ def get_call_node_arguments(arguments:) key = e.key if key.is_a?(Prism::SymbolNode) - existing_args[key.value] = true + existing_args[key.value] = e.value end end end diff --git a/test/ruby_lsp_rails/completion_test.rb b/test/ruby_lsp_rails/completion_test.rb index 5840d147..8c91960a 100644 --- a/test/ruby_lsp_rails/completion_test.rb +++ b/test/ruby_lsp_rails/completion_test.rb @@ -7,9 +7,9 @@ module RubyLsp module Rails class CompletionTest < ActiveSupport::TestCase test "Does not suggest column if it already exists within .where as an arg and parantheses are not closed" do - response = generate_completions_for_source(<<~RUBY, { line: 1, character: 30 }) + response = generate_completions_for_source(<<~RUBY, { line: 1, character: 37 }) # typed: false - User.where(id:, first_name:, f + User.where(id:, first_name:, first_na RUBY assert_equal(0, response.size) @@ -31,9 +31,9 @@ class CompletionTest < ActiveSupport::TestCase end test "Does not provide suggestion when typing argument value" do - response = generate_completions_for_source(<<~RUBY, { line: 1, character: 16 }) + response = generate_completions_for_source(<<~RUBY, { line: 1, character: 20 }) # typed: false - User.where(id: f + User.where(id: creat RUBY assert_equal(0, response.size) end