Repository navigation
Expand file tree
/
Copy pathplayground.js
More file actions
433 lines (395 loc) · 14.6 KB
/
Copy pathplayground.js
File metadata and controls
433 lines (395 loc) · 14.6 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// The clang-format playground on /playground/.
//
// clang-format runs in the browser: each LLVM version is a WebAssembly build
// from the @wasm-fmt/clang-format npm package, imported from jsDelivr the first
// time that version is picked. Releases up to 21 export `init` from
// clang-format.js; from 22 the browser entry is clang-format-web.js. Both then
// export `format(code, filename, style)` and `version()`.
//
// The result and the commands are <pre> elements without <code>: Material
// rewrites every `pre > code` it finds when the page loads.
//
// The picked version, style, language, code and .clang-format text are kept in
// the URL hash, so "Copy link" shares exactly what is on screen.
const CDN = "https://cdn.jsdelivr.net/npm/@wasm-fmt/clang-format@";
// LLVM major version -> the newest @wasm-fmt/clang-format release for it.
const VERSIONS = [
["23", "23.1.1"],
["22", "22.1.8"],
["21", "21.1.8"],
["20", "20.1.7"],
["19", "19.1.7"],
["18", "18.1.8"],
["17", "17.0.6"],
["16", "16.0.6"],
];
const DEFAULT_VERSION = "21";
const STYLES = ["LLVM", "Google", "Chromium", "Mozilla", "WebKit", "Microsoft", "GNU", "file"];
const SAMPLE = `#include <string>
#include<vector>
namespace demo{
struct Point{int x;int y;};
int sum(const std::vector<int>&values){int total=0;
for(auto v:values){total+=v;}
return total;}
std::string greet(const std::string &name) { return "Hello, "+name+"!"; }
}
`;
// Lines past this are formatted but not diffed: the diff is quadratic.
const MAX_DIFF_LINES = 3000;
// Unchanged lines shown around each change in the diff.
const CONTEXT = 3;
const $ = (id) => document.getElementById(id);
const ui = {
version: $("cl-pg-version"),
style: $("cl-pg-style"),
lang: $("cl-pg-lang"),
share: $("cl-pg-share"),
configWrap: $("cl-pg-config-wrap"),
config: $("cl-pg-config"),
input: $("cl-pg-input"),
status: $("cl-pg-status"),
diff: $("cl-pg-diff"),
out: $("cl-pg-out"),
copyOut: $("cl-pg-copy-out"),
viewDiff: $("cl-pg-view-diff"),
viewOut: $("cl-pg-view-out"),
cmdLocal: $("cl-pg-cmd-local"),
cmdHooks: $("cl-pg-cmd-hooks"),
cmdAction: $("cl-pg-cmd-action"),
fileNote: $("cl-pg-file-note"),
};
const ICON_PASS =
'<svg aria-hidden="true" viewBox="0 0 16 16" width="16" height="16"><circle cx="8" cy="8" r="8" fill="#48B888"/><path d="M4.5 8.2l2.3 2.3 4.7-4.9" fill="none" stroke="#fff" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>';
const ICON_WARN =
'<svg aria-hidden="true" viewBox="0 0 16 16" width="16" height="16"><circle cx="8" cy="8" r="8" fill="#F8C418"/><path d="M8 4v5" stroke="#1F1D1A" stroke-width="1.9" stroke-linecap="round"/><circle cx="8" cy="11.6" r="1.1" fill="#1F1D1A"/></svg>';
const ICON_FAIL =
'<svg aria-hidden="true" viewBox="0 0 16 16" width="16" height="16"><circle cx="8" cy="8" r="8" fill="#E5534B"/><path d="M5.4 5.4l5.2 5.2M10.6 5.4l-5.2 5.2" stroke="#fff" stroke-width="1.8" stroke-linecap="round"/></svg>';
// ---- loading clang-format -------------------------------------------------
const modules = new Map();
function loadVersion(major) {
if (!modules.has(major)) {
const release = VERSIONS.find(([m]) => m === major)[1];
const entry = Number(major) >= 22 ? "clang-format-web.js" : "clang-format.js";
const loading = import(`${CDN}${release}/${entry}`).then(async (mod) => {
await mod.default();
return mod;
});
// A failed download is retried the next time the version is picked.
loading.catch(() => modules.delete(major));
modules.set(major, loading);
}
return modules.get(major);
}
// ---- URL hash -------------------------------------------------------------
function encode(text) {
const bytes = new TextEncoder().encode(text);
let bin = "";
for (const b of bytes) bin += String.fromCharCode(b);
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
function decode(text) {
const bin = atob(text.replace(/-/g, "+").replace(/_/g, "/"));
return new TextDecoder().decode(Uint8Array.from(bin, (c) => c.charCodeAt(0)));
}
function readHash() {
const params = new URLSearchParams(location.hash.slice(1));
const state = {};
const v = params.get("v");
if (VERSIONS.some(([m]) => m === v)) state.version = v;
const s = params.get("style");
if (STYLES.includes(s)) state.style = s;
const l = params.get("lang");
if (l === "c") state.lang = "main.c";
try {
if (params.has("code")) state.code = decode(params.get("code"));
if (params.has("config")) state.config = decode(params.get("config"));
} catch {
// A truncated or edited link: keep the defaults for what cannot be read.
}
return state;
}
function writeHash() {
const params = new URLSearchParams();
params.set("v", ui.version.value);
params.set("style", ui.style.value);
if (ui.lang.value === "main.c") params.set("lang", "c");
if (ui.style.value === "file") params.set("config", encode(ui.config.value));
params.set("code", encode(ui.input.value));
history.replaceState(null, "", `#${params}`);
}
// ---- diff -----------------------------------------------------------------
// A line diff from the longest common subsequence. Returns rows of
// {kind: " " | "-" | "+", text, a, b} with 1-based line numbers.
function diffLines(before, after) {
const a = before.split("\n");
const b = after.split("\n");
const n = a.length;
const m = b.length;
const width = m + 1;
const lcs = new Uint32Array((n + 1) * width);
for (let i = n - 1; i >= 0; i--) {
for (let j = m - 1; j >= 0; j--) {
lcs[i * width + j] =
a[i] === b[j]
? lcs[(i + 1) * width + j + 1] + 1
: Math.max(lcs[(i + 1) * width + j], lcs[i * width + j + 1]);
}
}
const rows = [];
let i = 0;
let j = 0;
while (i < n || j < m) {
if (i < n && j < m && a[i] === b[j]) {
rows.push({ kind: " ", text: a[i], a: i + 1, b: j + 1 });
i++;
j++;
} else if (i < n && (j === m || lcs[(i + 1) * width + j] >= lcs[i * width + j + 1])) {
rows.push({ kind: "-", text: a[i], a: i + 1, b: "" });
i++;
} else {
rows.push({ kind: "+", text: b[j], a: "", b: j + 1 });
j++;
}
}
// In each run of changed lines, show the removed lines before the added
// ones, the way a unified diff reads.
const ordered = [];
let run = [];
const flush = () => {
ordered.push(...run.filter((r) => r.kind === "-"), ...run.filter((r) => r.kind === "+"));
run = [];
};
for (const row of rows) {
if (row.kind === " ") {
flush();
ordered.push(row);
} else {
run.push(row);
}
}
flush();
return ordered;
}
function renderDiff(rows) {
const frag = document.createElement("div");
frag.className = "cl-pg__lines";
const changed = rows.map((r) => r.kind !== " ");
const near = new Uint8Array(rows.length);
changed.forEach((c, k) => {
if (!c) return;
for (let d = Math.max(0, k - CONTEXT); d <= Math.min(rows.length - 1, k + CONTEXT); d++) near[d] = 1;
});
let skipped = 0;
const flushSkipped = () => {
if (!skipped) return;
const gap = document.createElement("div");
gap.className = "cl-pg__gap";
gap.textContent = `${skipped} unchanged line${skipped === 1 ? "" : "s"}`;
frag.append(gap);
skipped = 0;
};
rows.forEach((row, k) => {
if (!near[k]) {
skipped++;
return;
}
flushSkipped();
const line = document.createElement("div");
line.className = "cl-pg__line" + (row.kind === "-" ? " cl-pg__line--del" : row.kind === "+" ? " cl-pg__line--add" : "");
const numA = document.createElement("span");
numA.className = "cl-pg__num";
numA.textContent = row.a;
const numB = document.createElement("span");
numB.className = "cl-pg__num";
numB.textContent = row.b;
const sign = document.createElement("span");
sign.className = "cl-pg__sign";
sign.textContent = row.kind === " " ? "" : row.kind === "-" ? "−" : "+";
const code = document.createElement("code");
code.textContent = row.text || " ";
line.append(numA, numB, sign, code);
frag.append(line);
});
flushSkipped();
ui.diff.replaceChildren(frag);
}
// ---- commands -------------------------------------------------------------
function renderCommands() {
const v = ui.version.value;
const style = ui.style.value;
const file = ui.lang.value;
ui.cmdLocal.textContent = [
"pip install clang-tools",
`clang-tools install clang-format --version ${v}`,
`clang-format-${v} --style=${style} -i ${file}`,
].join("\n");
// scripts/update-versions.py keeps this rev at the latest release.
ui.cmdHooks.textContent = `- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.10.0
hooks:
- id: clang-format
args: [--style=${style}, --version=${v}]`;
ui.cmdAction.textContent = [
"- uses: cpp-linter/cpp-linter-action@v2",
" with:",
` style: ${style}`,
` version: ${v}`,
].join("\n");
ui.fileNote.hidden = style !== "file";
}
// ---- formatting -----------------------------------------------------------
// "clang-format version 21.1.8 (https://github.com/wasm-fmt/...)" -> "21.1.8"
function shortVersion(mod) {
const match = /\d+\.\d+\.\d+/.exec(mod.version());
return match ? match[0] : mod.version();
}
// clang-format prints why a style failed to stderr, which the WebAssembly
// build sends to console.error. Keep those lines while it runs. Releases up to
// 16 return the error as the result instead of throwing it.
function runFormat(mod, source, filename, style) {
const printed = [];
const original = console.error;
console.error = (...args) => printed.push(args.join(" "));
try {
const result = mod.format(source, filename, style);
if (/^Error (reading \.clang-format|parsing -style): /.test(result) && result !== source) {
throw new Error(result);
}
return result;
} catch (err) {
const detail = printed.find((line) => line.trim() && !/^Error /.test(line)) || "";
err.detail = detail.replace(/^error: /, "").trim();
throw err;
} finally {
console.error = original;
}
}
function setStatus(icon, text) {
ui.status.innerHTML = icon;
ui.status.append(document.createTextNode(text));
}
let run = 0;
// The hash is written once the visitor changes something, so opening the page
// leaves its address clean.
let touched = false;
async function update() {
const ticket = ++run;
const major = ui.version.value;
const styleName = ui.style.value;
const style = styleName === "file" ? ui.config.value : styleName;
ui.configWrap.hidden = styleName !== "file";
renderCommands();
if (touched) writeHash();
let mod;
if (!modules.has(major)) setStatus("", `Loading clang-format ${major}…`);
try {
mod = await loadVersion(major);
} catch (err) {
if (ticket !== run) return;
setStatus(ICON_FAIL, `Could not load clang-format ${major} from jsDelivr. Check your connection and pick the version again.`);
return;
}
if (ticket !== run) return;
const source = ui.input.value;
let formatted;
try {
formatted = runFormat(mod, source, ui.lang.value, style);
} catch (err) {
const message = String(err.message || err).replace(/: Invalid argument$/, "").trim();
setStatus(ICON_FAIL, `clang-format ${shortVersion(mod)}: ${message}${err.detail ? ` (${err.detail})` : ""}.`);
ui.diff.replaceChildren();
ui.out.textContent = "";
return;
}
const label = `clang-format ${shortVersion(mod)} with ${styleName === "file" ? "your .clang-format" : `--style=${styleName}`}`;
ui.out.textContent = formatted;
if (formatted === source) {
setStatus(ICON_PASS, `Already formatted. ${label} changes nothing.`);
ui.diff.replaceChildren();
return;
}
const lines = Math.max(source.split("\n").length, formatted.split("\n").length);
if (lines > MAX_DIFF_LINES) {
setStatus(ICON_WARN, `${label} changes this file. It is too long to diff here: see Formatted.`);
ui.diff.replaceChildren();
return;
}
const rows = diffLines(source, formatted);
const removed = rows.filter((r) => r.kind === "-").length;
const added = rows.filter((r) => r.kind === "+").length;
setStatus(ICON_WARN, `${label} changes ${removed} line${removed === 1 ? "" : "s"} into ${added}.`);
renderDiff(rows);
}
let timer;
function scheduleUpdate() {
clearTimeout(timer);
timer = setTimeout(update, 250);
}
function showView(view) {
const diff = view === "diff";
ui.viewDiff.setAttribute("aria-pressed", String(diff));
ui.viewOut.setAttribute("aria-pressed", String(!diff));
ui.diff.hidden = !diff;
ui.out.hidden = diff;
ui.copyOut.hidden = diff;
}
async function copyText(button, text) {
const label = button.textContent;
try {
await navigator.clipboard.writeText(text);
button.textContent = "Copied";
} catch {
button.textContent = "Select and copy";
}
setTimeout(() => (button.textContent = label), 2000);
}
// ---- start ----------------------------------------------------------------
for (const [major, release] of VERSIONS) {
const option = document.createElement("option");
option.value = major;
option.textContent = `${major} (clang-format ${release})`;
ui.version.append(option);
}
const initial = readHash();
ui.version.value = initial.version || DEFAULT_VERSION;
ui.style.value = initial.style || "LLVM";
ui.lang.value = initial.lang || "main.cpp";
if (initial.config !== undefined) ui.config.value = initial.config;
touched = location.hash.length > 1;
ui.input.value = initial.code !== undefined ? initial.code : SAMPLE;
const changed = (handler) => () => {
touched = true;
handler();
};
ui.version.addEventListener("change", changed(update));
ui.style.addEventListener("change", changed(update));
ui.lang.addEventListener("change", changed(update));
ui.input.addEventListener("input", changed(scheduleUpdate));
ui.config.addEventListener("input", changed(scheduleUpdate));
ui.viewDiff.addEventListener("click", () => showView("diff"));
ui.viewOut.addEventListener("click", () => showView("out"));
// Tab inserts indentation instead of leaving the editor; Escape then Tab leaves it.
let escaped = false;
ui.input.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
escaped = true;
return;
}
if (event.key === "Tab" && !event.shiftKey && !escaped) {
event.preventDefault();
ui.input.setRangeText(" ", ui.input.selectionStart, ui.input.selectionEnd, "end");
touched = true;
scheduleUpdate();
}
escaped = false;
});
ui.share.addEventListener("click", () => {
writeHash();
copyText(ui.share, location.href);
});
ui.copyOut.addEventListener("click", () => copyText(ui.copyOut, ui.out.textContent));
for (const button of document.querySelectorAll(".cl-pg [data-copy]")) {
button.addEventListener("click", () => copyText(button, $(button.dataset.copy).textContent));
}
update();