Skip to content

Improve contextual completions #53554

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

Merged
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
2 changes: 1 addition & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ function getContextualType(previousToken: Node, position: number, sourceFile: So
isEqualityOperatorKind(previousToken.kind) && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind) ?
// completion at `x ===/**/` should be for the right side
checker.getTypeAtLocation(parent.left) :
checker.getContextualType(previousToken as Expression);
checker.getContextualType(previousToken as Expression, ContextFlags.Completions) || checker.getContextualType(previousToken as Expression);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />

//// declare function test<P extends "a" | "b">(p: P): void;
////
//// test(/*ts*/)
////

verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

//// declare function test<T extends 'a' | 'b'>(a: { foo: T[] }): void
////
//// test({ foo: [/*ts*/] })

verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

//// declare function test<T extends 'a' | 'b'>(a: { foo: T[] }): void
////
//// test({ foo: ['a', /*ts*/] })

verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't exactly understand what isNewIdentifierLocation is about. It's true for most of the added test cases but false for object property values. Is this correct? What's the difference between an object property value and an array element's locations? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is it's just a bad heuristic that could be patched up?

For reference, it's discussed a bit here: #42595 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can look into this in a separate PR - it would be cool to have this consistent.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

//// declare function test<P extends "a" | "b">(p: { type: P }): void;
////
//// test({ type: /*ts*/ })

verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: false });
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@
//// b: "/*ts*/",
//// },
//// });
////
//// test({
//// foo: {},
//// bar: {
//// b: /*ts2*/,
//// },
//// });

verify.completions({ marker: ["ts"], exact: ["foo", "bar"] });
verify.completions({ marker: ["ts2"], includes: ['"foo"', '"bar"'], isNewIdentifierLocation: false });
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@
//// b: ["/*ts*/"],
//// },
//// });
////
//// test({
//// foo: {},
//// bar: {
//// b: [/*ts2*/],
//// },
//// });

verify.completions({ marker: ["ts"], exact: ["foo", "bar"] });
verify.completions({ marker: ["ts2"], includes: ['"foo"', '"bar"'], isNewIdentifierLocation: true });