Visual Studio Code, rebuilt in Rust
Rust
#!/usr/bin/env bun
// Runs the reference with CDP and dumps the boxes and computed styles of workbench elements and their descendants,
// for parts a screenshot alone does not measure (toasts, the notification center, status bar entries, views).
// Usage: port/tools/ref-dom <out-dir> <action>... Actions: `dump:<selector>` (the element and its descendants),
// `eval:<expression>` (prints its JSON value), `target:<url part>` (the last page whose URL has it, such as an editor window; prints the pages),
// `click:<selector>`, `hover:<selector>`, `key:<key>` (CDP key name, e.g. Escape, with modifiers shift+alt+F12), `type:<text>`, `wait:<ms>`, `shot:<name>`.
// `colors:<name>` (every --vscode-* variable of the workbench as <name>.json), `radii:<name>` (every visible element
// with a border radius: box, radius, borders, background, overflow and shadow, as <name>.json).
// Env REF, FOLDER, FILE as port/tools/ref-screenshot, THEME (dark, light, hc, hclight; default dark), ARGS (more
// arguments, space separated, e.g. --extensionDevelopmentPath=<abs path>), SETTLE (ms before the first action, default 8000),
// SETTINGS (a JSON object of more user settings).
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { referenceArgs, referenceUserData } from './ref-user-data.ts';
const [outDir, ...actions] = process.argv.slice(2);
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 themes: Record<string, string> = {
dark: 'Default Dark Modern', light: 'Default Light Modern', hc: 'Default High Contrast', hclight: 'Default High Contrast Light',
abyss: 'Abyss', kimbie: 'Kimbie Dark', monokai: 'Monokai', dimmed: 'Monokai Dimmed', quiet: 'Quiet Light', red: 'Red',
sdark: 'Solarized Dark', slight: 'Solarized Light', tnb: 'Tomorrow Night Blue',
};
const port = 9338;
const keys: Record<string, { key: string; code: string; windowsVirtualKeyCode: number }> = {
Escape: { key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 },
Enter: { key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13 },
F2: { key: 'F2', code: 'F2', windowsVirtualKeyCode: 113 },
F12: { key: 'F12', code: 'F12', windowsVirtualKeyCode: 123 },
P: { key: 'p', code: 'KeyP', windowsVirtualKeyCode: 80 },
B: { key: 'b', code: 'KeyB', windowsVirtualKeyCode: 66 },
K: { key: 'k', code: 'KeyK', windowsVirtualKeyCode: 75 },
ArrowDown: { key: 'ArrowDown', code: 'ArrowDown', windowsVirtualKeyCode: 40 },
F: { key: 'f', code: 'KeyF', windowsVirtualKeyCode: 70 },
J: { key: 'j', code: 'KeyJ', windowsVirtualKeyCode: 74 },
Comma: { key: ',', code: 'Comma', windowsVirtualKeyCode: 188 },
Space: { key: ' ', code: 'Space', windowsVirtualKeyCode: 32 },
Tab: { key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 },
};
// CDP `modifiers` bits, for keys written as `shift+alt+F12`.
const modifierBits: Record<string, number> = { alt: 1, ctrl: 2, meta: 4, shift: 8 };
mkdirSync(outDir, { recursive: true });
const userData = referenceUserData(themes[process.env.THEME ?? 'dark'], { 'editor.accessibilitySupport': 'off', ...JSON.parse(process.env.SETTINGS ?? '{}') });
const paths = [process.env.FOLDER, process.env.FILE].filter(path => path) as string[];
const extraArgs = process.env.ARGS?.split(' ') ?? [];
const app = Bun.spawn([electron, ref, ...paths, ...referenceArgs, ...extraArgs, '--user-data-dir', userData, '--shared-data-dir', join(userData, 'shared'), '--remote-debugging-port=' + port], {
env: { ...process.env, VSCODE_DEV: '1', NODE_ENV: 'development' },
stdout: 'ignore',
stderr: 'ignore',
});
try {
let cdp = await connect('workbench');
await cdp('Emulation.setDeviceMetricsOverride', { width: 1024, height: 768, deviceScaleFactor: 2, mobile: false });
await until(async () => (await evaluate(cdp, `!!document.querySelector('.monaco-workbench .part.statusbar')`)) === true);
await Bun.sleep(Number(process.env.SETTLE ?? 8000));
await cdp('Page.bringToFront');
for (const action of actions) {
const [kind, ...rest] = action.split(':');
const argument = rest.join(':');
if (kind === 'eval') {
console.log(JSON.stringify(await evaluate(cdp, argument)));
} else if (kind === 'target') {
cdp = await connect(argument);
} else if (kind === 'wait') {
await Bun.sleep(Number(argument));
} else if (kind === 'shot') {
const { data } = await cdp('Page.captureScreenshot', { format: 'png' });
writeFileSync(join(outDir, `${argument}.png`), Buffer.from(data, 'base64'));
console.log(`${argument}.png`);
} else if (kind === 'type') {
await cdp('Input.insertText', { text: argument });
} else if (kind === 'key') {
const parts = argument.split('+');
const modifiers = parts.slice(0, -1).reduce((bits, name) => bits | modifierBits[name], 0);
for (const type of ['keyDown', 'keyUp']) {
await cdp('Input.dispatchKeyEvent', { type, modifiers, ...keys[parts.at(-1)!] });
}
} else if (kind === 'click' || kind === 'hover') {
const point = await evaluate(cdp, `(() => {
const element = document.querySelector(${JSON.stringify(argument)});
if (!element) return null;
const box = element.getBoundingClientRect();
return { x: box.left + box.width / 2, y: box.top + box.height / 2 };
})()`);
if (!point) {
console.log(`${action}: no element`);
continue;
}
await cdp('Input.dispatchMouseEvent', { type: 'mouseMoved', ...point });
if (kind === 'click') {
await cdp('Input.dispatchMouseEvent', { type: 'mousePressed', ...point, button: 'left', clickCount: 1 });
await cdp('Input.dispatchMouseEvent', { type: 'mouseReleased', ...point, button: 'left', clickCount: 1 });
}
} else if (kind === 'colors') {
const colors = await evaluate(cdp, `(() => {
const style = getComputedStyle(document.querySelector('.monaco-workbench'));
const names = [...style].filter(name => name.startsWith('--vscode-')).sort();
return Object.fromEntries(names.map(name => [name, style.getPropertyValue(name).trim()]));
})()`);
writeFileSync(join(outDir, `${argument}.json`), JSON.stringify(colors, null, 1) + '\n');
console.log(`${argument}.json: ${Object.keys(colors).length} variables`);
} else if (kind === 'radii') {
// Every visible rounded element of the window, for the corner checks (port/results/corners.md).
const radii = await evaluate(cdp, `[...document.querySelectorAll('*')].flatMap(element => {
const s = getComputedStyle(element);
const r = element.getBoundingClientRect();
if (s.display === 'none' || s.visibility === 'hidden' || r.width === 0 || ['0px', ''].includes(s.borderRadius)) return [];
return [{ cls: String(element.className?.baseVal ?? element.className).slice(0, 120), box: [r.x, r.y, r.width, r.height].map(v => Math.round(v * 100) / 100),
radius: s.borderRadius, border: [s.borderTopWidth, s.borderRightWidth, s.borderBottomWidth, s.borderLeftWidth].join(' '), borderColor: s.borderTopColor,
background: s.backgroundColor, overflow: s.overflow, shadow: s.boxShadow === 'none' ? undefined : s.boxShadow }];
})`);
writeFileSync(join(outDir, `${argument}.json`), JSON.stringify(radii, null, 1));
console.log(`${argument}.json: ${radii.length} rounded elements`);
} else if (kind === 'dump') {
const dump = await evaluate(cdp, `(() => {
const root = document.querySelector(${JSON.stringify(argument)});
if (!root) return null;
const keys = ['display', 'font-size', 'font-weight', 'line-height', 'color', 'background-color', 'border-top-color', 'border-top-width', 'border-radius', 'box-shadow', 'padding', 'margin', 'opacity', 'cursor', 'outline'];
const out = [];
const walk = (element, depth) => {
const r = element.getBoundingClientRect();
const s = getComputedStyle(element);
if (s.display !== 'none' && r.width > 0) {
const text = [...element.childNodes].filter(node => node.nodeType === 3).map(node => node.textContent.trim()).join(' ');
out.push({ depth, tag: element.tagName.toLowerCase(), cls: String(element.className?.baseVal ?? element.className), id: element.id || undefined, text: text || undefined, box: [r.x, r.y, r.width, r.height].map(v => Math.round(v * 100) / 100), style: Object.fromEntries(keys.map(key => [key, s.getPropertyValue(key)])) });
}
for (const child of element.children) walk(child, depth + 1);
};
walk(root, 0);
return out;
})()`);
const name = argument.replace(/[^\w.-]+/g, '_');
writeFileSync(join(outDir, `${name}.json`), JSON.stringify(dump, null, 1));
console.log(`${name}.json: ${dump ? dump.length : 'no element'}`);
}
await Bun.sleep(300);
}
} finally {
app.kill('SIGKILL');
await app.exited;
rmSync(userData, { recursive: true, force: true });
}
process.exit(0);
async function evaluate(cdp: Cdp, expression: string) {
const { result, exceptionDetails } = await cdp('Runtime.evaluate', { expression, returnByValue: true });
if (exceptionDetails) {
throw new Error(exceptionDetails.exception?.description ?? exceptionDetails.text);
}
return result.value;
}
type Cdp = (method: string, params?: object) => Promise<any>;
async function connect(urlPart: string): Promise<Cdp> {
const target = await until(async () => {
const targets: { type: string; url: string; webSocketDebuggerUrl: string }[] = await (await fetch(`http://127.0.0.1:${port}/json/list`)).json();
const pages = targets.filter(t => t.type === 'page');
if (urlPart !== 'workbench') {
console.log(targets.map(t => `${t.type} ${t.url}`).join('\n'));
}
return pages.findLast(t => t.url.includes(urlPart));
});
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, params = {}) => 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 }));
});
}
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');
}