|
| 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 | + const addEntries: ts.CompletionEntry[] = [] |
| 18 | + const completionIndexesToRemove: number[] = [] |
| 19 | + entries = [...entries] |
| 20 | + if (ts.isObjectLiteralExpression(node)) { |
| 21 | + const typeChecker = languageService.getProgram()!.getTypeChecker()! |
| 22 | + const objType = typeChecker.getContextualType(node) |
| 23 | + if (!objType) return |
| 24 | + const properties = objType.getProperties() |
| 25 | + for (const property of properties) { |
| 26 | + const entry = entries.find(({ name }) => name === property.name) |
| 27 | + if (!entry) return |
| 28 | + const type = typeChecker.getTypeOfSymbolAtLocation(property, node) |
| 29 | + if (!type) continue |
| 30 | + if (isMethodCompletionCall(type, typeChecker)) { |
| 31 | + if (keepOriginal === 'remove') completionIndexesToRemove.push(entries.indexOf(entry)) |
| 32 | + continue |
| 33 | + } |
| 34 | + if (!enableMoreVariants) continue |
| 35 | + const getQuotedSnippet = (): [string, string] => { |
| 36 | + const quote = tsFull.getQuoteFromPreference(tsFull.getQuotePreference(node.getSourceFile() as any, preferences)) |
| 37 | + return [`: ${quote}$1${quote},$0`, `: ${quote}${quote},`] |
| 38 | + } |
| 39 | + const insertObjectArrayInnerText = c('objectLiteralCompletions.insertNewLine') ? '\n\t$1\n' : '$1' |
| 40 | + const completingStyleMap = [ |
| 41 | + [getQuotedSnippet, isStringCompletion], |
| 42 | + [[`: [${insertObjectArrayInnerText}],$0`, `: [],`], isArrayCompletion], |
| 43 | + [[`: {${insertObjectArrayInnerText}},$0`, `: {}`], isObjectCompletion], |
| 44 | + ] as const |
| 45 | + const insertSnippetVariant = completingStyleMap.find(([, detector]) => detector(type, typeChecker))?.[0] |
| 46 | + if (!insertSnippetVariant) continue |
| 47 | + const [insertSnippetText, insertSnippetPreview] = typeof insertSnippetVariant === 'function' ? insertSnippetVariant() : insertSnippetVariant |
| 48 | + const insertText = entry.name + insertSnippetText |
| 49 | + addEntries.push({ |
| 50 | + ...entry, |
| 51 | + // todo setting incompatible!!! |
| 52 | + sortText: entry.sortText, |
| 53 | + labelDetails: { |
| 54 | + detail: insertSnippetPreview, |
| 55 | + }, |
| 56 | + insertText, |
| 57 | + isSnippet: true, |
| 58 | + }) |
| 59 | + if (keepOriginal === 'remove') entries.splice(entries.indexOf(entry), 1) |
| 60 | + } |
| 61 | + if ((keepOriginal === 'above' || keepOriginal === 'remove') && preferences.includeCompletionsWithObjectLiteralMethodSnippets) { |
| 62 | + const metMethodCompletions: string[] = [] |
| 63 | + entries = entries.filter((entry, i) => { |
| 64 | + if (completionIndexesToRemove.includes(i)) return false |
| 65 | + |
| 66 | + const { detail } = entry.labelDetails ?? {} |
| 67 | + if (detail?.startsWith('(') && detail.split('\n')[0]!.trimEnd().endsWith(')')) { |
| 68 | + addEntries.push(entry) |
| 69 | + metMethodCompletions.push(entry.name) |
| 70 | + return false |
| 71 | + } |
| 72 | + if ( |
| 73 | + keepOriginal === 'remove' && |
| 74 | + entry.kind === ts.ScriptElementKind.memberFunctionElement && |
| 75 | + !detail && |
| 76 | + metMethodCompletions.includes(entry.name) |
| 77 | + ) { |
| 78 | + return false |
| 79 | + } |
| 80 | + return true |
| 81 | + }) |
| 82 | + } |
| 83 | + return keepOriginal === 'above' ? [...addEntries, ...entries] : [...entries, ...addEntries] |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const isMethodCompletionCall = (type: ts.Type, checker: ts.TypeChecker) => { |
| 89 | + if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) return true |
| 90 | + if (type.isUnion()) return type.types.some(type => isMethodCompletionCall(type, checker)) |
| 91 | +} |
| 92 | + |
| 93 | +const isStringCompletion = (type: ts.Type) => { |
| 94 | + if (type.flags & ts.TypeFlags.Undefined) return true |
| 95 | + if (type.isStringLiteral()) return true |
| 96 | + if (type.isUnion()) return type.types.every(type => isStringCompletion(type)) |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +const isArrayCompletion = (type: ts.Type, checker: ts.TypeChecker) => { |
| 101 | + if (type.flags & ts.TypeFlags.Undefined) return true |
| 102 | + if (checker['isArrayLikeType'](type)) return true |
| 103 | + if (type.isUnion()) return type.types.every(type => isArrayCompletion(type, checker)) |
| 104 | + return false |
| 105 | +} |
| 106 | + |
| 107 | +const isObjectCompletion = (type: ts.Type) => { |
| 108 | + if (type.flags & ts.TypeFlags.Undefined) return true |
| 109 | + if (type.flags & ts.TypeFlags.Object) return true |
| 110 | + if (type.isUnion()) return type.types.every(type => isObjectCompletion(type)) |
| 111 | + return false |
| 112 | +} |
0 commit comments