fix: indentation
This commit is contained in:
parent
a415dab320
commit
25a12368a0
|
|
@ -331,26 +331,33 @@ class QueryBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compact-but-readable JSON formatter. Inlines any subtree that fits on a single
|
// Standard 2-space JSON prettifier with one twist: any subtree whose single-line
|
||||||
// line within MAX_INLINE chars; otherwise breaks across lines with 2-space indent.
|
// form fits within MAX_INLINE chars at its current column gets inlined. Breaks
|
||||||
|
// always use clean 2-space indent — never key-aligned.
|
||||||
const MAX_INLINE = 60;
|
const MAX_INLINE = 60;
|
||||||
|
|
||||||
function formatQueryJson(value, indent = 0) {
|
function formatQueryJson(value, indent = 0, startCol = indent) {
|
||||||
const inline = inlineForm(value);
|
const inline = inlineForm(value);
|
||||||
if (inline.length + indent <= MAX_INLINE) return inline;
|
if (startCol + inline.length <= MAX_INLINE) return inline;
|
||||||
|
|
||||||
|
const childIndent = indent + 2;
|
||||||
|
const pad = ' '.repeat(childIndent);
|
||||||
|
const close = ' '.repeat(indent);
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
if (value.length === 0) return '[]';
|
if (value.length === 0) return '[]';
|
||||||
const pad = ' '.repeat(indent + 2);
|
const items = value.map(v => pad + formatQueryJson(v, childIndent));
|
||||||
const close = ' '.repeat(indent);
|
|
||||||
const items = value.map(v => pad + formatQueryJson(v, indent + 2));
|
|
||||||
return '[\n' + items.join(',\n') + '\n' + close + ']';
|
return '[\n' + items.join(',\n') + '\n' + close + ']';
|
||||||
}
|
}
|
||||||
if (value && typeof value === 'object') {
|
if (value && typeof value === 'object') {
|
||||||
const entries = Object.entries(value);
|
const entries = Object.entries(value);
|
||||||
if (entries.length === 0) return '{}';
|
if (entries.length === 0) return '{}';
|
||||||
const pad = ' '.repeat(indent + 2);
|
const items = entries.map(([k, v]) => {
|
||||||
const close = ' '.repeat(indent);
|
const keyStr = JSON.stringify(k) + ': ';
|
||||||
const items = entries.map(([k, v]) => pad + JSON.stringify(k) + ': ' + formatQueryJson(v, indent + 2 + JSON.stringify(k).length + 2));
|
// Recurse at childIndent for break depth, but tell the recursion the
|
||||||
|
// value's actual column for the inline-fit check.
|
||||||
|
return pad + keyStr + formatQueryJson(v, childIndent, childIndent + keyStr.length);
|
||||||
|
});
|
||||||
return '{\n' + items.join(',\n') + '\n' + close + '}';
|
return '{\n' + items.join(',\n') + '\n' + close + '}';
|
||||||
}
|
}
|
||||||
return JSON.stringify(value);
|
return JSON.stringify(value);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue