Skip to content

Commit ddd6a30

Browse files
authored
[ty] Do not suggest argument completion when at value of keyword argument (#24669)
In the following situation: ```python def foo(y_true,y_pred): ... y_true = 1 y_pred = 2 foo(y_true=y<CURSOR>) ``` the completion suggestions began with `y_true=` and `y_pred=`. But it is never the right thing to suggest an argument completion (i.e. `something=`) when the cursor is at the _value_ of a keyword argument. So this PR introduces an early exit to the logic for deciding to suggest arguments.
1 parent 9282e61 commit ddd6a30

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

crates/ty_ide/src/completion.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,13 @@ fn add_argument_completions<'db>(
14111411
let mut in_arguments = false;
14121412
for node in cursor.covering_node.ancestors() {
14131413
match node {
1414+
// Do not suggest argument completions in value positions for
1415+
// keyword arguments
1416+
ast::AnyNodeRef::Keyword(kw) => {
1417+
if kw.value.range().contains_range(cursor.range) {
1418+
return;
1419+
}
1420+
}
14141421
ast::AnyNodeRef::Arguments(_) => {
14151422
in_arguments = true;
14161423
}
@@ -8733,6 +8740,28 @@ re.match('', '', fla<CURSOR>
87338740
);
87348741
}
87358742

8743+
#[test]
8744+
fn call_keyword_argument_at_value() {
8745+
let builder = completion_test_builder(
8746+
"\
8747+
def bar(y_true,y_pred): ...
8748+
8749+
y_true = 1
8750+
y_pred = 2
8751+
8752+
bar(y_true=y<CURSOR>
8753+
",
8754+
);
8755+
8756+
assert_snapshot!(
8757+
builder.skip_keywords().skip_builtins().skip_auto_import().build().snapshot(),
8758+
@r###"
8759+
y_pred
8760+
y_true
8761+
"###
8762+
);
8763+
}
8764+
87368765
// Ideally, we should favour completions that are definitely raisable
87378766
// here. However, doing so would require `exception_ty` to fall back to
87388767
// token matching when AST-matching fails, making the function signficantly

0 commit comments

Comments
 (0)