-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add completions from the 'this' type #21231
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,7 +167,6 @@ namespace ts.Completions { | |
return undefined; | ||
} | ||
const { name, needsConvertPropertyAccess } = info; | ||
Debug.assert(!(needsConvertPropertyAccess && !propertyAccessToConvert)); | ||
if (needsConvertPropertyAccess && !includeInsertTextCompletions) { | ||
return undefined; | ||
} | ||
|
@@ -186,14 +185,24 @@ namespace ts.Completions { | |
kindModifiers: SymbolDisplay.getSymbolModifiers(symbol), | ||
sortText: "0", | ||
source: getSourceFromOrigin(origin), | ||
// TODO: GH#20619 Use configured quote style | ||
insertText: needsConvertPropertyAccess ? `["${name}"]` : undefined, | ||
replacementSpan: needsConvertPropertyAccess | ||
? createTextSpanFromBounds(findChildOfKind(propertyAccessToConvert, SyntaxKind.DotToken, sourceFile)!.getStart(sourceFile), propertyAccessToConvert.name.end) | ||
: undefined, | ||
hasAction: trueOrUndefined(needsConvertPropertyAccess || origin !== undefined), | ||
hasAction: trueOrUndefined(origin !== undefined), | ||
isRecommended: trueOrUndefined(isRecommendedCompletionMatch(symbol, recommendedCompletion, typeChecker)), | ||
...getInsertTextAndReplacementSpan(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we optimize this a bit instead of generating an extra object allocation on every entry. |
||
}; | ||
|
||
function getInsertTextAndReplacementSpan(): { insertText?: string, replacementSpan?: TextSpan } { | ||
if (kind === CompletionKind.Global) { | ||
if (typeChecker.isMemberSymbol(symbol)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems kinda weird that we add the symbol, then later on ask the checker if they were the ones we added.. i wounder if we can do something different here.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alas.. i could not come up with a better idea here myself. ignore my previous rant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah.. that is not bad.. |
||
return { insertText: needsConvertPropertyAccess ? `this["${name}"]` : `this.${name}` }; | ||
} | ||
} | ||
if (needsConvertPropertyAccess) { | ||
// TODO: GH#20619 Use configured quote style | ||
const replacementSpan = createTextSpanFromBounds(findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!.getStart(sourceFile), propertyAccessToConvert!.name.end); | ||
return { insertText: `["${name}"]`, replacementSpan }; | ||
} | ||
return {}; | ||
} | ||
} | ||
|
||
|
||
|
@@ -1097,6 +1106,15 @@ namespace ts.Completions { | |
const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias; | ||
|
||
symbols = typeChecker.getSymbolsInScope(scopeNode, symbolMeanings); | ||
|
||
// Need to insert 'this.' before properties of `this` type, so only do that if `includeInsertTextCompletions` | ||
if (options.includeInsertTextCompletions && scopeNode.kind !== SyntaxKind.SourceFile) { | ||
const thisType = typeChecker.tryGetThisTypeAt(scopeNode); | ||
if (thisType) { | ||
symbols.push(...getPropertiesForCompletion(thisType, typeChecker, /*isForAccess*/ true)); | ||
} | ||
} | ||
|
||
if (options.includeExternalModuleExports) { | ||
getSymbolsFromOtherSourceFileExports(symbols, previousToken && isIdentifier(previousToken) ? previousToken.text : "", target); | ||
} | ||
|
@@ -2052,13 +2070,13 @@ namespace ts.Completions { | |
if (isIdentifierText(name, target)) return validIdentiferResult; | ||
switch (kind) { | ||
case CompletionKind.None: | ||
case CompletionKind.Global: | ||
case CompletionKind.MemberLike: | ||
return undefined; | ||
case CompletionKind.ObjectPropertyDeclaration: | ||
// TODO: GH#18169 | ||
return { name: JSON.stringify(name), needsConvertPropertyAccess: false }; | ||
case CompletionKind.PropertyAccess: | ||
case CompletionKind.Global: | ||
// Don't add a completion for a name starting with a space. See https://github.com/Microsoft/TypeScript/pull/20547 | ||
return name.charCodeAt(0) === CharacterCodes.space ? undefined : { name, needsConvertPropertyAccess: true }; | ||
case CompletionKind.String: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////class C { | ||
//// "foo bar": number; | ||
//// xyz() { | ||
//// /**/ | ||
//// } | ||
////} | ||
//// | ||
////function f(this: { x: number }) { /*f*/ } | ||
|
||
goTo.marker(""); | ||
|
||
verify.completionListContains("xyz", "(method) C.xyz(): void", "", "method", undefined, undefined, { | ||
includeInsertTextCompletions: true, | ||
insertText: "this.xyz", | ||
}); | ||
|
||
verify.completionListContains("foo bar", '(property) C["foo bar"]: number', "", "property", undefined, undefined, { | ||
includeInsertTextCompletions: true, | ||
insertText: 'this["foo bar"]', | ||
}); | ||
|
||
goTo.marker("f"); | ||
|
||
verify.completionListContains("x", "(property) x: number", "", "property", undefined, undefined, { | ||
includeInsertTextCompletions: true, | ||
insertText: "this.x", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Asked @sandersn and @weswigham and they didn't know why these are properties, but if I try to change
undefined
to a variable I can't build the compiler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems that this should be in utilities instead on the checker. nothing here is about the checker internal state, but rather how we mark symbols.