220 lines
11 KiB
JavaScript
220 lines
11 KiB
JavaScript
// The variable tree of the Debug window, shared with the hover inspector of the editor (hover-inspect.js): one variable per row,
|
|
// its children read lazily (a big array a page at a time), a pencil / double click / F2 to give a scalar a new value in the running
|
|
// program, a Copy button per row (the data side is var-model.js). The caller says how to talk to the debugger through `ctx`:
|
|
// canSet() can a value be changed now (a program is stopped)
|
|
// fetchChildren(v, page) -> {items, size, pagesize} | null (debug.children)
|
|
// apply(v, expression) -> the variable as it is now (debug.set)
|
|
// promptSet(v) a dialog for a value the pencil cannot edit inline (arrays, objects)
|
|
// watch(expression) Add to Watches
|
|
// changed(v) a value was changed: the caller reads the variables again
|
|
import { el, icon, menu, t, copyText, notify } from './ui.js';
|
|
import { isScalar, isEditable, pagePlan, valueText, editText, plainText, fetchTree, toJson, toPhp } from './var-model.js';
|
|
|
|
const typeClass = (v) => (v.type || 'x').replace(/[^a-z]/g, '');
|
|
|
|
/** The small grey text after a value: visibility ("private static"), the type, the length of a string. */
|
|
function hintOf(v) {
|
|
if (['error', 'watch'].includes(v.type)) return '';
|
|
const parts = [];
|
|
if (v.facet) parts.push(v.facet);
|
|
if (v.type === 'string' && v.length > 0) parts.push(`string(${v.length})`);
|
|
else if (v.type && !(v.type === 'object' && v.class)) parts.push(v.type);
|
|
return parts.join(' ');
|
|
}
|
|
|
|
/** Arrow keys over the rows of a tree (the rows are focusable with tabindex -1): true when the key was used. */
|
|
export function navKey(e, line) {
|
|
const root = line.closest('.dbg-hover, .debug-vars');
|
|
if (!root || e.altKey || e.ctrlKey || e.metaKey) return false;
|
|
const rows = [...root.querySelectorAll('.row[tabindex]')].filter((r) => r.offsetParent !== null);
|
|
const i = rows.indexOf(line);
|
|
if (e.key === 'ArrowDown') rows[i + 1]?.focus();
|
|
else if (e.key === 'ArrowUp') rows[i - 1]?.focus();
|
|
else if (e.key === 'Home') rows[0]?.focus();
|
|
else if (e.key === 'End') rows[rows.length - 1]?.focus();
|
|
else if (e.key === 'ArrowRight' && line.dataset.expandable && !line.classList.contains('open')) line.click();
|
|
else if (e.key === 'ArrowLeft' && line.classList.contains('open')) line.click();
|
|
else return false;
|
|
e.preventDefault();
|
|
return true;
|
|
}
|
|
|
|
export class VarTree {
|
|
constructor(ctx) {
|
|
this.ctx = ctx;
|
|
this.openKeys = new Set(); // the rows the user opened (by path): the panel is drawn again after every stop, step or edit, they stay open
|
|
}
|
|
|
|
/** Forgets what was open (another stop, another frame). */
|
|
resetOpen() { this.openKeys.clear(); }
|
|
|
|
/** Copies a variable: form 'text' (a scalar as it is, a structure as JSON), 'json' or 'php'. A structure is read whole first. */
|
|
async copy(v, form = 'text') {
|
|
if (form === 'text' && (isScalar(v) || !v.children)) {
|
|
await copyText(plainText(v));
|
|
notify(t('Copied'), 'info', { timeout: 1500 });
|
|
return;
|
|
}
|
|
try {
|
|
const { tree, cut } = await fetchTree(this.ctx.fetchChildren, v);
|
|
await copyText(form === 'php' ? toPhp(tree) : toJson(tree));
|
|
notify(cut ? t('Copied, but cut: the value is too big') : t('Copied'), 'info', { timeout: cut ? 4000 : 1500 });
|
|
} catch (e) {
|
|
notify(e.message, 'error', { title: t('Copy value') });
|
|
}
|
|
}
|
|
|
|
copyMenu(v, at) {
|
|
const scalar = isScalar(v) || !v.children;
|
|
menu([
|
|
{ label: t('Copy value'), icon: 'copy', action: () => this.copy(v, 'text') },
|
|
scalar ? null : { label: t('Copy as JSON'), action: () => this.copy(v, 'json') },
|
|
{ label: t('Copy as PHP code'), action: () => this.copy(v, 'php') },
|
|
].filter(Boolean), at);
|
|
}
|
|
|
|
/** One variable (and its children once opened). o: {watch, result, onRemove, open, root, key}. A row that cannot be drawn is an error row: never an empty panel. */
|
|
row(v, level = 0, o = {}) {
|
|
try {
|
|
return this.drawRow(v, level, o);
|
|
} catch (e) {
|
|
const name = v && typeof v === 'object' ? String(v.name ?? v.fullname ?? '?') : '?';
|
|
return el('div', {}, el('div.row', { style: { paddingLeft: `${6 + level * 16}px` } }, el('span.twisty.leaf'), el('span.dbg-name', {}, name),
|
|
el('span.dbg-eq', {}, ' = '), el('span.dbg-value.error', {}, `error: ${e?.message || e}`)));
|
|
}
|
|
}
|
|
|
|
drawRow(v, level, o) {
|
|
const ctx = this.ctx;
|
|
const key = `${o.key ?? ''}\u0001${o.watch ? 'w' : o.result ? 'r' : ''}${String(v.name ?? v.fullname ?? '')}`;
|
|
const box = el('div');
|
|
const twisty = el('span.twisty' + (v.children ? '' : '.leaf'));
|
|
const value = el('span.dbg-value.' + typeClass(v), {}, valueText(v));
|
|
const showValue = () => { value.className = 'dbg-value ' + typeClass(v); value.textContent = valueText(v); };
|
|
const actions = [];
|
|
const settable = () => !o.watch && ctx.canSet() && !!(v.fullname || v.expression) && v.type !== 'error';
|
|
if (!o.watch && !o.result && isEditable(v) && ctx.canSet()) {
|
|
actions.push(el('button.row-action', { title: t('Edit value'), 'aria-label': t('Edit value'), onclick: (e) => { e.stopPropagation(); edit(); } }, icon('edit')));
|
|
}
|
|
if (v.type !== 'error' && v.type !== 'watch' && (v.fullname || v.expression || v.value !== undefined)) {
|
|
actions.push(el('button.row-action', { title: t('Copy value'), 'aria-label': t('Copy value'), onclick: (e) => { e.stopPropagation(); this.copy(v); } }, icon('copy')));
|
|
}
|
|
if (o.onRemove) actions.push(el('button.row-action', { title: t('Remove the watch'), onclick: (e) => { e.stopPropagation(); o.onRemove(v); } }, icon('close')));
|
|
const hint = hintOf(v);
|
|
const line = el('div.row', { style: { paddingLeft: `${6 + level * 16}px` }, tabIndex: -1, role: 'treeitem' },
|
|
twisty, el('span.dbg-name' + (o.watch ? '.watch' : o.result ? '.result' : ''), {}, v.name || v.fullname || '?'), el('span.dbg-eq', {}, ' = '), value,
|
|
hint ? el('span.hint', {}, hint) : null, ...actions);
|
|
box.append(line);
|
|
|
|
// a new value, typed in the row itself (a PHP expression: 9, 'Ada', null, $other * 2...), applied to the running program
|
|
const edit = () => {
|
|
if (!ctx.canSet()) return;
|
|
if (!isEditable(v)) { ctx.promptSet?.(v); return; }
|
|
const input = el('input.dbg-edit', { value: editText(v), spellcheck: false, 'aria-label': t('Edit value'), title: t('A PHP expression, evaluated where the program is stopped') });
|
|
value.replaceWith(input);
|
|
input.focus();
|
|
input.select();
|
|
let done = false;
|
|
const finish = async (apply) => {
|
|
if (done) return;
|
|
done = true;
|
|
const expression = input.value.trim();
|
|
input.replaceWith(value);
|
|
line.focus();
|
|
if (!apply || !expression || expression === editText(v)) return;
|
|
try {
|
|
const now = await ctx.apply(v, expression);
|
|
if (now) { Object.assign(v, now, { name: v.name }); showValue(); }
|
|
ctx.changed?.(v);
|
|
} catch (err) {
|
|
notify(err.message, 'error', { title: t('Edit value') });
|
|
}
|
|
};
|
|
input.addEventListener('keydown', (e) => {
|
|
e.stopPropagation();
|
|
if (e.key === 'Enter') { e.preventDefault(); finish(true); } else if (e.key === 'Escape') { e.preventDefault(); finish(false); }
|
|
});
|
|
input.addEventListener('click', (e) => e.stopPropagation());
|
|
input.addEventListener('dblclick', (e) => e.stopPropagation());
|
|
input.addEventListener('blur', () => finish(false));
|
|
};
|
|
box.edit = edit;
|
|
|
|
if (!o.watch && !o.result || o.root) {
|
|
line.title = t('Double click or F2: set its value');
|
|
line.ondblclick = (e) => { e.stopPropagation(); if (settable() || o.root) edit(); };
|
|
line.oncontextmenu = (e) => {
|
|
e.preventDefault();
|
|
menu([
|
|
settable() ? { label: t('Set Value…'), kbd: 'F2', action: edit } : null,
|
|
{ label: t('Copy value'), icon: 'copy', action: () => this.copy(v, 'text') },
|
|
v.children ? { label: t('Copy as JSON'), action: () => this.copy(v, 'json') } : null,
|
|
{ label: t('Copy as PHP code'), action: () => this.copy(v, 'php') },
|
|
(v.fullname || v.expression) ? { label: t('Add to Watches'), action: () => ctx.watch?.(v.fullname || v.expression) } : null,
|
|
].filter(Boolean), { x: e.clientX, y: e.clientY });
|
|
};
|
|
}
|
|
line.onkeydown = (e) => {
|
|
if (e.target !== line) return;
|
|
if (navKey(e, line)) return;
|
|
if ((e.key === 'F2' || (e.key === 'Enter' && isScalar(v))) && (settable() || o.root)) { e.preventDefault(); edit(); }
|
|
};
|
|
|
|
if (v.children) {
|
|
line.dataset.expandable = '1';
|
|
line.setAttribute('aria-expanded', 'false');
|
|
let open = false;
|
|
let kids = null;
|
|
const toggle = async () => {
|
|
open = !open;
|
|
if (open) this.openKeys.add(key); else this.openKeys.delete(key);
|
|
line.classList.toggle('open', open);
|
|
line.setAttribute('aria-expanded', String(open));
|
|
if (!open) { kids?.remove(); kids = null; return; }
|
|
kids = el('div', { role: 'group' });
|
|
const mine = kids;
|
|
box.append(kids);
|
|
let first;
|
|
try {
|
|
first = v.items ? { items: v.items, size: v.size, pagesize: v.pagesize } : await ctx.fetchChildren(v, 0);
|
|
} catch (e) {
|
|
first = { items: [{ name: 'error', value: e?.message || String(e), type: 'error' }] };
|
|
}
|
|
if (!open || kids !== mine) return;
|
|
let shown = 0;
|
|
let size = v.size ?? first?.size ?? 0;
|
|
const pagesize = v.pagesize || first?.pagesize || (first?.items || []).length;
|
|
const more = el('div.row.dbg-more', { style: { paddingLeft: `${6 + (level + 1) * 16}px` }, tabIndex: -1, role: 'button' },
|
|
el('span.twisty.leaf'), el('span.hint'));
|
|
const refresh = () => {
|
|
more.remove();
|
|
const { left, page } = pagePlan(shown, size, pagesize);
|
|
if (left > 0 && page !== null) {
|
|
more.lastChild.textContent = t('Show {n} more…', { n: Math.min(left, pagesize) });
|
|
more.onclick = async (e) => {
|
|
e.stopPropagation();
|
|
more.classList.add('busy');
|
|
let next = null;
|
|
try { next = await ctx.fetchChildren(v, page); } catch { next = null; }
|
|
more.classList.remove('busy');
|
|
if (!open || kids !== mine) return;
|
|
if (!next?.items?.length) { size = shown; refresh(); return; }
|
|
append(next.items);
|
|
};
|
|
more.onkeydown = (e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); more.click(); } else navKey(e, more); };
|
|
kids.append(more);
|
|
}
|
|
};
|
|
const append = (items) => {
|
|
for (const c of Array.isArray(items) ? items : []) kids.append(this.row(c ?? { name: '?', type: 'error', value: 'empty' }, level + 1, { key }));
|
|
shown += items.length;
|
|
refresh();
|
|
};
|
|
append(first?.items || []);
|
|
};
|
|
line.onclick = toggle;
|
|
if (o.open || this.openKeys.has(key)) { if (o.open) this.openKeys.add(key); toggle(); }
|
|
}
|
|
return box;
|
|
}
|
|
}
|