Файли
openidea/crates/ide-plugin/tests/fixtures/editor-panel/web/index.html
T
LU XIANGRUIтаwildcatDownstairs 577193d387 feat(plugins): the editor's tab size in editor panels (#3906)
openidea.editor.get() includes tabSize, the editor state's tab size (Code Style, .editorconfig, indent detection or the status bar's indent picker), and onTarget fires again with the new get() value when it changes for the same file, at most once a frame. Documented under API 1.10; the minimap fixture expands tabs to that size.

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-17 13:31:19 +08:00

289 рядки
11 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Minimap probe</title>
<style>
html,
body {
margin: 0;
height: 100%;
overflow: hidden;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: default;
}
</style>
</head>
<body>
<canvas id="map"></canvas>
<script>
// A minimal minimap over `openidea.editor`: each line 2px high, each character 1px wide (tabs
// as wide as the editor draws them), in its highlight colour; the visible area as a frame that
// can be dragged; the wheel scrolls the editor; the marker bar's problems as bars at the right
// edge; folded lines left out; the selected lines tinted and the caret's line marked; VCS
// changes at the left edge.
const editor = window.openidea.editor;
const canvas = document.getElementById("map");
const ctx = canvas.getContext("2d");
const LINE = 2;
const CHUNK = 2000;
let text = [];
let colors = new Map();
let loading = new Set();
let view = null;
let problems = [];
let folds = [];
let selections = [];
let changes = [];
let tabSize = 4;
const PROBLEM_COLOR = { error: "--openidea-danger", warning: "--openidea-warning" };
const PROBLEM_RANK = { error: 2, warning: 1 };
let drag = null;
let queued = false;
const grey = (color) => {
const m = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(color);
return !!m && Math.max(m[1], m[2], m[3]) - Math.min(m[1], m[2], m[3]) < 24;
};
// Column → x, with tabs expanded to the editor's tab stops.
const xOf = (line) => {
if (!line.includes("\t")) return (c) => c;
const xs = [0];
for (let i = 0, x = 0; i < line.length; i++) xs.push((x += line[i] === "\t" ? tabSize - (x % tabSize) : 1));
return (c) => xs[Math.min(c, line.length)];
};
const cssVar = (name) => (window.openidea.theme && window.openidea.theme.vars[name]) || "";
// Document lines ↔ minimap rows, without the lines folds hide (fractional within a fold's row).
const rowOf = (line) => {
let hidden = 0;
for (const f of folds) {
if (line >= f.toLine + 1) hidden += f.toLine - f.fromLine;
else if (line > f.fromLine) return f.fromLine - hidden + (line - f.fromLine) / (f.toLine + 1 - f.fromLine);
else break;
}
return line - hidden;
};
const lineOfRow = (row) => {
let hidden = 0;
for (const f of folds) {
const r = f.fromLine - hidden;
if (row >= r + 1) hidden += f.toLine - f.fromLine;
else if (row > r) return f.fromLine + (row - r) * (f.toLine + 1 - f.fromLine);
else break;
}
return row + hidden;
};
function layout() {
const H = canvas.clientHeight;
if (!view) return { H, offset: 0, top: 0, height: 0, range: 1, visible: 0 };
const L = Math.max(1, rowOf(view.lineCount));
const firstRow = rowOf(view.firstLine);
const visible = Math.max(0, rowOf(view.lastLine) - firstRow);
const scrollable = Math.max(1, view.scrollHeight - view.clientHeight);
const p = Math.min(1, Math.max(0, view.scrollTop / scrollable));
const mapH = L * LINE;
const offset = p * Math.max(0, mapH - H);
const range = Math.max(1, Math.min(H, mapH) - visible * LINE);
return { H, offset, top: firstRow * LINE - offset, height: visible * LINE, range, visible, L };
}
function redraw() {
if (queued) return;
queued = true;
requestAnimationFrame(draw);
}
function draw() {
queued = false;
const dpr = window.devicePixelRatio || 1;
const W = canvas.clientWidth;
const { H, offset, top, height, L } = layout();
if (canvas.width !== Math.round(W * dpr) || canvas.height !== Math.round(H * dpr)) {
canvas.width = Math.round(W * dpr);
canvas.height = Math.round(H * dpr);
}
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, W, H);
if (!view) return;
const first = Math.floor(offset / LINE);
const last = Math.min(L, Math.ceil((offset + H) / LINE));
const missing = [];
const muted = cssVar("--openidea-fg-muted");
// Selections under the text: the lines they cover tinted, a caret's line a stronger strip.
ctx.fillStyle = cssVar("--openidea-accent");
for (const s of selections) {
const caret = s.anchor.line === s.head.line && s.anchor.column === s.head.column;
const from = Math.floor(rowOf(Math.min(s.anchor.line, s.head.line)));
const to = Math.floor(rowOf(Math.max(s.anchor.line, s.head.line)));
ctx.globalAlpha = caret ? 0.6 : 0.3;
ctx.fillRect(0, from * LINE - offset, W, (to - from + 1) * LINE);
}
ctx.globalAlpha = 1;
for (let row = first; row < last; row++) {
const i = lineOfRow(row);
const y = row * LINE - offset;
const line = text[i] || "";
const x = xOf(line);
// Text without a syntax colour: faint, so the coloured spans stand out.
ctx.globalAlpha = 0.2;
ctx.fillStyle = muted;
for (const m of line.matchAll(/\S+/g)) ctx.fillRect(x(m.index), y, m[0].length, LINE - 0.5);
ctx.globalAlpha = 1;
const spans = colors.get(i);
if (!spans) missing.push(i);
else
for (const s of spans) {
// Greys (the plain text colour, comments) stay as faint as uncoloured text.
if (grey(s.color)) continue;
ctx.fillStyle = s.color;
ctx.fillRect(x(s.from), y, x(s.to) - x(s.from), LINE - 0.5);
}
}
// Changes in the gutter's colours: a bar over changed lines, a notch where lines were deleted.
for (const c of changes) {
ctx.fillStyle = cssVar(`--openidea-change-${c.kind}`);
const y = Math.floor(rowOf(c.line)) * LINE - offset;
if (c.kind === "deleted") ctx.fillRect(0, y - 1, 4, 2);
else ctx.fillRect(0, y, 2, (Math.floor(rowOf(c.lastLine)) - Math.floor(rowOf(c.line)) + 1) * LINE);
}
// Problems: errors drawn last, on top; weak warnings in the muted colour.
const ranked = [...problems].sort((a, b) => (PROBLEM_RANK[a.severity] || 0) - (PROBLEM_RANK[b.severity] || 0));
for (const p of ranked) {
ctx.fillStyle = cssVar(PROBLEM_COLOR[p.severity] || "--openidea-fg-muted");
const y = Math.floor(rowOf(p.line));
ctx.fillRect(W - 3, y * LINE - offset, 3, Math.max(LINE, (Math.floor(rowOf(p.lastLine)) - y + 1) * LINE));
}
// The visible area: a light accent tint and a 1px border, stronger while dragged.
const frameH = Math.max(height, 4);
ctx.fillStyle = cssVar("--openidea-accent");
ctx.globalAlpha = drag ? 0.16 : 0.08;
ctx.fillRect(0, top, W, frameH);
ctx.strokeStyle = ctx.fillStyle;
ctx.globalAlpha = drag ? 0.9 : 0.5;
ctx.lineWidth = 1;
ctx.strokeRect(0.5, top + 0.5, W - 1, frameH - 1);
ctx.globalAlpha = 1;
if (missing.length) load(missing[0], missing[missing.length - 1] + 1);
}
async function load(from, to) {
const key = `${from}:${to}`;
if (loading.has(key)) return;
loading.add(key);
const target = view && view.version;
try {
for (let at = from; at < to; at += CHUNK) {
const lines = await editor.highlights(at, Math.min(to, at + CHUNK));
lines.forEach((spans, i) => colors.set(at + i, spans));
}
} finally {
loading.delete(key);
}
if (view && view.version === target) redraw();
}
async function reload() {
const target = await editor.get();
colors = new Map();
text = [];
if (!target) {
view = null;
return redraw();
}
// `tabSize` is API 1.10.
tabSize = target.tabSize || 4;
const doc = await editor.document.get();
text = doc.text.split("\n");
// `problems` is API 1.10.
problems = editor.problems ? await editor.problems() : [];
folds = editor.folded ? await editor.folded() : [];
selections = editor.selections ? await editor.selections() : [];
changes = editor.changes ? await editor.changes() : [];
view = Object.assign(await editor.viewport(), { version: target.version });
redraw();
}
editor.onTarget(() => void reload());
editor.document.onChange((doc) => {
text = doc.text.split("\n");
redraw();
});
editor.onViewport((v) => {
view = Object.assign(v, { version: view ? view.version : 0 });
redraw();
});
editor.onHighlights(({ fromLine, toLine }) => {
for (const k of [...colors.keys()]) if (k >= fromLine && k < toLine) colors.delete(k);
redraw();
});
if (editor.onProblems)
editor.onProblems((p) => {
problems = p;
redraw();
});
// `onLayout` is API 1.10.
if (editor.onLayout)
editor.onLayout((l) => {
folds = l.folded;
redraw();
});
if (editor.onSelections)
editor.onSelections((s) => {
selections = s;
redraw();
});
if (editor.onChanges)
editor.onChanges((c) => {
changes = c;
redraw();
});
window.openidea.onThemeChange(redraw);
new ResizeObserver(redraw).observe(canvas);
// Dragging the frame (or pressing beside it, which centres it there) scrolls the editor.
const scrollToFrameTop = (frameTop) => {
const { range, visible, L } = layout();
const p = Math.min(1, Math.max(0, frameTop / range));
void editor.scrollTo(lineOfRow(p * Math.max(0, L - visible)));
};
canvas.addEventListener("pointerdown", (e) => {
if (!view) return;
const { top, height } = layout();
const grab = e.offsetY >= top && e.offsetY <= top + height ? e.offsetY - top : height / 2;
drag = { grab };
try {
canvas.setPointerCapture(e.pointerId);
} catch {
// A pointer that isn't down (an event a test dispatched).
}
scrollToFrameTop(e.offsetY - grab);
});
canvas.addEventListener("pointermove", (e) => {
if (drag) scrollToFrameTop(e.offsetY - drag.grab);
});
const stop = () => {
drag = null;
redraw();
};
canvas.addEventListener("pointerup", stop);
canvas.addEventListener("pointercancel", stop);
canvas.addEventListener(
"wheel",
(e) => {
e.preventDefault();
void editor.scrollBy(e.deltaY);
},
{ passive: false },
);
void reload();
</script>
</body>
</html>