Fixing pretty printer indentation. launching inspector when selecting any store value

master
Dustin Swan 2 weeks ago
parent 5c4ecbafdb
commit f27b946790
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -180,9 +180,9 @@ export function prettyPrint(ast: AST, indent = 0): string {
case 'literal': { case 'literal': {
const val = ast.value; const val = ast.value;
if (val.kind === 'string') { if (val.kind === 'string') {
return `${i}"${val.value}"`; return `"${val.value}"`;
} }
return `${i}${val.value}`; return `${val.value}`;
} }
case 'variable': case 'variable':
@ -190,9 +190,9 @@ export function prettyPrint(ast: AST, indent = 0): string {
return ast.name; return ast.name;
case 'apply': case 'apply':
const func = prettyPrint(ast.func, 0); const func = prettyPrint(ast.func, indent);
const args = ast.args.map(a => { const args = ast.args.map(a => {
const printed = prettyPrint(a, 0); const printed = prettyPrint(a, indent);
if (a.kind === 'lambda' || a.kind === 'match' || a.kind === 'let' || a.kind === 'rebind') { if (a.kind === 'lambda' || a.kind === 'match' || a.kind === 'let' || a.kind === 'rebind') {
return `(${printed})`; return `(${printed})`;
} }
@ -204,17 +204,22 @@ export function prettyPrint(ast: AST, indent = 0): string {
case 'let': case 'let':
return `${ast.name} = ${prettyPrint(ast.value, indent + 1)};\n${i}${prettyPrint(ast.body, indent)}` return `${ast.name} = ${prettyPrint(ast.value, indent + 1)};\n${i}${prettyPrint(ast.body, indent)}`
case 'list': case 'list': {
const elems = ast.elements.map(e => prettyPrint(e, 0)).join(', '); const elems = ast.elements.map(e => prettyPrint(e, indent + 1))
return `[${elems}]`; if (elems.length <= 1) return `[${elems.join(', ')}]`;
const inner = elems.map(e => `${' '.repeat(indent + 1)}${e}`).join(',\n');
return `[\n${inner}\n${i}]`;
}
case 'record': case 'record':
const parts = ast.entries.map(entry => const parts = ast.entries.map(entry =>
entry.kind === 'spread' entry.kind === 'spread'
? `...${prettyPrint(entry.expr, )}` ? `...${prettyPrint(entry.expr, indent + 1)}`
: `${entry.key} = ${prettyPrint(entry.value, 0)}` : `${entry.key} = ${prettyPrint(entry.value, indent + 1)}`
); );
return `{ ${parts.join(', ')} }`; if (parts.length <= 1) return `{ ${parts.join(', ')} }`;
const inner = parts.map(p => `${' '.repeat(indent + 1)}${p}`).join(',\n');
return `{\n${inner}\n${i}}`;
case 'lambda': { case 'lambda': {
const params = ast.params.join(' '); const params = ast.params.join(' ');
@ -228,27 +233,27 @@ export function prettyPrint(ast: AST, indent = 0): string {
} }
case 'record-access': case 'record-access':
return `${prettyPrint(ast.record, 0)}.${ast.field}`; return `${prettyPrint(ast.record, indent)}.${ast.field}`;
case 'record-update': { case 'record-update': {
const updates = Object.entries(ast.updates) const updates = Object.entries(ast.updates)
.map(([k, v]) => `${k} = ${prettyPrint(v, 0)}`) .map(([k, v]) => `${k} = ${prettyPrint(v, indent)}`)
.join(', '); .join(', ');
return `${prettyPrint(ast.record, 0)}.{ ${updates} }` return `${prettyPrint(ast.record, indent)}.{ ${updates} }`
} }
case 'match': case 'match':
const expr = prettyPrint(ast.expr, 0); const expr = prettyPrint(ast.expr, indent);
const cases = ast.cases const cases = ast.cases
.map(c => `${i}| ${prettyPrintPattern(c.pattern)} \\ ${prettyPrint(c.result, indent + 1)}`) .map(c => `${i}| ${prettyPrintPattern(c.pattern)} \\ ${prettyPrint(c.result, indent + 1)}`)
.join('\n'); .join('\n');
return `${expr}\n${cases}`; return `${expr}\n${cases}`;
case 'rebind': case 'rebind':
return `${prettyPrint(ast.target, 0)} := ${prettyPrint(ast.value, 0)}`; return `${prettyPrint(ast.target, indent)} := ${prettyPrint(ast.value, indent)}`;
case 'list-spread': case 'list-spread':
return `...${prettyPrint(ast.spread, 0)}`; return `...${prettyPrint(ast.spread, indent)}`;
case 'definition': case 'definition':
return `${ast.name} = ${prettyPrint(ast.body, indent)};`; return `${ast.name} = ${prettyPrint(ast.body, indent)};`;

@ -172,9 +172,9 @@ os =
result = eval input; result = eval input;
_ = debug "onSelect eval result" result; _ = debug "onSelect eval result" result;
result result
| Value v \ openWindow input (_ \ ui.text { content = show v, color = "white" }) | Value v \ (getSource input == ""
| Defined name \ openWindow name (size \ inspector { name = name, w = size.w, h = size.h }) | True \ openWindow input (_ \ ui.text { content = show v, color = "white" })
| Err msg \ openWindow "Error" (_ \ ui.text { content = msg, color = "red" }); | False \ openWindow input (size \ inspector { name = input, w = size.w, h = size.h }));
handleFocusLeftEvent = state \ handleFocusLeftEvent = state \
newIndex = max 0 (osState.wm.focusedIndex - 1); newIndex = max 0 (osState.wm.focusedIndex - 1);

Loading…
Cancel
Save