|
| 1 | +import { oneOf } from '@zardoy/utils' |
| 2 | +import constructMethodSnippet from '../constructMethodSnippet' |
| 3 | +import { insertTextAfterEntry } from '../utils' |
| 4 | +import { sharedCompletionContext } from './sharedContext' |
| 5 | + |
| 6 | +export default (entries: ts.CompletionEntry[]) => { |
| 7 | + const { languageService, c, sourceFile, position } = sharedCompletionContext |
| 8 | + |
| 9 | + const methodSnippetInsertTextMode = c('methodSnippetsInsertText') |
| 10 | + const enableResolvingInsertText = c('enableMethodSnippets') && methodSnippetInsertTextMode !== 'disable' |
| 11 | + const changeKindToFunction = c('experiments.changeKindToFunction') |
| 12 | + |
| 13 | + if (!enableResolvingInsertText && !changeKindToFunction) return |
| 14 | + |
| 15 | + const typeChecker = languageService.getProgram()!.getTypeChecker()! |
| 16 | + // let timeSpend = 0 |
| 17 | + const newEntries = entries.map(entry => { |
| 18 | + const patch = (): ts.CompletionEntry | undefined => { |
| 19 | + const { kind, symbol } = entry |
| 20 | + if ( |
| 21 | + !enableResolvingInsertText && |
| 22 | + !oneOf( |
| 23 | + kind, |
| 24 | + ts.ScriptElementKind.alias, |
| 25 | + ts.ScriptElementKind.memberVariableElement, |
| 26 | + ts.ScriptElementKind.variableElement, |
| 27 | + ts.ScriptElementKind.localVariableElement, |
| 28 | + ts.ScriptElementKind.constElement, |
| 29 | + ts.ScriptElementKind.variableElement, |
| 30 | + ) |
| 31 | + ) { |
| 32 | + return |
| 33 | + } |
| 34 | + if (methodSnippetInsertTextMode === 'only-local' && entry.source) return |
| 35 | + if (!symbol) return |
| 36 | + const { valueDeclaration } = symbol |
| 37 | + if (!valueDeclaration) return |
| 38 | + |
| 39 | + // const dateNow = Date.now() |
| 40 | + if (enableResolvingInsertText) { |
| 41 | + const resolveData = {} as { isAmbiguous: boolean } |
| 42 | + const methodSnippet = constructMethodSnippet(languageService, sourceFile, position, symbol, c, resolveData) |
| 43 | + if (!methodSnippet || resolveData.isAmbiguous) return |
| 44 | + return { |
| 45 | + ...entry, |
| 46 | + insertText: insertTextAfterEntry(entry, `(${methodSnippet.map((x, i) => `$\{${i + 1}:${x}}`).join(', ')})`), |
| 47 | + labelDetails: { |
| 48 | + detail: `(${methodSnippet.join(', ')})`, |
| 49 | + description: ts.displayPartsToString(entry.sourceDisplay), |
| 50 | + }, |
| 51 | + kind: changeKindToFunction ? ts.ScriptElementKind.functionElement : entry.kind, |
| 52 | + isSnippet: true, |
| 53 | + } |
| 54 | + } |
| 55 | + const type = typeChecker.getTypeOfSymbolAtLocation(symbol, valueDeclaration) |
| 56 | + const signatures = typeChecker.getSignaturesOfType(type, ts.SignatureKind.Call) |
| 57 | + // timeSpend += Date.now() - dateNow |
| 58 | + if (signatures.length === 0) return |
| 59 | + |
| 60 | + return { ...entry, kind: ts.ScriptElementKind.functionElement } |
| 61 | + } |
| 62 | + |
| 63 | + return patch() ?? entry |
| 64 | + }) |
| 65 | + |
| 66 | + // remove logging once stable |
| 67 | + // console.log('changeKindToFunction time:', timeSpend) |
| 68 | + |
| 69 | + return newEntries |
| 70 | +} |
0 commit comments