From 6e9d098d4134f865c9e352e46182ec6a2417dd41 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Wed, 3 Jul 2019 17:59:35 -0400 Subject: [PATCH 1/2] Adds support for completions after ASI inserted expressions Signed-off-by: Andrew Branch --- src/services/completions.ts | 18 +++++++++++++--- src/services/types.ts | 2 +- ...ompletionEntryAfterASIExpressionInClass.ts | 21 +++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/completionEntryAfterASIExpressionInClass.ts diff --git a/src/services/completions.ts b/src/services/completions.ts index 6d1d48b22b8..ce8fec3f04f 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1538,7 +1538,7 @@ namespace ts.Completions { * Relevant symbols are stored in the captured 'symbols' variable. */ function tryGetClassLikeCompletionSymbols(): GlobalsSearch { - const decl = tryGetObjectTypeDeclarationCompletionContainer(sourceFile, contextToken, location); + const decl = tryGetObjectTypeDeclarationCompletionContainer(sourceFile, contextToken, location, position); if (!decl) return GlobalsSearch.Continue; // We're looking up possible property names from parent type. @@ -2155,7 +2155,7 @@ namespace ts.Completions { * Returns the immediate owning class declaration of a context token, * on the condition that one exists and that the context implies completion should be given. */ - function tryGetObjectTypeDeclarationCompletionContainer(sourceFile: SourceFile, contextToken: Node | undefined, location: Node): ObjectTypeDeclaration | undefined { + function tryGetObjectTypeDeclarationCompletionContainer(sourceFile: SourceFile, contextToken: Node | undefined, location: Node, position: number): ObjectTypeDeclaration | undefined { // class c { method() { } | method2() { } } switch (location.kind) { case SyntaxKind.SyntaxList: @@ -2165,9 +2165,15 @@ namespace ts.Completions { if (cls && !findChildOfKind(cls, SyntaxKind.CloseBraceToken, sourceFile)) { return cls; } + break; + case SyntaxKind.Identifier: // class c extends React.Component { a: () => 1\n compon| } + if (isFromObjectTypeDeclaration(location)) { + return findAncestor(location, isObjectTypeDeclaration); + } } if (!contextToken) return undefined; + switch (contextToken.kind) { case SyntaxKind.SemicolonToken: // class c {getValue(): number; | } case SyntaxKind.CloseBraceToken: // class c { method() { } | } @@ -2179,7 +2185,13 @@ namespace ts.Completions { case SyntaxKind.CommaToken: // class c {getValue(): number, | } return tryCast(contextToken.parent, isObjectTypeDeclaration); default: - if (!isFromObjectTypeDeclaration(contextToken)) return undefined; + if (!isFromObjectTypeDeclaration(contextToken)) { + // class c extends React.Component { a: () => 1\n| } + if (getLineAndCharacterOfPosition(sourceFile, contextToken.getEnd()).line !== getLineAndCharacterOfPosition(sourceFile, position).line && isObjectTypeDeclaration(location)) { + return location; + } + return undefined; + } const isValidKeyword = isClassLike(contextToken.parent.parent) ? isClassMemberCompletionKeyword : isInterfaceOrTypeLiteralCompletionKeyword; return (isValidKeyword(contextToken.kind) || contextToken.kind === SyntaxKind.AsteriskToken || isIdentifier(contextToken) && isValidKeyword(stringToToken(contextToken.text)!)) // TODO: GH#18217 ? contextToken.parent.parent as ObjectTypeDeclaration : undefined; diff --git a/src/services/types.ts b/src/services/types.ts index b97125734f7..099263063a8 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -892,7 +892,7 @@ namespace ts { } export interface CompletionInfo { - /** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */ + /** Not true for all global completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */ isGlobalCompletion: boolean; isMemberCompletion: boolean; diff --git a/tests/cases/fourslash/completionEntryAfterASIExpressionInClass.ts b/tests/cases/fourslash/completionEntryAfterASIExpressionInClass.ts new file mode 100644 index 00000000000..61271140ad0 --- /dev/null +++ b/tests/cases/fourslash/completionEntryAfterASIExpressionInClass.ts @@ -0,0 +1,21 @@ +/// + +//// class Parent { +//// protected shouldWork() { +//// console.log(); +//// } +//// } +//// +//// class Child extends Parent { +//// // this assumes ASI, but on next line wants to +//// x = () => 1 +//// shoul/*insideid*/ +//// } +//// +//// class ChildTwo extends Parent { +//// // this assumes ASI, but on next line wants to +//// x = () => 1 +//// /*root*/ //nothing +//// } + +verify.completions({ marker: ["insideid", "root"], includes: "shouldWork", isNewIdentifierLocation: true }); From e55f97ec282d720f9bf58792ece0acc048202f93 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Mon, 8 Jul 2019 14:43:06 -0400 Subject: [PATCH 2/2] Updates the baselines for the typo fixes --- tests/baselines/reference/api/tsserverlibrary.d.ts | 2 +- tests/baselines/reference/api/typescript.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 944092e13d0..7853a4855ef 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -5398,7 +5398,7 @@ declare namespace ts { argumentCount: number; } interface CompletionInfo { - /** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */ + /** Not true for all global completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */ isGlobalCompletion: boolean; isMemberCompletion: boolean; /** diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index be8eea8062b..142ed88a111 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -5398,7 +5398,7 @@ declare namespace ts { argumentCount: number; } interface CompletionInfo { - /** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */ + /** Not true for all global completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */ isGlobalCompletion: boolean; isMemberCompletion: boolean; /**