Visual Studio Code, rebuilt in Rust
Rust
#!/usr/bin/env bun
// Writes a default keybindings table (native/crates/app/src/keybindings/default-<os>.json) from the reference's
// `--export-default-keybindings` folder: the core rows (up to `diffEditor.exitCompareMove`, the last core rule; the
// built-in extensions' rows after it come from their package.json), the exporting profile's keybindings.json URI frozen as
// in default-macos.json, and each command's `weight` from default-macos.json (rows registered above the extension weights).
// Usage, on the Studio:
// "$REF/.build/electron/Code - OSS.app/Contents/MacOS/Code - OSS" "$REF" --user-data-dir <tmp> --export-default-keybindings <dir>
// port/tools/default-keybindings <dir> win native/crates/app/src/keybindings/default-win.json
// With `osx` it reproduces default-macos.json byte for byte.
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
const [folder, os, out] = process.argv.slice(2);
if (!folder || !['osx', 'win', 'linux'].includes(os) || !out) throw new Error('usage: default-keybindings <export dir> <osx|win|linux> <out.json>');
const text = readFileSync(join(folder, `doc.keybindings.${os}.json`), 'utf8');
// The export is the rows array, then the unbound commands as `//` comments.
const rows = JSON.parse(text.slice(0, text.indexOf('\n]') + 2).replace(/^\s*\/\/.*$/gm, ''));
const core = rows.slice(0, rows.findIndex((row: { command: string }) => row.command === 'diffEditor.exitCompareMove') + 1);
if (core.length === 0) throw new Error('no diffEditor.exitCompareMove row: the last core rule moved, update this tool');
const macos = JSON.parse(readFileSync(new URL('../../native/crates/app/src/keybindings/default-macos.json', import.meta.url), 'utf8'));
const weights = new Map<string, number>();
for (const row of macos) if (row.weight) weights.set(row.command, row.weight);
const lines = core.map((rule: { key: string; command: string; when?: string; args?: unknown }) => {
const row: Record<string, unknown> = { key: rule.key, command: rule.command };
if (rule.when) row.when = rule.when.replace(/vscode-userdata:[^']*\/User\/keybindings\.json/, 'vscode-userdata:/tmp/m9-kb-ud/User/keybindings.json');
if (rule.args !== undefined) row.args = rule.args;
if (weights.has(rule.command)) row.weight = weights.get(rule.command);
return JSON.stringify(row);
});
writeFileSync(out, `[\n${lines.join(',\n')}\n]\n`);
console.log(`${out}: ${lines.length} rows`);