Skip to content

Skip invalid completion check immediately after newline #55061

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 18 commits into from
Nov 20, 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
15 changes: 14 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,10 @@ function getCompletionData(
return undefined;
}

function isInDifferentLineThanContextToken(contextToken: Node, position: number): boolean {
return sourceFile.getLineEndOfPosition(contextToken.getEnd()) < position;
}

/**
* @returns true if we are certain that the currently edited location must define a new location; false otherwise.
*/
Expand Down Expand Up @@ -4724,15 +4728,24 @@ function getCompletionData(
case SyntaxKind.SetKeyword:
return !isFromObjectTypeDeclaration(contextToken);

case SyntaxKind.Identifier:
case SyntaxKind.Identifier: {
if (containingNodeKind === SyntaxKind.ImportSpecifier &&
contextToken === (parent as ImportSpecifier).name &&
(contextToken as Identifier).text === "type"
) {
// import { type | }
return false;
}
const ancestorVariableDeclaration = findAncestor(
contextToken.parent, isVariableDeclaration);
if (ancestorVariableDeclaration
&& isInDifferentLineThanContextToken(contextToken, position)) {
// let a
// |
return false;
}
break;
}

case SyntaxKind.ClassKeyword:
case SyntaxKind.EnumKeyword:
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/completionAfterNewline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />

// issue: https://github.com/microsoft/TypeScript/issues/54729
// Tests that `isCompletionListBlocker` returns true at position 1, and returns false after a newline.


////let foo /*1*/
/////*2*/
/////*3*/

verify.completions(
{ marker: "1", exact: undefined },
{
marker: ["2", "3"],
exact: completion.globalsPlus([
{
name: "foo",
},
]),
}
);
19 changes: 19 additions & 0 deletions tests/cases/fourslash/completionAfterNewline2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

// issue: https://github.com/microsoft/TypeScript/issues/54729
// Tests that `isCompletionListBlocker` returns true at position 1, and returns false after a newline.

////let foo = 5 as const /*1*/
/////*2*/

verify.completions(
{ marker: "1", exact: undefined },
{
marker: "2",
exact: completion.globalsPlus([
{
name: "foo",
},
]),
}
);