Skip to content

Commit ec33dc3

Browse files
committed
[skip ci] fix: restore suggestions sorting in more locations
1 parent a3f755f commit ec33dc3

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

typescript/src/completions/fixPropertiesSorting.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@ export default (entries: ts.CompletionEntry[], node: ts.Node | undefined, source
88
// const type = typeChecker.getTypeAtLocation(node.parent)
99
// const callSignatures = type.getCallSignatures()
1010
// }
11-
if (ts.isIdentifier(node)) node = node.parent
12-
if (!ts.isPropertyAccessExpression(node)) return
11+
let rightNode: ts.Node | undefined
12+
const upperNode = ts.isIdentifier(node) ? node.parent : node
13+
if (ts.isPropertyAccessExpression(upperNode)) rightNode = upperNode.expression
14+
else if (ts.isObjectBindingPattern(node)) {
15+
if (ts.isVariableDeclaration(node.parent)) {
16+
const { initializer } = node.parent
17+
if (initializer) {
18+
if (ts.isIdentifier(initializer)) rightNode = initializer
19+
if (ts.isPropertyAccessExpression(initializer)) rightNode = initializer.name
20+
}
21+
}
22+
if (ts.isParameter(node.parent)) rightNode = node.parent.type
23+
} else if (ts.isObjectLiteralExpression(node) && ts.isReturnStatement(node.parent) && ts.isArrowFunction(node.parent.parent.parent)) {
24+
rightNode = node.parent.parent.parent.type
25+
}
26+
if (!rightNode) return
1327
const typeChecker = program.getTypeChecker()
14-
const expr = node.expression
15-
const type = typeChecker.getTypeAtLocation(expr)
28+
const type = typeChecker.getTypeAtLocation(rightNode)
1629
const sourceProps = type.getProperties?.()?.map(({ name }) => name)
1730
// languageService.getSignatureHelpItems(fileName, position, {}))
1831
if (!sourceProps) return
@@ -22,13 +35,13 @@ export default (entries: ts.CompletionEntry[], node: ts.Node | undefined, source
2235
entries,
2336
)
2437
// if sortText first symbol is not a number, than most probably it was highlighted by IntelliCode, keep them high
25-
const [sortableEntries, notSortableEntries] = partition(entry => !isNaN(+entry.sortText), interestedEntries)
26-
const lowestSortText = Math.min(...sortableEntries.map(({ sortText }) => +sortText))
38+
const [sortableEntries, notSortableEntries] = partition(entry => !isNaN(parseInt(entry.sortText)), interestedEntries)
39+
const lowestSortText = Math.min(...sortableEntries.map(({ sortText }) => parseInt(sortText)))
2740
// make sorted
28-
sortableEntries
41+
const sortedEntries = sortableEntries
2942
.sort((a, b) => {
3043
return sourceProps.indexOf(a.name) - sourceProps.indexOf(b.name)
3144
})
3245
.map((entry, i) => ({ ...entry, sortText: String(lowestSortText + i) }))
33-
return [...notSortableEntries, ...sortableEntries, ...notInterestedEntries]
46+
return [...notSortableEntries, ...sortedEntries, ...notInterestedEntries]
3447
}

0 commit comments

Comments
 (0)