-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup some logic around checking method arg/values
- Loading branch information
1 parent
955dc27
commit 62284ef
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Rails | ||
class Completion | ||
extend T::Sig | ||
include Requests::Support::Common | ||
|
||
sig do | ||
override.params( | ||
client: RunnerClient, | ||
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem], | ||
node_context: NodeContext, | ||
dispatcher: Prism::Dispatcher, | ||
uri: URI::Generic, | ||
).void | ||
end | ||
def initialize(client, response_builder, node_context, dispatcher, uri) | ||
@response_builder = response_builder | ||
@client = client | ||
@node_context = node_context | ||
dispatcher.register( | ||
self, | ||
:on_call_node_enter, | ||
) | ||
end | ||
|
||
sig { params(node: Prism::CallNode).void } | ||
def on_call_node_enter(node) | ||
if @node_context.call_node && @node_context.call_node&.name == :where | ||
handle_active_record_where_completions(node) | ||
end | ||
end | ||
|
||
private | ||
|
||
sig { params(node: Prism::CallNode).void } | ||
def handle_active_record_where_completions(node) | ||
receiver = T.must(@node_context.call_node).receiver | ||
return if receiver.nil? | ||
return unless receiver.is_a?(Prism::ConstantReadNode) | ||
|
||
resolved_class = @client.model(receiver.name.to_s) | ||
return if resolved_class.nil? | ||
|
||
arguments = T.must(@node_context.call_node).arguments&.arguments | ||
existing_args = T.let({}, T::Hash[String, T::Boolean]) | ||
|
||
if arguments&.is_a?(Array) | ||
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.key?(column[0]) | ||
|
||
@response_builder << Interface::CompletionItem.new( | ||
label: column[0], | ||
filter_text: column[0], | ||
label_details: Interface::CompletionItemLabelDetails.new( | ||
description: "Filter #{receiver.name} records by #{column[0]}", | ||
), | ||
text_edit: Interface::TextEdit.new(range: range_from_location(node.location), new_text: "#{column[0]}: "), | ||
kind: Constant::CompletionItemKind::FIELD, | ||
) | ||
end | ||
end | ||
|
||
sig { params(arguments: T::Array[Prism::Node]).returns(T::Hash[String, T::Boolean]) } | ||
def get_call_node_arguments(arguments:) | ||
existing_args = {} | ||
arguments.each do |argument| | ||
next unless argument.is_a?(Prism::KeywordHashNode) | ||
|
||
argument.elements.each do |e| | ||
next unless e.is_a?(Prism::AssocNode) | ||
|
||
key = e.key | ||
if key.is_a?(Prism::SymbolNode) | ||
existing_args[key.value] = e.value | ||
end | ||
end | ||
end | ||
existing_args | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
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: 37 }) | ||
# typed: false | ||
User.where(id:, first_name:, first_na | ||
RUBY | ||
|
||
assert_equal(0, response.size) | ||
end | ||
|
||
test "Provides suggestions when typing column name partially" do | ||
response = generate_completions_for_source(<<~RUBY, { line: 1, character: 17 }) | ||
# typed: false | ||
User.where(first_ | ||
RUBY | ||
|
||
assert_equal(1, response.size) | ||
assert_equal("first_name", response[0].label) | ||
assert_equal("first_name", response[0].filter_text) | ||
assert_equal(11, response[0].text_edit.range.start.character) | ||
assert_equal(1, response[0].text_edit.range.start.line) | ||
assert_equal(17, response[0].text_edit.range.end.character) | ||
assert_equal(1, response[0].text_edit.range.end.line) | ||
end | ||
|
||
test "Does not provide suggestion when typing argument value" do | ||
response = generate_completions_for_source(<<~RUBY, { line: 1, character: 20 }) | ||
# typed: false | ||
User.where(id: creat | ||
RUBY | ||
assert_equal(0, response.size) | ||
end | ||
|
||
private | ||
|
||
def generate_completions_for_source(source, position) | ||
with_server(source) do |server, uri| | ||
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@rails_runner_client).is_a?(NullClient) | ||
|
||
server.process_message( | ||
id: 1, | ||
method: "textDocument/completion", | ||
params: { textDocument: { uri: uri }, position: position }, | ||
) | ||
|
||
result = pop_result(server) | ||
result.response | ||
end | ||
end | ||
end | ||
end | ||
end |