File size: 6,269 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | /**
* Zod schema for keybindings.json configuration.
* Used for validation and JSON schema generation.
*/
import { z } from 'zod/v4'
import { lazySchema } from '../utils/lazySchema.js'
/**
* Valid context names where keybindings can be applied.
*/
export const KEYBINDING_CONTEXTS = [
'Global',
'Chat',
'Autocomplete',
'Confirmation',
'Help',
'Transcript',
'HistorySearch',
'Task',
'ThemePicker',
'Settings',
'Tabs',
// New contexts for keybindings migration
'Attachments',
'Footer',
'MessageSelector',
'DiffDialog',
'ModelPicker',
'Select',
'Plugin',
] as const
/**
* Human-readable descriptions for each keybinding context.
*/
export const KEYBINDING_CONTEXT_DESCRIPTIONS: Record<
(typeof KEYBINDING_CONTEXTS)[number],
string
> = {
Global: 'Active everywhere, regardless of focus',
Chat: 'When the chat input is focused',
Autocomplete: 'When autocomplete menu is visible',
Confirmation: 'When a confirmation/permission dialog is shown',
Help: 'When the help overlay is open',
Transcript: 'When viewing the transcript',
HistorySearch: 'When searching command history (ctrl+r)',
Task: 'When a task/agent is running in the foreground',
ThemePicker: 'When the theme picker is open',
Settings: 'When the settings menu is open',
Tabs: 'When tab navigation is active',
Attachments: 'When navigating image attachments in a select dialog',
Footer: 'When footer indicators are focused',
MessageSelector: 'When the message selector (rewind) is open',
DiffDialog: 'When the diff dialog is open',
ModelPicker: 'When the model picker is open',
Select: 'When a select/list component is focused',
Plugin: 'When the plugin dialog is open',
}
/**
* All valid keybinding action identifiers.
*/
export const KEYBINDING_ACTIONS = [
// App-level actions (Global context)
'app:interrupt',
'app:exit',
'app:toggleTodos',
'app:toggleTranscript',
'app:toggleBrief',
'app:toggleTeammatePreview',
'app:toggleTerminal',
'app:redraw',
'app:globalSearch',
'app:quickOpen',
// History navigation
'history:search',
'history:previous',
'history:next',
// Chat input actions
'chat:cancel',
'chat:killAgents',
'chat:cycleMode',
'chat:modelPicker',
'chat:fastMode',
'chat:thinkingToggle',
'chat:submit',
'chat:newline',
'chat:undo',
'chat:externalEditor',
'chat:stash',
'chat:imagePaste',
'chat:messageActions',
// Autocomplete menu actions
'autocomplete:accept',
'autocomplete:dismiss',
'autocomplete:previous',
'autocomplete:next',
// Confirmation dialog actions
'confirm:yes',
'confirm:no',
'confirm:previous',
'confirm:next',
'confirm:nextField',
'confirm:previousField',
'confirm:cycleMode',
'confirm:toggle',
'confirm:toggleExplanation',
// Tabs navigation actions
'tabs:next',
'tabs:previous',
// Transcript viewer actions
'transcript:toggleShowAll',
'transcript:exit',
// History search actions
'historySearch:next',
'historySearch:accept',
'historySearch:cancel',
'historySearch:execute',
// Task/agent actions
'task:background',
// Theme picker actions
'theme:toggleSyntaxHighlighting',
// Help menu actions
'help:dismiss',
// Attachment navigation (select dialog image attachments)
'attachments:next',
'attachments:previous',
'attachments:remove',
'attachments:exit',
// Footer indicator actions
'footer:up',
'footer:down',
'footer:next',
'footer:previous',
'footer:openSelected',
'footer:clearSelection',
'footer:close',
// Message selector (rewind) actions
'messageSelector:up',
'messageSelector:down',
'messageSelector:top',
'messageSelector:bottom',
'messageSelector:select',
// Diff dialog actions
'diff:dismiss',
'diff:previousSource',
'diff:nextSource',
'diff:back',
'diff:viewDetails',
'diff:previousFile',
'diff:nextFile',
// Model picker actions (ant-only)
'modelPicker:decreaseEffort',
'modelPicker:increaseEffort',
// Select component actions (distinct from confirm: to avoid collisions)
'select:next',
'select:previous',
'select:accept',
'select:cancel',
// Plugin dialog actions
'plugin:toggle',
'plugin:install',
// Permission dialog actions
'permission:toggleDebug',
// Settings config panel actions
'settings:search',
'settings:retry',
'settings:close',
// Voice actions
'voice:pushToTalk',
] as const
/**
* Schema for a single keybinding block.
*/
export const KeybindingBlockSchema = lazySchema(() =>
z
.object({
context: z
.enum(KEYBINDING_CONTEXTS)
.describe(
'UI context where these bindings apply. Global bindings work everywhere.',
),
bindings: z
.record(
z
.string()
.describe('Keystroke pattern (e.g., "ctrl+k", "shift+tab")'),
z
.union([
z.enum(KEYBINDING_ACTIONS),
z
.string()
.regex(/^command:[a-zA-Z0-9:\-_]+$/)
.describe(
'Command binding (e.g., "command:help", "command:compact"). Executes the slash command as if typed.',
),
z.null().describe('Set to null to unbind a default shortcut'),
])
.describe(
'Action to trigger, command to invoke, or null to unbind',
),
)
.describe('Map of keystroke patterns to actions'),
})
.describe('A block of keybindings for a specific context'),
)
/**
* Schema for the entire keybindings.json file.
* Uses object wrapper format with optional $schema and $docs metadata.
*/
export const KeybindingsSchema = lazySchema(() =>
z
.object({
$schema: z
.string()
.optional()
.describe('JSON Schema URL for editor validation'),
$docs: z.string().optional().describe('Documentation URL'),
bindings: z
.array(KeybindingBlockSchema())
.describe('Array of keybinding blocks by context'),
})
.describe(
'Claude Code keybindings configuration. Customize keyboard shortcuts by context.',
),
)
/**
* TypeScript types derived from the schema.
*/
export type KeybindingsSchemaType = z.infer<
ReturnType<typeof KeybindingsSchema>
>
|