Skip to content

Commit

Permalink
Clean up some of the logic around checking for existing args/values
Browse files Browse the repository at this point in the history
  • Loading branch information
ChallaHalla committed Dec 12, 2024
1 parent 896ba66 commit 04407eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
23 changes: 3 additions & 20 deletions lib/ruby_lsp/ruby_lsp_rails/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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 = {}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/ruby_lsp_rails/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 04407eb

Please sign in to comment.