Skip to content

Commit 39c34e7

Browse files
committed
fix(regression): fix properties sorting feature in most cases
feat: Now locality bonus is used only for global completion, so it doesn't conflict with fix properties sorting feature and it seems to be more logical.
1 parent 7213157 commit 39c34e7

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

typescript/src/completions/fixPropertiesSorting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ export default (entries: ts.CompletionEntry[]) => {
6060
.sort((a, b) => {
6161
return getScore(a) - getScore(b)
6262
})
63-
.map((entry, i) => ({ ...entry /* sortText: String(lowestSortText + i) */ }))
63+
.map((entry, i) => ({ ...entry, sortText: String(Number.parseInt(entry.sortText, 10)) }))
6464
return [...notSortableEntries, ...sortedEntries, ...notInterestedEntries]
6565
}

typescript/src/completions/localityBonus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { sharedCompletionContext } from './sharedContext'
22

33
export default (entries: ts.CompletionEntry[]) => {
4-
const { node, sourceFile, c, position } = sharedCompletionContext
5-
if (!c('suggestions.localityBonus')) return
4+
const { node, sourceFile, c, position, prior } = sharedCompletionContext
5+
if (!c('suggestions.localityBonus') || prior.isMemberCompletion) return
66

77
if (!node) return
88
const LOWEST_SCORE = node.getSourceFile().getFullText().length

typescript/src/completions/sharedContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GetConfig } from '../types'
33

44
/** Must be used within functions */
55
export const sharedCompletionContext = {} as unknown as Readonly<{
6+
prior: ts.CompletionInfo
67
position: number
78
sourceFile: ts.SourceFile
89
program: ts.Program

typescript/src/completionsAtPosition.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,6 @@ export const getCompletionsAtPosition = (
7474
const exactNode = findChildContainingExactPosition(sourceFile, position)
7575
const isCheckedFile =
7676
!tsFull.isSourceFileJS(sourceFile as any) || !!tsFull.isCheckJsEnabledForFile(sourceFile as any, additionalData.compilerOptions as any)
77-
Object.assign(sharedCompletionContext, {
78-
position,
79-
languageService,
80-
sourceFile,
81-
program,
82-
isCheckedFile,
83-
node: exactNode,
84-
prevCompletionsMap,
85-
c,
86-
formatOptions: formatOptions || {},
87-
preferences: options || {},
88-
} satisfies typeof sharedCompletionContext)
8977
const unpatch = patchBuiltinMethods(c, languageService, isCheckedFile)
9078
const getPrior = () => {
9179
try {
@@ -156,6 +144,20 @@ export const getCompletionsAtPosition = (
156144

157145
if (!prior) return
158146

147+
Object.assign(sharedCompletionContext, {
148+
position,
149+
languageService,
150+
sourceFile,
151+
program,
152+
isCheckedFile,
153+
node: exactNode,
154+
prevCompletionsMap,
155+
c,
156+
formatOptions: formatOptions || {},
157+
preferences: options || {},
158+
prior,
159+
} satisfies typeof sharedCompletionContext)
160+
159161
if (c('tupleHelpSignature') && node) {
160162
const tupleSignature = getTupleSignature(node, program.getTypeChecker()!)
161163
if (tupleSignature) {

typescript/test/completions.spec.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ test('Fix properties sorting', () => {
471471
overrideSettings({
472472
fixSuggestionsSorting: true,
473473
})
474-
const tester = fourslashLikeTester(/* tsx */ `
474+
fourslashLikeTester(/* tsx */ `
475475
let a: {
476476
d
477477
b(a: {c, a}): {c, a}
@@ -486,22 +486,20 @@ test('Fix properties sorting', () => {
486486
487487
declare function MyComponent(props: { b?; c? } & { a? }): JSX.Element
488488
<MyComponent /*4*/ />;
489+
490+
let a: { b:{}, a() } = {
491+
/*5*/
492+
}
489493
`)
490-
tester.completion(1, {
491-
exact: {
492-
names: ['c', 'b'],
493-
},
494-
})
495-
tester.completion([2, 3], {
496-
exact: {
497-
names: ['c', 'b'],
498-
},
499-
})
500-
tester.completion(4, {
501-
exact: {
502-
names: ['b', 'c', 'a'],
503-
},
504-
})
494+
const assertSorted = (marker: number, expected: string[]) => {
495+
const { entriesSorted } = getCompletionsAtPosition(currentTestingContext.markers[marker]!)!
496+
expect(entriesSorted.map(x => x.name)).toEqual(expected)
497+
}
498+
assertSorted(1, ['c', 'b'])
499+
assertSorted(2, ['c', 'b'])
500+
assertSorted(3, ['c', 'b'])
501+
assertSorted(4, ['b', 'c', 'a'])
502+
assertSorted(5, ['b', 'b', 'a', 'a'])
505503
settingsOverride.fixSuggestionsSorting = false
506504
})
507505

0 commit comments

Comments
 (0)