|
| 1 | +import { GetConfig } from '../types' |
| 2 | + |
| 3 | +export default ( |
| 4 | + entries: ts.CompletionEntry[], |
| 5 | + node: ts.Node, |
| 6 | + languageService: ts.LanguageService, |
| 7 | + preferences: ts.UserPreferences, |
| 8 | + c: GetConfig, |
| 9 | +): ts.CompletionEntry[] | void => { |
| 10 | + if (entries.length && node) { |
| 11 | + const enableMoreVariants = c('objectLiteralCompletions.moreVariants') |
| 12 | + const keepOriginal = c('objectLiteralCompletions.keepOriginal') |
| 13 | + if (!preferences.includeCompletionsWithObjectLiteralMethodSnippets && !enableMoreVariants) return |
| 14 | + // plans to make it hihgly configurable! e.g. if user wants to make some subtype leading (e.g. from [] | {}) |
| 15 | + if (ts.isIdentifier(node)) node = node.parent |
| 16 | + if (ts.isShorthandPropertyAssignment(node)) node = node.parent |
| 17 | + entries = [...entries] |
| 18 | + if (ts.isObjectLiteralExpression(node)) { |
| 19 | + const typeChecker = languageService.getProgram()!.getTypeChecker()! |
| 20 | + const objType = typeChecker.getContextualType(node) |
| 21 | + if (!objType) return |
| 22 | + const properties = objType.getProperties() |
| 23 | + for (const property of properties) { |
| 24 | + const entry = entries.find(({ name }) => name === property.name) |
| 25 | + if (!entry) return |
| 26 | + const type = typeChecker.getTypeOfSymbolAtLocation(property, node) |
| 27 | + if (!type) continue |
| 28 | + if (isMethodCompletionCall(type, typeChecker)) { |
| 29 | + if (['above', 'remove'].includes(keepOriginal) && preferences.includeCompletionsWithObjectLiteralMethodSnippets) { |
| 30 | + const methodEntryIndex = entries.findIndex(e => e.name === entry.name && isObjectLiteralMethodSnippet(e)) |
| 31 | + const methodEntry = entries[methodEntryIndex] |
| 32 | + if (methodEntry) { |
| 33 | + entries.splice(methodEntryIndex, 1) |
| 34 | + entries.splice(entries.indexOf(entry) + (keepOriginal === 'below' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, { |
| 35 | + ...methodEntry, |
| 36 | + // let correctSorting.enable sort it |
| 37 | + sortText: entry.sortText, |
| 38 | + }) |
| 39 | + } |
| 40 | + } |
| 41 | + continue |
| 42 | + } |
| 43 | + if (!enableMoreVariants) continue |
| 44 | + const getQuotedSnippet = (): [string, string] => { |
| 45 | + const quote = tsFull.getQuoteFromPreference(tsFull.getQuotePreference(node.getSourceFile() as any, preferences)) |
| 46 | + return [`: ${quote}$1${quote},$0`, `: ${quote}${quote},`] |
| 47 | + } |
| 48 | + const insertObjectArrayInnerText = c('objectLiteralCompletions.insertNewLine') ? '\n\t$1\n' : '$1' |
| 49 | + const completingStyleMap = [ |
| 50 | + [getQuotedSnippet, isStringCompletion], |
| 51 | + [[`: [${insertObjectArrayInnerText}],$0`, `: [],`], isArrayCompletion], |
| 52 | + [[`: {${insertObjectArrayInnerText}},$0`, `: {},`], isObjectCompletion], |
| 53 | + ] as const |
| 54 | + const fallbackSnippet = c('objectLiteralCompletions.fallbackVariant') ? ([': $0,', ': ,'] as const) : undefined |
| 55 | + const insertSnippetVariant = completingStyleMap.find(([, detector]) => detector(type, typeChecker))?.[0] ?? fallbackSnippet |
| 56 | + if (!insertSnippetVariant) continue |
| 57 | + const [insertSnippetText, insertSnippetPreview] = typeof insertSnippetVariant === 'function' ? insertSnippetVariant() : insertSnippetVariant |
| 58 | + const insertText = entry.name + insertSnippetText |
| 59 | + const index = entries.indexOf(entry) |
| 60 | + entries.splice(index + (keepOriginal === 'below' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, { |
| 61 | + ...entry, |
| 62 | + // todo setting incompatible!!! |
| 63 | + sortText: entry.sortText, |
| 64 | + labelDetails: { |
| 65 | + detail: insertSnippetPreview, |
| 66 | + }, |
| 67 | + insertText, |
| 68 | + isSnippet: true, |
| 69 | + }) |
| 70 | + } |
| 71 | + return entries |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +const isObjectLiteralMethodSnippet = (entry: ts.CompletionEntry) => { |
| 77 | + const { detail } = entry.labelDetails ?? {} |
| 78 | + return detail?.startsWith('(') && detail.split('\n')[0]!.trimEnd().endsWith(')') |
| 79 | +} |
| 80 | + |
| 81 | +const isMethodCompletionCall = (type: ts.Type, checker: ts.TypeChecker) => { |
| 82 | + if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) return true |
| 83 | + if (type.isUnion()) return type.types.some(type => isMethodCompletionCall(type, checker)) |
| 84 | +} |
| 85 | + |
| 86 | +const isStringCompletion = (type: ts.Type) => { |
| 87 | + if (type.flags & ts.TypeFlags.Undefined) return true |
| 88 | + if (type.flags & ts.TypeFlags.StringLike) return true |
| 89 | + if (type.isUnion()) return type.types.every(type => isStringCompletion(type)) |
| 90 | + return false |
| 91 | +} |
| 92 | + |
| 93 | +const isArrayCompletion = (type: ts.Type, checker: ts.TypeChecker) => { |
| 94 | + if (type.flags & ts.TypeFlags.Any) return false |
| 95 | + if (type.flags & ts.TypeFlags.Undefined) return true |
| 96 | + if (checker['isArrayLikeType'](type)) return true |
| 97 | + if (type.isUnion()) return type.types.every(type => isArrayCompletion(type, checker)) |
| 98 | + return false |
| 99 | +} |
| 100 | + |
| 101 | +const isObjectCompletion = (type: ts.Type) => { |
| 102 | + if (type.flags & ts.TypeFlags.Undefined) return true |
| 103 | + if (type.flags & ts.TypeFlags.Object) return true |
| 104 | + if (type.isUnion()) return type.types.every(type => isObjectCompletion(type)) |
| 105 | + return false |
| 106 | +} |
0 commit comments