Skip to content

Commit 4f30970

Browse files
author
Andy
authored
Separate isGlobalCompletion from CompletionKind (#22074)
* Separate isGlobalCompletion from CompletionKind * Fix comments
1 parent 66fa9f6 commit 4f30970

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

src/services/completions.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace ts.Completions {
106106
}
107107

108108
function completionInfoFromData(sourceFile: SourceFile, typeChecker: TypeChecker, compilerOptions: CompilerOptions, log: Log, completionData: CompletionData, includeInsertTextCompletions: boolean): CompletionInfo {
109-
const { symbols, completionKind, isNewIdentifierLocation, location, propertyAccessToConvert, keywordFilters, symbolToOriginInfoMap, recommendedCompletion, isJsxInitializer } = completionData;
109+
const { symbols, completionKind, isInSnippetScope, isNewIdentifierLocation, location, propertyAccessToConvert, keywordFilters, symbolToOriginInfoMap, recommendedCompletion, isJsxInitializer } = completionData;
110110

111111
if (sourceFile.languageVariant === LanguageVariant.JSX && location && location.parent && isJsxClosingElement(location.parent)) {
112112
// In the TypeScript JSX element, if such element is not defined. When users query for completion at closing tag,
@@ -148,7 +148,7 @@ namespace ts.Completions {
148148
addRange(entries, getKeywordCompletions(keywordFilters));
149149
}
150150

151-
return { isGlobalCompletion: completionKind === CompletionKind.Global, isMemberCompletion, isNewIdentifierLocation, entries };
151+
return { isGlobalCompletion: isInSnippetScope, isMemberCompletion, isNewIdentifierLocation, entries };
152152
}
153153

154154
function isMemberCompletionKind(kind: CompletionKind): boolean {
@@ -635,6 +635,7 @@ namespace ts.Completions {
635635
readonly kind: CompletionDataKind.Data;
636636
readonly symbols: ReadonlyArray<Symbol>;
637637
readonly completionKind: CompletionKind;
638+
readonly isInSnippetScope: boolean;
638639
/** Note that the presence of this alone doesn't mean that we need a conversion. Only do that if the completion is not an ordinary identifier. */
639640
readonly propertyAccessToConvert: PropertyAccessExpression | undefined;
640641
readonly isNewIdentifierLocation: boolean;
@@ -649,7 +650,6 @@ namespace ts.Completions {
649650

650651
const enum CompletionKind {
651652
ObjectPropertyDeclaration,
652-
/** Note that sometimes we access completions from global scope, but use "None" instead of this. See isGlobalCompletionScope. */
653653
Global,
654654
PropertyAccess,
655655
MemberLike,
@@ -753,6 +753,7 @@ namespace ts.Completions {
753753
log("getCompletionData: Is inside comment: " + (timestamp() - start));
754754

755755
let insideJsDocTagTypeExpression = false;
756+
let isInSnippetScope = false;
756757
if (insideComment) {
757758
if (hasDocComment(sourceFile, position)) {
758759
if (sourceFile.text.charCodeAt(position - 1) === CharacterCodes.at) {
@@ -971,7 +972,7 @@ namespace ts.Completions {
971972
log("getCompletionData: Semantic work: " + (timestamp() - semanticStart));
972973

973974
const recommendedCompletion = previousToken && getRecommendedCompletion(previousToken, position, sourceFile, typeChecker);
974-
return { kind: CompletionDataKind.Data, symbols, completionKind, propertyAccessToConvert, isNewIdentifierLocation, location, keywordFilters, symbolToOriginInfoMap, recommendedCompletion, previousToken, isJsxInitializer };
975+
return { kind: CompletionDataKind.Data, symbols, completionKind, isInSnippetScope, propertyAccessToConvert, isNewIdentifierLocation, location, keywordFilters, symbolToOriginInfoMap, recommendedCompletion, previousToken, isJsxInitializer };
975976

976977
type JSDocTagWithTypeExpression = JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag;
977978

@@ -1098,7 +1099,7 @@ namespace ts.Completions {
10981099
}
10991100

11001101
// Get all entities in the current scope.
1101-
completionKind = CompletionKind.None;
1102+
completionKind = CompletionKind.Global;
11021103
isNewIdentifierLocation = isNewIdentifierDefinitionLocation(contextToken);
11031104

11041105
if (previousToken !== contextToken) {
@@ -1134,9 +1135,7 @@ namespace ts.Completions {
11341135
position;
11351136

11361137
const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile;
1137-
if (isGlobalCompletionScope(scopeNode)) {
1138-
completionKind = CompletionKind.Global;
1139-
}
1138+
isInSnippetScope = isSnippetScope(scopeNode);
11401139

11411140
const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias;
11421141

@@ -1161,7 +1160,7 @@ namespace ts.Completions {
11611160
return true;
11621161
}
11631162

1164-
function isGlobalCompletionScope(scopeNode: Node): boolean {
1163+
function isSnippetScope(scopeNode: Node): boolean {
11651164
switch (scopeNode.kind) {
11661165
case SyntaxKind.SourceFile:
11671166
case SyntaxKind.TemplateExpression:
@@ -2128,10 +2127,10 @@ namespace ts.Completions {
21282127
// TODO: GH#18169
21292128
return { name: JSON.stringify(name), needsConvertPropertyAccess: false };
21302129
case CompletionKind.PropertyAccess:
2131-
case CompletionKind.None:
21322130
case CompletionKind.Global:
21332131
// Don't add a completion for a name starting with a space. See https://github.com/Microsoft/TypeScript/pull/20547
21342132
return name.charCodeAt(0) === CharacterCodes.space ? undefined : { name, needsConvertPropertyAccess: true };
2133+
case CompletionKind.None:
21352134
case CompletionKind.String:
21362135
return validIdentiferResult;
21372136
default:

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ namespace ts {
727727
}
728728

729729
export interface CompletionInfo {
730-
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isGlobalCompletionScope`. */
730+
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */
731731
isGlobalCompletion: boolean;
732732
isMemberCompletion: boolean;
733733

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4494,7 +4494,7 @@ declare namespace ts {
44944494
argumentCount: number;
44954495
}
44964496
interface CompletionInfo {
4497-
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isGlobalCompletionScope`. */
4497+
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */
44984498
isGlobalCompletion: boolean;
44994499
isMemberCompletion: boolean;
45004500
/**

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4746,7 +4746,7 @@ declare namespace ts {
47464746
argumentCount: number;
47474747
}
47484748
interface CompletionInfo {
4749-
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isGlobalCompletionScope`. */
4749+
/** Not true for all glboal completions. This will be true if the enclosing scope matches a few syntax kinds. See `isSnippetScope`. */
47504750
isGlobalCompletion: boolean;
47514751
isMemberCompletion: boolean;
47524752
/**

0 commit comments

Comments
 (0)