|
| 1 | +import { matchParents, buildNotStrictStringCompletion } from '../utils' |
| 2 | +import { sharedCompletionContext } from './sharedContext' |
| 3 | + |
| 4 | +export default (): ts.CompletionEntry[] | void => { |
| 5 | + const { node, program } = sharedCompletionContext |
| 6 | + if (!node) return |
| 7 | + const comparisonNode = getComparisonNode(node) |
| 8 | + if (!comparisonNode) return |
| 9 | + if (ts.isPropertyAccessExpression(comparisonNode) && ts.isIdentifier(comparisonNode.name) && comparisonNode.name.text === 'code') { |
| 10 | + const typeChecker = program.getTypeChecker() |
| 11 | + const symbol = typeChecker.getSymbolAtLocation(comparisonNode.name) |
| 12 | + const decl = symbol?.declarations?.[0] |
| 13 | + if (!decl) return |
| 14 | + if ( |
| 15 | + decl.getSourceFile().fileName.endsWith('node_modules/typescript/lib/lib.dom.d.ts') && |
| 16 | + matchParents(decl.parent, ['InterfaceDeclaration'])?.name.text === 'KeyboardEvent' |
| 17 | + ) { |
| 18 | + return allKeyCodes.map(keyCode => buildNotStrictStringCompletion(node as ts.StringLiteralLike, keyCode)) |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// type: https://github.com/zardoy/contro-max/blob/master/src/types/keyCodes.ts |
| 24 | +const singleNumber = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 25 | +const fNumbers = [...singleNumber.filter(x => x !== 0), 10, 11, 12].map(x => `F${x}`) // actually can go up to 24, but used rarely |
| 26 | +const letterKeys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'].map( |
| 27 | + x => `Key${x.toUpperCase()}`, |
| 28 | +) |
| 29 | +const someOtherKeys = [ |
| 30 | + 'Space', |
| 31 | + 'Esc', |
| 32 | + 'Tab', |
| 33 | + 'Enter', |
| 34 | + 'Equal', |
| 35 | + 'Minus', |
| 36 | + 'Backslash', |
| 37 | + 'Slash', |
| 38 | + 'Period', |
| 39 | + 'Comma', |
| 40 | + 'Capslock', |
| 41 | + 'Numlock', |
| 42 | + 'PrintScreen', |
| 43 | + 'Scrolllock', |
| 44 | + 'Pause', |
| 45 | + 'Backspace', |
| 46 | + 'Delete', |
| 47 | + 'Insert', |
| 48 | + 'Backquote', |
| 49 | + 'BracketLeft', |
| 50 | + 'BracketRight', |
| 51 | + ...['Up', 'Down', 'Left', 'Right'].map(x => `Arrow${x}`), |
| 52 | + 'Home', |
| 53 | + 'End', |
| 54 | + 'PageUp', |
| 55 | + 'PageDown', |
| 56 | +] |
| 57 | + |
| 58 | +const digitKeys = singleNumber.map(x => `Digit${x}`) |
| 59 | +const modifierOnlyKeys = ['Meta', 'Control', 'Alt', 'Shift'].flatMap(x => ['', 'Left', 'Right'].map(j => x + j)) |
| 60 | + |
| 61 | +const numpadKeys = [...singleNumber, 'Divide', 'Multiply', 'Subtract', 'Add', 'Enter', 'Decimal'].map(x => `Numpad${x}`) |
| 62 | +const allKeyCodes = [...digitKeys, ...letterKeys, ...fNumbers, ...someOtherKeys, ...modifierOnlyKeys, ...numpadKeys] |
| 63 | + |
| 64 | +const getComparisonNode = (node: ts.Node) => { |
| 65 | + if (!ts.isStringLiteralLike(node)) return |
| 66 | + const binaryExpr = matchParents(node.parent, ['BinaryExpression']) |
| 67 | + return binaryExpr?.right === node && |
| 68 | + [ts.SyntaxKind.EqualsEqualsEqualsToken, ts.SyntaxKind.ExclamationEqualsEqualsToken].includes(binaryExpr.operatorToken.kind) |
| 69 | + ? binaryExpr.left |
| 70 | + : matchParents(node.parent, ['CaseClause', 'CaseBlock', 'SwitchStatement'])?.expression |
| 71 | +} |
0 commit comments