Skip to content

Commit 6e9d098

Browse files
Orta TheroxAndrew Branch
authored andcommitted
Adds support for completions after ASI inserted expressions
Signed-off-by: Andrew Branch <[email protected]>
1 parent 340f810 commit 6e9d098

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/services/completions.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ namespace ts.Completions {
15381538
* Relevant symbols are stored in the captured 'symbols' variable.
15391539
*/
15401540
function tryGetClassLikeCompletionSymbols(): GlobalsSearch {
1541-
const decl = tryGetObjectTypeDeclarationCompletionContainer(sourceFile, contextToken, location);
1541+
const decl = tryGetObjectTypeDeclarationCompletionContainer(sourceFile, contextToken, location, position);
15421542
if (!decl) return GlobalsSearch.Continue;
15431543

15441544
// We're looking up possible property names from parent type.
@@ -2155,7 +2155,7 @@ namespace ts.Completions {
21552155
* Returns the immediate owning class declaration of a context token,
21562156
* on the condition that one exists and that the context implies completion should be given.
21572157
*/
2158-
function tryGetObjectTypeDeclarationCompletionContainer(sourceFile: SourceFile, contextToken: Node | undefined, location: Node): ObjectTypeDeclaration | undefined {
2158+
function tryGetObjectTypeDeclarationCompletionContainer(sourceFile: SourceFile, contextToken: Node | undefined, location: Node, position: number): ObjectTypeDeclaration | undefined {
21592159
// class c { method() { } | method2() { } }
21602160
switch (location.kind) {
21612161
case SyntaxKind.SyntaxList:
@@ -2165,9 +2165,15 @@ namespace ts.Completions {
21652165
if (cls && !findChildOfKind(cls, SyntaxKind.CloseBraceToken, sourceFile)) {
21662166
return cls;
21672167
}
2168+
break;
2169+
case SyntaxKind.Identifier: // class c extends React.Component { a: () => 1\n compon| }
2170+
if (isFromObjectTypeDeclaration(location)) {
2171+
return findAncestor(location, isObjectTypeDeclaration);
2172+
}
21682173
}
21692174

21702175
if (!contextToken) return undefined;
2176+
21712177
switch (contextToken.kind) {
21722178
case SyntaxKind.SemicolonToken: // class c {getValue(): number; | }
21732179
case SyntaxKind.CloseBraceToken: // class c { method() { } | }
@@ -2179,7 +2185,13 @@ namespace ts.Completions {
21792185
case SyntaxKind.CommaToken: // class c {getValue(): number, | }
21802186
return tryCast(contextToken.parent, isObjectTypeDeclaration);
21812187
default:
2182-
if (!isFromObjectTypeDeclaration(contextToken)) return undefined;
2188+
if (!isFromObjectTypeDeclaration(contextToken)) {
2189+
// class c extends React.Component { a: () => 1\n| }
2190+
if (getLineAndCharacterOfPosition(sourceFile, contextToken.getEnd()).line !== getLineAndCharacterOfPosition(sourceFile, position).line && isObjectTypeDeclaration(location)) {
2191+
return location;
2192+
}
2193+
return undefined;
2194+
}
21832195
const isValidKeyword = isClassLike(contextToken.parent.parent) ? isClassMemberCompletionKeyword : isInterfaceOrTypeLiteralCompletionKeyword;
21842196
return (isValidKeyword(contextToken.kind) || contextToken.kind === SyntaxKind.AsteriskToken || isIdentifier(contextToken) && isValidKeyword(stringToToken(contextToken.text)!)) // TODO: GH#18217
21852197
? contextToken.parent.parent as ObjectTypeDeclaration : undefined;

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ namespace ts {
892892
}
893893

894894
export interface CompletionInfo {
895-
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */
895+
/** Not true for all global completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */
896896
isGlobalCompletion: boolean;
897897
isMemberCompletion: boolean;
898898

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
//// class Parent {
4+
//// protected shouldWork() {
5+
//// console.log();
6+
//// }
7+
//// }
8+
////
9+
//// class Child extends Parent {
10+
//// // this assumes ASI, but on next line wants to
11+
//// x = () => 1
12+
//// shoul/*insideid*/
13+
//// }
14+
////
15+
//// class ChildTwo extends Parent {
16+
//// // this assumes ASI, but on next line wants to
17+
//// x = () => 1
18+
//// /*root*/ //nothing
19+
//// }
20+
21+
verify.completions({ marker: ["insideid", "root"], includes: "shouldWork", isNewIdentifierLocation: true });

0 commit comments

Comments
 (0)