import type { KeybindingCommand, KeybindingsConfig } from "@t3tools/contracts"; export interface ShortcutEventLike { key: string; metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean; } export interface ShortcutMatchContext { terminalFocus: boolean; terminalOpen: boolean; [key: string]: boolean; } export type ResolvedKeybindings = KeybindingsConfig; interface ParsedShortcut { key: string; metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean; modKey: boolean; } interface ShortcutMatchOptions { platform?: string; context?: Partial; } type WhenNode = | { type: "identifier"; name: string } | { type: "not"; node: WhenNode } | { type: "and"; left: WhenNode; right: WhenNode } | { type: "or"; left: WhenNode; right: WhenNode }; type WhenToken = | { type: "identifier"; value: string } | { type: "not" } | { type: "and" } | { type: "or" } | { type: "lparen" } | { type: "rparen" }; const WHEN_AST_CACHE = new Map(); const MAX_WHEN_AST_CACHE_ENTRIES = 512; function isMacPlatform(platform: string): boolean { return /mac|iphone|ipad|ipod/i.test(platform); } function normalizeKeyToken(token: string): string { if (token === "space") return " "; if (token === "esc") return "escape"; return token; } function parseShortcutValue(value: string): ParsedShortcut | null { const rawTokens = value.toLowerCase().split("+").map((token) => token.trim()); const tokens = [...rawTokens]; let trailingEmptyCount = 0; while (tokens[tokens.length - 1] === "") { trailingEmptyCount += 1; tokens.pop(); } if (trailingEmptyCount > 0) { tokens.push("+"); } if (tokens.some((token) => token.length === 0)) { return null; } if (tokens.length === 0) return null; let key: string | null = null; let metaKey = false; let ctrlKey = false; let shiftKey = false; let altKey = false; let modKey = false; for (const token of tokens) { switch (token) { case "cmd": case "meta": metaKey = true; break; case "ctrl": case "control": ctrlKey = true; break; case "shift": shiftKey = true; break; case "alt": case "option": altKey = true; break; case "mod": modKey = true; break; default: { if (key !== null) { return null; } key = normalizeKeyToken(token); } } } if (key === null) return null; return { key, metaKey, ctrlKey, shiftKey, altKey, modKey, }; } function normalizeEventKey(key: string): string { const normalized = key.toLowerCase(); if (normalized === "esc") return "escape"; return normalized; } function matchesShortcutValue( event: ShortcutEventLike, shortcutValue: string, platform = navigator.platform, ): boolean { const parsed = parseShortcutValue(shortcutValue); if (!parsed) return false; const key = normalizeEventKey(event.key); if (key !== parsed.key) return false; const useMetaForMod = isMacPlatform(platform); const expectedMeta = parsed.metaKey || (parsed.modKey && useMetaForMod); const expectedCtrl = parsed.ctrlKey || (parsed.modKey && !useMetaForMod); return ( event.metaKey === expectedMeta && event.ctrlKey === expectedCtrl && event.shiftKey === parsed.shiftKey && event.altKey === parsed.altKey ); } function tokenizeWhenExpression(expression: string): WhenToken[] | null { const tokens: WhenToken[] = []; let index = 0; while (index < expression.length) { const current = expression[index]; if (!current) break; if (/\s/.test(current)) { index += 1; continue; } if (expression.startsWith("&&", index)) { tokens.push({ type: "and" }); index += 2; continue; } if (expression.startsWith("||", index)) { tokens.push({ type: "or" }); index += 2; continue; } if (current === "!") { tokens.push({ type: "not" }); index += 1; continue; } if (current === "(") { tokens.push({ type: "lparen" }); index += 1; continue; } if (current === ")") { tokens.push({ type: "rparen" }); index += 1; continue; } const identifier = /^[A-Za-z_][A-Za-z0-9_.-]*/.exec(expression.slice(index)); if (!identifier) { return null; } tokens.push({ type: "identifier", value: identifier[0] }); index += identifier[0].length; } return tokens; } function parseWhenExpression(expression: string): WhenNode | null { const tokens = tokenizeWhenExpression(expression); if (!tokens || tokens.length === 0) return null; let index = 0; const parsePrimary = (): WhenNode | null => { const token = tokens[index]; if (!token) return null; if (token.type === "identifier") { index += 1; return { type: "identifier", name: token.value }; } if (token.type === "lparen") { index += 1; const expressionNode = parseOr(); const closeToken = tokens[index]; if (!expressionNode || !closeToken || closeToken.type !== "rparen") { return null; } index += 1; return expressionNode; } return null; }; const parseUnary = (): WhenNode | null => { const token = tokens[index]; if (token?.type === "not") { index += 1; const node = parseUnary(); if (!node) return null; return { type: "not", node }; } return parsePrimary(); }; const parseAnd = (): WhenNode | null => { let left = parseUnary(); if (!left) return null; while (tokens[index]?.type === "and") { index += 1; const right = parseUnary(); if (!right) return null; left = { type: "and", left, right }; } return left; }; const parseOr = (): WhenNode | null => { let left = parseAnd(); if (!left) return null; while (tokens[index]?.type === "or") { index += 1; const right = parseAnd(); if (!right) return null; left = { type: "or", left, right }; } return left; }; const ast = parseOr(); if (!ast || index !== tokens.length) return null; return ast; } function evaluateWhenNode(node: WhenNode, context: ShortcutMatchContext): boolean { switch (node.type) { case "identifier": if (node.name === "true") return true; if (node.name === "false") return false; return Boolean(context[node.name]); case "not": return !evaluateWhenNode(node.node, context); case "and": return evaluateWhenNode(node.left, context) && evaluateWhenNode(node.right, context); case "or": return evaluateWhenNode(node.left, context) || evaluateWhenNode(node.right, context); } } function matchesWhenExpression(when: string | undefined, context: ShortcutMatchContext): boolean { if (!when) return true; const normalized = when.trim(); if (normalized.length === 0) return true; if (!WHEN_AST_CACHE.has(normalized)) { if (WHEN_AST_CACHE.size >= MAX_WHEN_AST_CACHE_ENTRIES) { WHEN_AST_CACHE.clear(); } WHEN_AST_CACHE.set(normalized, parseWhenExpression(normalized)); } const ast = WHEN_AST_CACHE.get(normalized); if (!ast) return false; return evaluateWhenNode(ast, context); } function resolvePlatform(options: ShortcutMatchOptions | undefined): string { return options?.platform ?? navigator.platform; } function resolveContext(options: ShortcutMatchOptions | undefined): ShortcutMatchContext { return { terminalFocus: false, terminalOpen: false, ...options?.context, }; } function matchesCommandShortcut( event: ShortcutEventLike, keybindings: ResolvedKeybindings, command: KeybindingCommand, options?: ShortcutMatchOptions, ): boolean { return resolveShortcutCommand(event, keybindings, options) === command; } function resolveShortcutCommand( event: ShortcutEventLike, keybindings: ResolvedKeybindings, options?: ShortcutMatchOptions, ): KeybindingCommand | null { const platform = resolvePlatform(options); const context = resolveContext(options); for (let index = keybindings.length - 1; index >= 0; index -= 1) { const binding = keybindings[index]; if (!binding) continue; if (!matchesWhenExpression(binding.when, context)) continue; if (!matchesShortcutValue(event, binding.key, platform)) continue; return binding.command; } return null; } function formatShortcutKeyLabel(key: string): string { if (key === " ") return "Space"; if (key.length === 1) return key.toUpperCase(); if (key === "escape") return "Esc"; if (key === "arrowup") return "Up"; if (key === "arrowdown") return "Down"; if (key === "arrowleft") return "Left"; if (key === "arrowright") return "Right"; return key.slice(0, 1).toUpperCase() + key.slice(1); } export function formatShortcutLabel(shortcutValue: string, platform = navigator.platform): string { const parsed = parseShortcutValue(shortcutValue); if (!parsed) { return shortcutValue; } const keyLabel = formatShortcutKeyLabel(parsed.key); const useMetaForMod = isMacPlatform(platform); const showMeta = parsed.metaKey || (parsed.modKey && useMetaForMod); const showCtrl = parsed.ctrlKey || (parsed.modKey && !useMetaForMod); const showAlt = parsed.altKey; const showShift = parsed.shiftKey; if (useMetaForMod) { return `${showCtrl ? "\u2303" : ""}${showAlt ? "\u2325" : ""}${showShift ? "\u21e7" : ""}${showMeta ? "\u2318" : ""}${keyLabel}`; } const parts: string[] = []; if (showCtrl) parts.push("Ctrl"); if (showAlt) parts.push("Alt"); if (showShift) parts.push("Shift"); if (showMeta) parts.push("Meta"); parts.push(keyLabel); return parts.join("+"); } export function shortcutLabelForCommand( keybindings: ResolvedKeybindings, command: KeybindingCommand, platform = navigator.platform, ): string | null { for (let index = keybindings.length - 1; index >= 0; index -= 1) { const binding = keybindings[index]; if (!binding || binding.command !== command) continue; return formatShortcutLabel(binding.key, platform); } return null; } export function isTerminalToggleShortcut( event: ShortcutEventLike, keybindings: ResolvedKeybindings, options?: ShortcutMatchOptions, ): boolean { return matchesCommandShortcut(event, keybindings, "terminal.toggle", options); } export function isTerminalSplitShortcut( event: ShortcutEventLike, keybindings: ResolvedKeybindings, options?: ShortcutMatchOptions, ): boolean { return matchesCommandShortcut(event, keybindings, "terminal.split", options); } export function isTerminalNewShortcut( event: ShortcutEventLike, keybindings: ResolvedKeybindings, options?: ShortcutMatchOptions, ): boolean { return matchesCommandShortcut(event, keybindings, "terminal.new", options); } export function isChatNewShortcut( event: ShortcutEventLike, keybindings: ResolvedKeybindings, options?: ShortcutMatchOptions, ): boolean { return matchesCommandShortcut(event, keybindings, "chat.new", options); } export function isOpenFavoriteEditorShortcut( event: ShortcutEventLike, keybindings: ResolvedKeybindings, options?: ShortcutMatchOptions, ): boolean { return matchesCommandShortcut(event, keybindings, "editor.openFavorite", options); } export function isTerminalClearShortcut( event: ShortcutEventLike, platform = navigator.platform, ): boolean { const key = event.key.toLowerCase(); if (key === "l" && event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) { return true; } return ( isMacPlatform(platform) && key === "k" && event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey ); }