Skip to content

Commit 3ab385f

Browse files
committed
Adressing pr comments
1 parent 586a2e5 commit 3ab385f

File tree

5 files changed

+53
-45
lines changed

5 files changed

+53
-45
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7720,5 +7720,9 @@
77207720
"Compiler option '{0}' cannot be given an empty string.": {
77217721
"category": "Error",
77227722
"code": 18051
7723+
},
7724+
"Add missing comma for an object member completion '{0}'.": {
7725+
"category": "Message",
7726+
"code": 18052
77237727
}
77247728
}

src/services/completions.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import {
5454
Expression,
5555
ExpressionWithTypeArguments,
5656
factory,
57-
FileTextChanges,
5857
filter,
5958
find,
6059
findAncestor,
@@ -294,7 +293,6 @@ import {
294293
Program,
295294
programContainsModules,
296295
PropertyAccessExpression,
297-
PropertyAssignment,
298296
PropertyDeclaration,
299297
PropertyName,
300298
PropertySignature,
@@ -360,6 +358,8 @@ import {
360358
UserPreferences,
361359
VariableDeclaration,
362360
walkUpParenthesizedExpressions,
361+
isPropertyAssignment,
362+
findNextToken,
363363
} from "./_namespaces/ts";
364364
import { StringCompletions } from "./_namespaces/ts.Completions";
365365

@@ -425,6 +425,8 @@ export enum CompletionSource {
425425
ObjectLiteralMethodSnippet = "ObjectLiteralMethodSnippet/",
426426
/** Case completions for switch statements */
427427
SwitchCases = "SwitchCases/",
428+
/** Completions for an Object literal expression */
429+
ObjectLiteralExpression = "ObjectLiteralExpression/",
428430
}
429431

430432
/** @internal */
@@ -1348,7 +1350,13 @@ function createCompletionEntry(
13481350
}
13491351
}
13501352

1351-
if ((origin?.kind === SymbolOriginInfoKind.TypeOnlyAlias) || (contextToken && contextToken.kind !== SyntaxKind.OpenBraceToken && completionKind === CompletionKind.ObjectPropertyDeclaration && preferences.includeCompletionsWithInsertText)) {
1353+
if ((origin?.kind === SymbolOriginInfoKind.TypeOnlyAlias)) {
1354+
hasAction = true;
1355+
}
1356+
1357+
if ((contextToken && isPropertyAssignment(contextToken.parent) && findNextToken(contextToken, contextToken?.parent, sourceFile)?.kind !== SyntaxKind.CommaToken &&
1358+
completionKind === CompletionKind.ObjectPropertyDeclaration)) {
1359+
source = CompletionSource.ObjectLiteralExpression;
13521360
hasAction = true;
13531361
}
13541362

@@ -2277,7 +2285,8 @@ function getSymbolCompletionFromEntryId(
22772285
return info && info.name === entryId.name && (
22782286
entryId.source === CompletionSource.ClassMemberSnippet && symbol.flags & SymbolFlags.ClassMember
22792287
|| entryId.source === CompletionSource.ObjectLiteralMethodSnippet && symbol.flags & (SymbolFlags.Property | SymbolFlags.Method)
2280-
|| getSourceFromOrigin(origin) === entryId.source)
2288+
|| getSourceFromOrigin(origin) === entryId.source
2289+
|| entryId.source === CompletionSource.ObjectLiteralExpression)
22812290
? { type: "symbol" as const, symbol, location, origin, contextToken, previousToken, isJsxInitializer, isTypeOnlyLocation }
22822291
: undefined;
22832292
}) || { type: "none" };
@@ -2466,24 +2475,16 @@ function getCompletionEntryCodeActionsAndSourceDisplay(
24662475
return { codeActions: [codeAction], sourceDisplay: undefined };
24672476
}
24682477

