Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.1 KB
Raw Blame
// A clone of the reference whose out/vs/nls.js tags every `localize`/`localize2` result with its module and key, so a
// dump of the running workbench (port/tools/ref-commands) knows which call made each string. The tag is U+E000 then
// the call's index in `globalThis.__nlsCalls` as two U+E001..U+F000 digits; `untag` reads it back.
// The clone is an APFS copy (`cp -c`) in the system temp folder: vscode-ref itself is never edited.
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

export function taggedClone(ref: string) {
	const clone = join(mkdtempSync(join(tmpdir(), 'ref-nls-')), 'vscode');
	const copy = Bun.spawnSync(['cp', '-cR', ref, clone]);
	if (copy.exitCode !== 0) {
		throw new Error(`cp -cR ${ref}: ${copy.stderr}`);
	}
	const nls = join(clone, 'out/vs/nls.js');
	const source = readFileSync(nls, 'utf8');
	if (!source.includes('function localize(data, message, ...args) {') || !source.includes('function localize2(data, originalMessage, ...args) {')) {
		throw new Error(`${nls}: localize or localize2 changed shape, update ref-nls-clone.ts`);
	}
	writeFileSync(nls, source
		.replace('function localize(data, message, ...args) {', 'function untaggedLocalize(data, message, ...args) {')
		.replace('function localize2(data, originalMessage, ...args) {', 'function untaggedLocalize2(data, originalMessage, ...args) {')
		+ tagging);
	return clone;
}

// The caller's module comes from the first stack frame outside nls.js: `.../out/vs/x/y.js:1:2` is `vs/x/y`.
const tagging = `
const nlsCalls = globalThis.__nlsCalls ??= [];
function nlsTag(data) {
	const key = typeof data === 'string' ? data : data?.key;
	const frame = new Error().stack.split('\\n').find(line => line.includes('/out/vs/') && !line.includes('/out/vs/nls.js'));
	const module = frame?.match(/\\/out\\/(vs\\/[^:]+)\\.js:/)?.[1];
	if (typeof key !== 'string' || !module) {
		return '';
	}
	const index = nlsCalls.push(module + '#' + key) - 1;
	return '\\uE000' + String.fromCharCode(0xE001 + (index >> 12), 0xE001 + (index & 0xFFF));
}
function localize(data, message, ...args) {
	return untaggedLocalize(data, message, ...args) + nlsTag(data);
}
function localize2(data, originalMessage, ...args) {
	const tag = nlsTag(data);
	const { value, original } = untaggedLocalize2(data, originalMessage, ...args);
	return { value: value + tag, original: original + tag };
}
`;

/** The page-side reader, as source for a Runtime.evaluate: `untag(text)` is `{ text, key }`, `key` only for one
 * whole tagged message (a message with tagged arguments formatted in has none). */
export const untagSource = `const untag = value => {
	if (typeof value !== 'string') {
		return { text: value, key: undefined };
	}
	const tags = [...value.matchAll(/\\uE000([\\uE001-\\uF000])([\\uE001-\\uF000])/g)];
	const text = value.replace(/\\uE000[\\uE001-\\uF000]{2}/g, '');
	const last = tags.at(-1);
	const whole = tags.length === 1 && last.index === value.length - 3;
	return { text, key: whole ? globalThis.__nlsCalls[((last[1].charCodeAt(0) - 0xE001) << 12) + last[2].charCodeAt(0) - 0xE001] : undefined };
};`;