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/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { readFile, writeFile } from 'node:fs/promises';
import type { KeyBinding } from '../packages/cli/src/config/keyBindings.js';
import {
commandCategories,
commandDescriptions,
defaultKeyBindings,
} from '../packages/cli/src/config/keyBindings.js';
import {
formatWithPrettier,
injectBetweenMarkers,
normalizeForCompare,
} from './utils/autogen.js';
const START_MARKER = '<!-- KEYBINDINGS-AUTOGEN:START -->';
const END_MARKER = '<!-- KEYBINDINGS-AUTOGEN:END -->';
const OUTPUT_RELATIVE_PATH = ['docs', 'cli', 'keyboard-shortcuts.md'];
const KEY_NAME_OVERRIDES: Record<string, string> = {
return: 'Enter',
escape: 'Esc',
tab: 'Tab',
backspace: 'Backspace',
delete: 'Delete',
up: 'Up Arrow',
down: 'Down Arrow',
left: 'Left Arrow',
right: 'Right Arrow',
home: 'Home',
end: 'End',
pageup: 'Page Up',
pagedown: 'Page Down',
clear: 'Clear',
insert: 'Insert',
f1: 'F1',
f2: 'F2',
f3: 'F3',
f4: 'F4',
f5: 'F5',
f6: 'F6',
f7: 'F7',
f8: 'F8',
f9: 'F9',
f10: 'F10',
f11: 'F11',
f12: 'F12',
};
export interface KeybindingDocCommand {
description: string;
bindings: readonly KeyBinding[];
}
export interface KeybindingDocSection {
title: string;
commands: readonly KeybindingDocCommand[];
}
export async function main(argv = process.argv.slice(2)) {
const checkOnly = argv.includes('--check');
const repoRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'..',
);
const docPath = path.join(repoRoot, ...OUTPUT_RELATIVE_PATH);
const sections = buildDefaultDocSections();
const generatedBlock = renderDocumentation(sections);
const currentDoc = await readFile(docPath, 'utf8');
const injectedDoc = injectBetweenMarkers({
document: currentDoc,
startMarker: START_MARKER,
endMarker: END_MARKER,
newContent: generatedBlock,
paddingBefore: '\n\n',
paddingAfter: '\n',
});
const updatedDoc = await formatWithPrettier(injectedDoc, docPath);
if (normalizeForCompare(updatedDoc) === normalizeForCompare(currentDoc)) {
if (!checkOnly) {
console.log('Keybinding documentation already up to date.');
}
return;
}
if (checkOnly) {
console.error(
'Keybinding documentation is out of date. Run `npm run docs:keybindings` to regenerate.',
);
process.exitCode = 1;
return;
}
await writeFile(docPath, updatedDoc, 'utf8');
console.log('Keybinding documentation regenerated.');
}
export function buildDefaultDocSections(): readonly KeybindingDocSection[] {
return commandCategories.map((category) => ({
title: category.title,
commands: category.commands.map((command) => ({
description: commandDescriptions[command],
bindings: defaultKeyBindings[command],
})),
}));
}
export function renderDocumentation(
sections: readonly KeybindingDocSection[],
): string {
const renderedSections = sections.map((section) => {
const rows = section.commands.map((command) => {
const formattedBindings = formatBindings(command.bindings);
const keysCell = formattedBindings.join('<br />');
return `| ${command.description} | ${keysCell} |`;
});
return [
`#### ${section.title}`,
'',
'| Action | Keys |',
'| --- | --- |',
...rows,
].join('\n');
});
return renderedSections.join('\n\n');
}
function formatBindings(bindings: readonly KeyBinding[]): string[] {
const seen = new Set<string>();
const results: string[] = [];
for (const binding of bindings) {
const label = formatBinding(binding);
if (label && !seen.has(label)) {
seen.add(label);
results.push(label);
}
}
return results;
}
function formatBinding(binding: KeyBinding): string {
const modifiers: string[] = [];
if (binding.ctrl) modifiers.push('Ctrl');
if (binding.command) modifiers.push('Cmd');
if (binding.shift) modifiers.push('Shift');
const keyName = formatKeyName(binding.key);
if (!keyName) {
return '';
}
const segments = [...modifiers, keyName].filter(Boolean);
let combo = segments.join(' + ');
const restrictions: string[] = [];
if (binding.ctrl === false) restrictions.push('no Ctrl');
if (binding.shift === false) restrictions.push('no Shift');
if (binding.command === false) restrictions.push('no Cmd');
if (restrictions.length > 0) {
combo = `${combo} (${restrictions.join(', ')})`;
}
return combo ? `\`${combo}\`` : '';
}
function formatKeyName(key: string): string {
const normalized = key.toLowerCase();
if (KEY_NAME_OVERRIDES[normalized]) {
return KEY_NAME_OVERRIDES[normalized];
}
return key.length === 1 ? key.toUpperCase() : key;
}
if (process.argv[1]) {
const entryUrl = pathToFileURL(path.resolve(process.argv[1])).href;
if (entryUrl === import.meta.url) {
await main();
}
}