|
1 |
| -import type tslib from 'typescript/lib/tsserverlibrary' |
| 1 | +import { oneOf } from '@zardoy/utils' |
| 2 | +import { groupBy, partition } from 'rambda' |
2 | 3 |
|
3 |
| -export default ( |
4 |
| - position: number, |
5 |
| - node: tslib.Node | undefined, |
6 |
| - scriptSnapshot: tslib.IScriptSnapshot, |
7 |
| - sourceFile: tslib.SourceFile, |
8 |
| - program: tslib.Program, |
9 |
| - ts: typeof tslib, |
10 |
| -) => { |
| 4 | +export default (entries: ts.CompletionEntry[], node: ts.Node | undefined, sourceFile: ts.SourceFile, program: ts.Program) => { |
11 | 5 | if (!node) return
|
12 |
| - // TO BE DONE HERE |
13 |
| - // const typeChecker = program.getTypeChecker() |
14 |
| - // const type = typeChecker.getTypeAtLocation(node) |
15 |
| - // console.log(typeChecker.getAugmentedPropertiesOfType(type).map(({ name }) => name)) |
| 6 | + // if (ts.isObjectLiteralExpression(node) && ts.isCallExpression(node.parent)) { |
| 7 | + // const typeChecker = program.getTypeChecker() |
| 8 | + // const type = typeChecker.getTypeAtLocation(node.parent) |
| 9 | + // const callSignatures = type.getCallSignatures() |
| 10 | + // } |
| 11 | + if (ts.isIdentifier(node)) node = node.parent |
| 12 | + if (!ts.isPropertyAccessExpression(node)) return |
| 13 | + const typeChecker = program.getTypeChecker() |
| 14 | + const expr = node.expression |
| 15 | + const type = typeChecker.getTypeAtLocation(expr) |
| 16 | + const sourceProps = type.getProperties?.()?.map(({ name }) => name) |
| 17 | + // languageService.getSignatureHelpItems(fileName, position, {})) |
| 18 | + if (!sourceProps) return |
| 19 | + // const entriesBySortText = groupBy(({ sortText }) => sortText, entries) |
| 20 | + const [interestedEntries, notInterestedEntries] = partition( |
| 21 | + entry => oneOf(entry.kind, ts.ScriptElementKind.memberVariableElement, ts.ScriptElementKind.memberFunctionElement), |
| 22 | + entries, |
| 23 | + ) |
| 24 | + // 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)) |
| 27 | + // make sorted |
| 28 | + sortableEntries |
| 29 | + .sort((a, b) => { |
| 30 | + return sourceProps.indexOf(a.name) - sourceProps.indexOf(b.name) |
| 31 | + }) |
| 32 | + .map((entry, i) => ({ ...entry, sortText: String(lowestSortText + i) })) |
| 33 | + return [...notSortableEntries, ...sortableEntries, ...notInterestedEntries] |
16 | 34 | }
|
0 commit comments