Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword completion in block call #865

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/steep/node_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,28 @@ def test_send_node(node)
false
end
end

def deconstruct_sendish_and_block_nodes(*nodes)
send_node, block_node = nodes.take(2)

if send_node
case send_node.type
when :send, :csend, :super
if block_node
case block_node.type
when :block, :numblock
if send_node.equal?(block_node.children[0])
return [send_node, block_node]
end
end
end

[send_node, nil]
when :zsuper
# zsuper doesn't receive block
[send_node, nil]
end
end
end
end
end
41 changes: 30 additions & 11 deletions lib/steep/services/completion_provider.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Steep
module Services
class CompletionProvider
include NodeHelper

Position = _ = Struct.new(:line, :column, keyword_init: true) do
# @implements Position
def -(size)
Expand Down Expand Up @@ -308,7 +310,7 @@ def items_for_trigger(position:)
node, *parents = source.find_nodes(line: position.line, column: position.column)
node ||= source.node

return [] unless node
return [] unless node && parents

items = [] #: Array[item]

Expand All @@ -321,7 +323,16 @@ def items_for_trigger(position:)

method_items_for_receiver_type(context.self_type, include_private: true, prefix: prefix, position: position, items: items)
local_variable_items_for_context(context, position: position, prefix: prefix, items: items)
keyword_argument_items_for_method(node: parents&.first, position: position, prefix: prefix, items: items)

if (send_node, block_node = deconstruct_sendish_and_block_nodes(*parents))
keyword_argument_items_for_method(
call_node: block_node || send_node,
send_node: send_node,
position: position,
prefix: prefix,
items: items
)
end

when node.type == :lvar && at_end?(position, of: node.loc)
# foo ← (lvar)
Expand Down Expand Up @@ -548,10 +559,17 @@ def items_for_following_keyword_arguments(text, index:, line:, column:, items:)
return
end

node, *_parents = source.find_nodes(line: line, column: column)
if node && (node.type == :send || node.type == :csend)
position = Position.new(line: line, column: column)
keyword_argument_items_for_method(node: node, position: position, prefix: argname.join, items: items)
if nodes = source.find_nodes(line: line, column: column)
if (send_node, block_node = deconstruct_sendish_and_block_nodes(*nodes))
position = Position.new(line: line, column: column)
keyword_argument_items_for_method(
call_node: block_node || send_node,
send_node: send_node,
position: position,
prefix: argname.join,
items: items
)
end
end
end

Expand Down Expand Up @@ -669,16 +687,17 @@ def instance_variable_items_for_context(context, position:, prefix:, items:)
end
end

def keyword_argument_items_for_method(node:, position:, prefix:, items:)
return unless node && (node.type == :send || node.type == :csend)
def keyword_argument_items_for_method(call_node:, send_node:, position:, prefix:, items:)
receiver_node, method_name, argument_nodes = deconstruct_send_node!(send_node)

call = typing.call_of(node: call_node)

call = typing.call_of(node: node)
case call
when TypeInference::MethodCall::Typed, TypeInference::MethodCall::Error
type = call.receiver_type
shape = subtyping.builder.shape(
type,
public_only: !node.children[0].nil?,
public_only: !!receiver_node,
config: Interface::Builder::Config.new(self_type: type, class_type: nil, instance_type: nil, variable_bounds: {})
)
if shape
Expand All @@ -687,7 +706,7 @@ def keyword_argument_items_for_method(node:, position:, prefix:, items:)
defn = method_type.method_decls.to_a[0]&.method_def
if defn
range = range_for(position, prefix: prefix)
kwargs = node.children[2...]&.find { |arg| arg.type == :kwargs }&.children || []
kwargs = argument_nodes.find { |arg| arg.type == :kwargs }&.children || []
used_kwargs = kwargs.filter_map { |arg| arg.type == :pair && arg.children.first.children.first }

kwargs = defn.type.type.required_keywords.keys + defn.type.type.optional_keywords.keys
Expand Down
9 changes: 9 additions & 0 deletions sig/steep/node_helper.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,14 @@ module Steep
def deconstruct_send_node!: (Node) -> [Node?, Symbol, Array[Node], send_loc]

def test_send_node: (Node) { (Node?, Symbol, Array[Node], send_loc) -> bool } -> bool

# Deconstruct sendish node and it's associated block node
#
# Receives a sequence of node tree where the leaf node comes first.
# If the first node is `send`, `csend`, `super`, or `zsuper`, it is the sendish node.
#
# If the next node is a `block` or `numblock` that is associated to the *sendish node*, it is the block node.
#
def deconstruct_sendish_and_block_nodes: (*Parser::AST::Node) -> [Parser::AST::Node, Parser::AST::Node?]?
end
end
5 changes: 3 additions & 2 deletions sig/steep/services/completion_provider.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use Steep::TypeInference::MethodCall
module Steep
module Services
class CompletionProvider
include NodeHelper

# Cursor position
class Position
attr_reader line: Integer
Expand Down Expand Up @@ -237,14 +239,13 @@ module Steep

def instance_variable_items_for_context: (TypeInference::Context context, position: Position, prefix: String, items: Array[item]) -> void

def keyword_argument_items_for_method: (node: Parser::AST::Node?, position: Position, prefix: String, items: Array[item]) -> void
def keyword_argument_items_for_method: (call_node: Parser::AST::Node, send_node: Parser::AST::Node, position: Position, prefix: String, items: Array[item]) -> void

def index_for: (String, line: Integer, column: Integer) -> Integer

def disallowed_method?: (Symbol name) -> bool

def unwrap_optional: (AST::Types::t) -> AST::Types::t

end
end
end
19 changes: 19 additions & 0 deletions test/completion_provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,23 @@ def foo: (arg1: Integer, arg2: Integer, ?arg3: Integer, ?arg4: Integer) -> void
end
end
end

def test_keyword_argument_block
with_checker <<EOF do
class TestClass
def foo: (arg1: Integer, arg2: Integer, ?arg3: Integer, ?arg4: Integer) { () -> void } -> void
end
EOF
CompletionProvider.new(source_text: <<-EOR, path: Pathname("foo.rb"), subtyping: checker).tap do |provider|
TestClass.new.foo(arg1: 1, a) do
end
# ^
EOR

provider.run(line: 1, column: 28).tap do |items|
assert_equal ["arg2:", "arg3:", "arg4:"], items.map(&:identifier)
end
end
end
end
end