2469-
if (contextToken && isObjectLiteralExpression(contextToken.parent.parent) && previousToken?.kind !== SyntaxKind.ColonToken) {
2470-
let changes: FileTextChanges[];
2471-
if (getLineAndCharacterOfPosition(sourceFile, contextToken.getEnd()).line !== getLineAndCharacterOfPosition(sourceFile, position).line) {
2472-
changes = textChanges.ChangeTracker.with(
2473-
{ host, formatContext, preferences },
2474-
tracker=>tracker.replacePropertyAssignment(sourceFile, contextToken.parent as PropertyAssignment ,contextToken.parent as PropertyAssignment));
2475-
}
2476-
else {
2477-
changes = textChanges.ChangeTracker.with(
2478-
{ host, formatContext, preferences },
2479-
tracker=>tracker.replacePropertyAssignmentOnSameLine(sourceFile, contextToken.parent as PropertyAssignment ,contextToken.parent as PropertyAssignment));
2480-
}
2478+
if (source === CompletionSource.ObjectLiteralExpression && contextToken) {
2479+
const changes = textChanges.ChangeTracker.with(
2480+
{ host, formatContext, preferences },
2481+
tracker=>tracker.insertText(sourceFile, contextToken.end,","));
24812482
if (changes) {
24822483
return {
24832484
sourceDisplay: undefined,
24842485
codeActions: [{
24852486
changes,
2486-
description: diagnosticToString([Diagnostics.Includes_imports_of_types_referenced_by_0, name]),
2487+
description: diagnosticToString([Diagnostics.Add_missing_comma_for_an_object_member_completion_0, name]),
24872488
}],
24882489
};
24892490
}

tests/cases/fourslash/completionsObjectLiteralExpressions1.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@
1010
//// };
1111

1212
verify.completions({
13-
marker: "",
14-
includes: [
15-
{
16-
name: "secondary",
17-
sortText: completion.SortText.OptionalMember,
18-
hasAction: true,
19-
}],
20-
preferences: {
21-
allowIncompleteCompletions: true,
22-
includeInsertTextCompletions: true,
23-
},
13+
marker: "",
14+
includes: [{
15+
name: "secondary",
16+
sortText: completion.SortText.OptionalMember,
17+
hasAction: true,
18+
source: completion.CompletionSource.ObjectLiteralExpression,
19+
}],
20+
preferences: {
21+
allowIncompleteCompletions: true,
22+
includeInsertTextCompletions: true,
23+
},
2424
});
2525

2626
verify.applyCodeActionFromCompletion("", {
27-
name: "secondary",
28-
description: `Includes imports of types referenced by 'secondary'`,
29-
newFileContent:
27+
name: "secondary",
28+
description: `Add missing comma for an object member completion 'secondary'.`,
29+
source: completion.CompletionSource.ObjectLiteralExpression,
30+
newFileContent:
3031
`interface ColorPalette {
3132
primary?: string;
3233
secondary?: string;
@@ -35,8 +36,8 @@ let colors: ColorPalette = {
3536
primary: "red",
3637
3738
};`,
38-
preferences: {
39-
allowIncompleteCompletions: true,
40-
includeInsertTextCompletions: true,
41-
},
42-
});
39+
preferences: {
40+
allowIncompleteCompletions: true,
41+
includeInsertTextCompletions: true,
42+
},
43+
});

tests/cases/fourslash/completionsObjectLiteralExpressions2.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@
1414

1515
verify.completions({
1616
marker: "",
17-
includes: [
18-
{
19-
name: "secondary",
20-
sortText: completion.SortText.OptionalMember,
21-
hasAction: true,
22-
}],
17+
includes: [{
18+
name: "secondary",
19+
sortText: completion.SortText.OptionalMember,
20+
hasAction: true,
21+
source: completion.CompletionSource.ObjectLiteralExpression,
22+
}],
2323
preferences: {
24-
allowIncompleteCompletions: true,
25-
includeInsertTextCompletions: true,
24+
allowIncompleteCompletions: true,
25+
includeInsertTextCompletions: true,
2626
}
2727
});
2828

2929
verify.applyCodeActionFromCompletion("", {
3030
name: "secondary",
31-
description: `Includes imports of types referenced by 'secondary'`,
31+
description: `Add missing comma for an object member completion 'secondary'.`,
32+
source: completion.CompletionSource.ObjectLiteralExpression,
3233
newFileContent:
3334
`interface ColorPalette {
3435
primary?: string;

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ declare namespace completion {
899899
TypeOnlyAlias = "TypeOnlyAlias/",
900900
ObjectLiteralMethodSnippet = "ObjectLiteralMethodSnippet/",
901901
SwitchCases = "SwitchCases/",
902+
ObjectLiteralExpression = "ObjectLiteralExpression/",
902903
}
903904
export const globalThisEntry: Entry;
904905
export const undefinedVarEntry: Entry;

0 commit comments

Comments
 (0)