|
| 1 | +import { oneOf } from '@zardoy/utils' |
| 2 | +import { sharedCompletionContext } from './sharedContext' |
| 3 | + |
| 4 | +export default (entries: ts.CompletionEntry[]) => { |
| 5 | + const { languageService } = sharedCompletionContext |
| 6 | + |
| 7 | + const typeChecker = languageService.getProgram()!.getTypeChecker()! |
| 8 | + let timeSpend = 0 |
| 9 | + const newEntries = entries.map(entry => { |
| 10 | + const patch = (): ts.CompletionEntry | undefined => { |
| 11 | + const { kind } = entry |
| 12 | + if ( |
| 13 | + !oneOf( |
| 14 | + kind, |
| 15 | + ts.ScriptElementKind.alias, |
| 16 | + ts.ScriptElementKind.memberVariableElement, |
| 17 | + ts.ScriptElementKind.variableElement, |
| 18 | + ts.ScriptElementKind.localVariableElement, |
| 19 | + ts.ScriptElementKind.constElement, |
| 20 | + ts.ScriptElementKind.variableElement, |
| 21 | + ) |
| 22 | + ) { |
| 23 | + return |
| 24 | + } |
| 25 | + const symbol = entry['symbol'] as ts.Symbol | undefined |
| 26 | + if (!symbol) return |
| 27 | + const { valueDeclaration } = symbol |
| 28 | + if (!valueDeclaration) return |
| 29 | + |
| 30 | + const dateNow = Date.now() |
| 31 | + const type = typeChecker.getTypeOfSymbolAtLocation(symbol, valueDeclaration) |
| 32 | + const signatures = typeChecker.getSignaturesOfType(type, ts.SignatureKind.Call) |
| 33 | + timeSpend += Date.now() - dateNow |
| 34 | + if (signatures.length === 0) return |
| 35 | + |
| 36 | + return { ...entry, kind: ts.ScriptElementKind.functionElement } |
| 37 | + } |
| 38 | + |
| 39 | + return patch() ?? entry |
| 40 | + }) |
| 41 | + |
| 42 | + // remove logging once stable |
| 43 | + console.log('changeKindToFunction time:', timeSpend) |
| 44 | + |
| 45 | + return newEntries |
| 46 | +} |
0 commit comments