Skip to content

Commit 09ea4d2

Browse files
committed
fix: Object literal completions remove original
fixes #145
1 parent a306b1a commit 09ea4d2

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

typescript/src/completions/objectLiteralCompletions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default (prior: ts.CompletionInfo): ts.CompletionEntry[] | void => {
99
const enableMoreVariants = c('objectLiteralCompletions.moreVariants')
1010
const keepOriginal = c('objectLiteralCompletions.keepOriginal')
1111
if (!preferences.includeCompletionsWithObjectLiteralMethodSnippets && !enableMoreVariants) return
12-
// plans to make it hihgly configurable! e.g. if user wants to make some subtype leading (e.g. from [] | {})
12+
// plans to make it highly configurable! e.g. if user wants to make some subtype leading (e.g. from [] | {})
1313
if (ts.isIdentifier(node)) node = node.parent
1414
if (ts.isShorthandPropertyAssignment(node)) node = node.parent
1515
const nextChar = node.getSourceFile().getFullText()[position]
@@ -22,8 +22,8 @@ export default (prior: ts.CompletionInfo): ts.CompletionEntry[] | void => {
2222
if (!objType) return
2323
oldProperties = getAllPropertiesOfType(objType, typeChecker)
2424
}
25-
const clonedEntries = [...entries]
26-
for (const entry of clonedEntries) {
25+
const nonObjectLiteralEntries = entries.filter(entry => !isObjectLiteralMethodSnippet(entry))
26+
for (const entry of nonObjectLiteralEntries) {
2727
let type: ts.Type | undefined
2828
if (!isTs5()) {
2929
const property = oldProperties!.find(property => property.name === entry.name)

typescript/test/completions.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,29 @@ test('Object Literal Completions', () => {
712712
expect(pos4.filter(x => x.insertText?.includes(': '))).toEqual([])
713713
})
714714

715+
test.only('Object Literal Completions with keepOriginal: remove & builtin method snippets', () => {
716+
overrideSettings({
717+
'objectLiteralCompletions.keepOriginal': 'remove',
718+
})
719+
const { completion } = fourslashLikeTester(/* ts */ `
720+
interface Options {
721+
a: {}
722+
onA()
723+
}
724+
const options: Options = {
725+
/*1*/
726+
}
727+
`)
728+
completion(1, {
729+
exact: {
730+
insertTexts: ['a: {\n\t$1\n},$0', 'onA() {\n $0\n},'],
731+
all: {
732+
isSnippet: true,
733+
},
734+
},
735+
})
736+
})
737+
715738
test('Extract to type / interface name inference', () => {
716739
fourslashLikeTester(/* ts */ `
717740
const foo: { bar: string; } = { bar: 'baz' }

typescript/test/testing.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { defaultConfigFunc, entrypoint, sharedLanguageService, settingsOverride,
55

66
interface CompletionPartMatcher {
77
names?: string[]
8-
all?: Pick<ts.CompletionEntry, 'kind' | 'isSnippet'>
8+
insertTexts?: string[]
9+
all?: Partial<Pick<ts.CompletionEntry, 'kind' | 'isSnippet'>>
910
}
1011

1112
interface CompletionMatcher {
@@ -31,6 +32,10 @@ export const getCompletionsAtPosition = (pos: number, { fileName = entrypoint, s
3132
pos,
3233
{
3334
includeCompletionsWithInsertText: true,
35+
includeCompletionsWithObjectLiteralMethodSnippets: true,
36+
includeCompletionsWithSnippetText: true,
37+
includeCompletionsWithClassMemberSnippets: true,
38+
useLabelDetailsInCompletionEntries: true,
3439
},
3540
defaultConfigFunc,
3641
languageService,
@@ -85,10 +90,16 @@ export const fourslashLikeTester = (contents: string, fileName = entrypoint) =>
8590
const message = ` at marker ${mark}`
8691
const { exact, includes, excludes } = matcher
8792
if (exact) {
88-
const { names, all } = exact
93+
const { names, all, insertTexts } = exact
8994
if (names) {
9095
expect(result?.entryNames, message).toEqual(names)
9196
}
97+
if (insertTexts) {
98+
expect(
99+
result.entries.map(entry => entry.insertText),
100+
message,
101+
).toEqual(insertTexts)
102+
}
92103
if (all) {
93104
for (const entry of result.entries) {
94105
expect(entry, entry.name + message).toContain(all)

0 commit comments

Comments
 (0)