Visual Studio Code, rebuilt in Rust
Rust
#!/usr/bin/env bun
// Dumps the command titles the reference's Keyboard Shortcuts editor and Command Palette read: MenuRegistry.getCommands()
// (title, category, metadata description, precondition; extension commands left out, the port reads them from package.json), the labels and
// preconditions of EditorExtensionsRegistry.getEditorActions(), the MenuId.CommandPalette items with their `when`, and the
// CommandsRegistry ids with `metadata.args`. Palette items carry `titleKey`, `categoryKey` and `descriptionKey` (`module#key` of
// the localize call that made the text, from a ref-nls-clone.ts clone) for the display language (src/nls.rs); `nlsKeys` gives the
// same keys for the menu bar's items and the views' titles.
// Usage: port/tools/ref-commands [out.json] (env REF as port/tools/ref-screenshot). Run on the Studio.
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { taggedClone, untagSource } from './ref-nls-clone.ts';
const out = process.argv[2] ?? 'native/crates/app/src/command_palette/reference-commands.json';
// contextkey.ts `getContextKeyInfo`: RawContextKey.all() sorted by key, the context key descriptions keybindings.json completes.
const contextKeysOut = process.argv[3] ?? 'native/crates/app/src/contextkey/reference-context-keys.json';
const ref = process.env.REF ?? join(process.env.HOME!, 'Developer/vscode-ref-1.148.0');
const electron = join(ref, '.build/electron/Code - OSS.app/Contents/MacOS/Code - OSS');
const port = 9338;
await takeGuiLock();
const clone = taggedClone(ref);
const userData = mkdtempSync(join(tmpdir(), 'ref-commands-'));
mkdirSync(join(userData, 'User'));
writeFileSync(join(userData, 'User/settings.json'), JSON.stringify({
'workbench.startupEditor': 'none',
'window.restoreWindows': 'none',
'update.mode': 'none',
'chat.disableAIFeatures': true,
'security.workspace.trust.enabled': false,
}));
const app = Bun.spawn([electron, clone, '--user-data-dir', userData, `--remote-debugging-port=${port}`, '--disable-extension', 'GitHub.copilot-chat', '--disable-extension', 'vscode.vscode-api-tests'], {
env: { ...process.env, VSCODE_DEV: '1', NODE_ENV: 'development' },
stdout: 'ignore',
stderr: 'ignore',
});
try {
const cdp = await connect();
await until(async () => (await evaluate(cdp, `!!document.querySelector('.monaco-workbench .part.statusbar')`)) === true);
await Bun.sleep(5000);
// The dev workbench loads each module from out/; importing the same URL gives the instance the workbench filled.
const dump = await evaluate(cdp, `(async () => {
const out = location.href.slice(0, location.href.indexOf('/out/') + '/out'.length);
const load = path => import(out + path);
const { MenuRegistry, MenuId, isIMenuItem } = await load('/vs/platform/actions/common/actions.js');
const { EditorExtensionsRegistry } = await load('/vs/editor/browser/editorExtensions.js');
const { RawContextKey } = await load('/vs/platform/contextkey/common/contextkey.js');
const { CommandsRegistry } = await load('/vs/platform/commands/common/commands.js');
${untagSource}
const text = value => typeof value === 'string' ? value : value?.value;
const key = value => untag(text(value)).key;
const label = command => command.category ? text(command.category) + ': ' + text(command.title) : text(command.title);
// KeybindingsEditor.getActionsLabels, then toKeybindingEntry's MenuRegistry.getCommand first.
const labels = {};
for (const action of EditorExtensionsRegistry.getEditorActions()) {
labels[action.id] = action.label;
}
const commandPalette = [];
for (const item of MenuRegistry.getMenuItems(MenuId.CommandPalette)) {
if (!isIMenuItem(item) || item.command.source) {
continue;
}
labels[item.command.id] = label(item.command);
commandPalette.push({ id: item.command.id, title: text(item.command.title), titleKey: key(item.command.title), category: text(item.command.category), categoryKey: key(item.command.category), description: text(item.command.metadata?.description), descriptionKey: key(item.command.metadata?.description), when: item.when?.serialize(), precondition: item.command.precondition?.serialize() });
}
// menusExtensionPoint.ts reports an extension command whose id MenuRegistry.getCommand already knows.
const menuCommands = [];
for (const [id, command] of MenuRegistry.getCommands()) {
if (!command.source) {
labels[id] = label(command);
menuCommands.push(id);
}
}
menuCommands.sort();
// unboundCommands.ts leaves registered commands with metadata.args out of the unbound ones.
const commandsWithArgs = [...CommandsRegistry.getCommands()].filter(([, command]) => command.metadata?.args?.length).map(([id]) => id).sort();
const contextKeys = [...RawContextKey.all()].sort((a, b) => a.key.localeCompare(b.key));
// Per menu bar menu (but the main menu, whose labels electron-main/menubar.ts makes), view and view container: the
// text of its items or titles (menubarControl.ts: mnemonicTitle, else value) to the key of the call that made it.
const nlsKeys = {};
const addTitle = (scope, title) => {
for (const value of [typeof title === 'string' ? title : title?.mnemonicTitle, text(title)]) {
const { text: label, key } = untag(value);
if (key) {
(nlsKeys[scope] ??= {})[label] = key;
}
}
};
for (const [id, menuId] of MenuId._instances) {
if (!id.startsWith('Menubar') || menuId === MenuId.MenubarMainMenu) {
continue;
}
for (const item of MenuRegistry.getMenuItems(menuId)) {
addTitle(id, isIMenuItem(item) ? item.command.title : item.title);
addTitle(id, isIMenuItem(item) ? item.command.toggled?.title : undefined);
}
}
const { Registry } = await load('/vs/platform/registry/common/platform.js');
const views = await load('/vs/workbench/common/views.js');
const viewsRegistry = Registry.as(views.Extensions.ViewsRegistry);
// viewsExtensionPoint.ts warns of views for a container it does not know and of an id already registered.
const viewContainers = [];
const viewIds = [];
for (const container of Registry.as(views.Extensions.ViewContainersRegistry).all) {
addTitle(container.id, container.title);
if (!container.extensionId) {
viewContainers.push(container.id);
}
for (const view of viewsRegistry.getViews(container)) {
if (!view.extensionId) {
viewIds.push(view.id);
}
addTitle(view.id, view.name);
addTitle(view.id, view.containerTitle);
addTitle(view.id, view.singleViewPaneContainerTitle);
}
}
return { labels, commandPalette, commandsWithArgs, menuCommands, viewContainers: viewContainers.sort(), viewIds: viewIds.sort(), nlsKeys, contextKeys };
})()`);
const { contextKeys, ...commands } = JSON.parse(JSON.stringify(dump).replace(/\uE000[\uE001-\uF000]{2}/g, ''));
writeFileSync(out, JSON.stringify(commands, null, '\t') + '\n');
writeFileSync(contextKeysOut, JSON.stringify(contextKeys, null, '\t') + '\n');
console.log(`${contextKeys.length} context keys: ${contextKeysOut}`);
console.log(`${Object.keys(dump.labels).length} labels, ${dump.commandPalette.length} palette items: ${out}`);
} finally {
app.kill('SIGKILL');
await app.exited;
rmSync(userData, { recursive: true, force: true });
rmSync(join(clone, '..'), { recursive: true, force: true });
}
process.exit(0);
async function evaluate(cdp: (method: string, params?: object) => Promise<any>, expression: string) {
const result = await cdp('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true });
if (result.exceptionDetails) {
throw new Error(result.exceptionDetails.exception?.description ?? result.exceptionDetails.text);
}
return result.result.value;
}
async function connect() {
const target = await until(async () => (await (await fetch(`http://127.0.0.1:${port}/json/list`)).json() as { type: string; url: string; webSocketDebuggerUrl: string }[]).find(t => t.type === 'page' && t.url.includes('workbench')));
const socket = new WebSocket(target.webSocketDebuggerUrl);
await new Promise(resolve => socket.addEventListener('open', resolve, { once: true }));
let nextId = 1;
const pending = new Map<number, (message: any) => void>();
socket.addEventListener('message', event => {
const message = JSON.parse(String(event.data));
pending.get(message.id)?.(message);
pending.delete(message.id);
});
return (method: string, params: object = {}) => new Promise<any>((resolve, reject) => {
const id = nextId++;
pending.set(id, message => message.error ? reject(new Error(`${method}: ${message.error.message}`)) : resolve(message.result));
socket.send(JSON.stringify({ id, method, params }));
});
}
/** gui-lock.sh's lock: windows of parallel sessions take turns. */
async function takeGuiLock() {
// port/tools/gui-lane holds it for this run.
if (process.env.PORT_GUI_LOCK_HELD) {
return;
}
const lock = '/tmp/port-gui.lock';
while (true) {
try {
mkdirSync(lock);
writeFileSync(join(lock, 'pid'), String(process.pid));
process.on('exit', () => rmSync(lock, { recursive: true, force: true }));
return;
} catch {
const owner = Number(await Bun.file(join(lock, 'pid')).text().catch(() => '0'));
if (owner && !isAlive(owner)) {
rmSync(lock, { recursive: true, force: true });
continue;
}
await Bun.sleep(5000);
}
}
}
function isAlive(pid: number) {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
async function until<T>(probe: () => Promise<T>, timeoutMs = 60000): Promise<NonNullable<T>> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const value = await probe().catch(() => undefined);
if (value) {
return value;
}
await Bun.sleep(250);
}
throw new Error('timed out');
}