Skip to content

Support incomplete identifier in JSX initializer completions (#21681) #21692

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
1 commit merged into from
Feb 6, 2018
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
4 changes: 4 additions & 0 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3166,6 +3166,9 @@ Actual: ${stringify(fullActual)}`);
assert.equal(item.hasAction, hasAction, "hasAction");
assert.equal(item.isRecommended, options && options.isRecommended, "isRecommended");
assert.equal(item.insertText, options && options.insertText, "insertText");
if (options && options.replacementSpan) { // TODO: GH#21679
assert.deepEqual(item.replacementSpan, options && options.replacementSpan && textSpanFromRange(options.replacementSpan), "replacementSpan");
}
}

private findFile(indexOrName: string | number) {
Expand Down Expand Up @@ -4622,6 +4625,7 @@ namespace FourSlashInterface {
sourceDisplay: string;
isRecommended?: true;
insertText?: string;
replacementSpan?: FourSlash.Range;
}

export interface NewContentOptions {
Expand Down
25 changes: 19 additions & 6 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace ts.Completions {
origin: SymbolOriginInfo | undefined,
recommendedCompletion: Symbol | undefined,
propertyAccessToConvert: PropertyAccessExpression | undefined,
isJsxInitializer: boolean,
isJsxInitializer: IsJsxInitializer,
includeInsertTextCompletions: boolean,
): CompletionEntry | undefined {
const info = getCompletionEntryDisplayNameForSymbol(symbol, target, origin, kind);
Expand All @@ -193,6 +193,9 @@ namespace ts.Completions {
if (isJsxInitializer) {
if (insertText === undefined) insertText = name;
insertText = `{${insertText}}`;
if (typeof isJsxInitializer !== "boolean") {
replacementSpan = createTextSpanFromNode(isJsxInitializer, sourceFile);
}
}
}

Expand Down Expand Up @@ -250,7 +253,7 @@ namespace ts.Completions {
kind: CompletionKind,
includeInsertTextCompletions?: boolean,
propertyAccessToConvert?: PropertyAccessExpression | undefined,
isJsxInitializer?: boolean,
isJsxInitializer?: IsJsxInitializer,
recommendedCompletion?: Symbol,
symbolToOriginInfoMap?: SymbolOriginInfoMap,
): Map<true> {
Expand Down Expand Up @@ -499,7 +502,7 @@ namespace ts.Completions {
location: Node;
symbolToOriginInfoMap: SymbolOriginInfoMap;
previousToken: Node;
readonly isJsxInitializer: boolean;
readonly isJsxInitializer: IsJsxInitializer;
}
function getSymbolCompletionFromEntryId(
typeChecker: TypeChecker,
Expand Down Expand Up @@ -683,6 +686,8 @@ namespace ts.Completions {
}

const enum CompletionDataKind { Data, JsDocTagName, JsDocTag, JsDocParameterName }
/** true: after the `=` sign but no identifier has been typed yet. Else is the Identifier after the initializer. */
type IsJsxInitializer = boolean | Identifier;
interface CompletionData {
readonly kind: CompletionDataKind.Data;
readonly symbols: ReadonlyArray<Symbol>;
Expand All @@ -695,7 +700,7 @@ namespace ts.Completions {
readonly symbolToOriginInfoMap: SymbolOriginInfoMap;
readonly recommendedCompletion: Symbol | undefined;
readonly previousToken: Node | undefined;
readonly isJsxInitializer: boolean;
readonly isJsxInitializer: IsJsxInitializer;
}
type Request = { readonly kind: CompletionDataKind.JsDocTagName | CompletionDataKind.JsDocTag } | { readonly kind: CompletionDataKind.JsDocParameterName, tag: JSDocParameterTag };

Expand Down Expand Up @@ -877,7 +882,7 @@ namespace ts.Completions {
let isRightOfDot = false;
let isRightOfOpenTag = false;
let isStartingCloseTag = false;
let isJsxInitializer = false;
let isJsxInitializer: IsJsxInitializer = false;

let location = getTouchingPropertyName(sourceFile, position, insideJsDocTagTypeExpression); // TODO: GH#15853
if (contextToken) {
Expand Down Expand Up @@ -938,7 +943,15 @@ namespace ts.Completions {
break;

case SyntaxKind.JsxAttribute:
isJsxInitializer = previousToken.kind === SyntaxKind.EqualsToken;
switch (previousToken.kind) {
case SyntaxKind.EqualsToken:
isJsxInitializer = true;
break;
case SyntaxKind.Identifier:
if (previousToken !== (parent as JsxAttribute).name) {
isJsxInitializer = previousToken as Identifier;
}
}
break;
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/fourslash/completionsJsxAttributeInitializer2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.tsx
////const foo = 0;
////<div x=[|f/**/|] />;

const [replacementSpan] = test.ranges();
goTo.marker();
verify.completionListContains("foo", "const foo: 0", undefined, "const", undefined, undefined, {
includeInsertTextCompletions: true,
insertText: "{foo}",
replacementSpan,
});
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ declare namespace FourSlashInterface {
sourceDisplay?: string,
isRecommended?: true,
insertText?: string,
replacementSpan?: Range,
},
): void;
completionListItemsCountIsGreaterThan(count: number): void;
Expand Down