Better pretty printing. scrolling in the textEditor

master
Dustin Swan 13 hours ago
parent f1ff6c29d6
commit f54b8ca65e
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -207,7 +207,8 @@ export function prettyPrint(ast: AST, indent = 0): string {
return `(${func} ${args})` return `(${func} ${args})`
case 'let': case 'let':
return `${ast.name} = ${prettyPrint(ast.value, indent + 1)};\n${i}${prettyPrint(ast.body, indent)}` const sep = indent === 0 ? '\n\n' : '\n';
return `${ast.name} = ${prettyPrint(ast.value, indent + 1)};${sep}${i}${prettyPrint(ast.body, indent)}`
case 'list': { case 'list': {
const elems = ast.elements.map(e => prettyPrint(e, indent + 1)) const elems = ast.elements.map(e => prettyPrint(e, indent + 1))

@ -138,6 +138,11 @@ textEditor = name \
}, },
update = state event \ event update = state event \ event
| Scrolled delta \ (
newY = max 0 (state.scrollY + delta.deltaY);
newX = max 0 (state.scrollX + delta.deltaX);
{ state = state.{ scrollY = newY, scrollX = newX }, emit = [] })
| Key { key = "ArrowDown" } \ downArrow state | Key { key = "ArrowDown" } \ downArrow state
| Key { key = "j" } \ (state.mode | Key { key = "j" } \ (state.mode
| Insert \ insertChar "j" state | Insert \ insertChar "j" state
@ -200,27 +205,46 @@ textEditor = name \
| _ \ { state = state, emit = [] }, | _ \ { state = state, emit = [] },
view = state emit \ view = state emit \
buffer = map (l \ text l) state.lines;
scale = 2; scale = 2;
charH = 12; charH = 12;
charW = 5; charW = 5;
lineGap = 1; lineGap = 1;
charGap = 2; charGap = 2;
lineH = charH * scale + lineGap;
cursorY = state.cursorRow * lineH;
scrollY = (cursorY < state.scrollY
| True \ cursorY
| False \ (cursorY + lineH > state.scrollY + ctx.h
| True \ cursorY + lineH - ctx.h
| False \ state.scrollY));
cursorX = state.cursorCol * charW * scale + charGap * state.cursorCol;
scrollX = (cursorX < state.scrollX
| True \ cursorX
| False \ (cursorX + charW * scale > state.scrollX + ctx.w
| True \ cursorX + charW * scale - ctx.w
| False \ state.scrollX));
buffer = map (l \ text l) state.lines;
cursor = ui.positioned { cursor = ui.positioned {
x = state.cursorCol * charW * scale + charGap * state.cursorCol, x = cursorX,
y = state.cursorRow * charH * scale + lineGap * state.cursorRow, y = cursorY,
child = ui.rect { w = charW * scale, h = charH * scale, color = "rgba(255,255,255,0.5)" } child = ui.rect { w = charW * scale, h = charH * scale, color = "rgba(255,255,255,0.5)" }
}; };
maxLineLen = fold (acc line \ max acc (len line)) 0 state.lines;
totalWidth = maxLineLen * charW * scale + maxLineLen * charGap;
totalHeight = len state.lines * lineH;
scrollable { scrollable {
w = ctx.w, w = ctx.w,
h = ctx.h, h = ctx.h,
totalWidth = 1000, totalWidth = totalWidth,
totalHeight = 1000, totalHeight = totalHeight,
scrollX = state.scrollX, scrollX = scrollX,
scrollY = state.scrollY, scrollY = scrollY,
child = ui.stack { child = ui.stack {
children = [ children = [
ui.column { ui.column {

@ -65,7 +65,7 @@ treeNode = config \
valueLabel = value \ value valueLabel = value \ value
| NumberValue n \ Some (show n) | NumberValue n \ Some (show n)
| StringValue n \ Some ("\"" & n & "\"") | StringValue n \ Some ("\"" & r & "\"")
| ConstructorValue { tag = tag } \ Some tag | ConstructorValue { tag = tag } \ Some tag
| FunctionValue _ \ Some "<fn>" | FunctionValue _ \ Some "<fn>"
| _ \ None; | _ \ None;
@ -105,7 +105,7 @@ treeNode = config \
h = 30, h = 30,
onSubmit = onSubmit onSubmit = onSubmit
} }
| False \ simple config.path (config.prefix & "\"" & n & "\"") "#f6a" (Some (_ \ config.onEditLeaf config.path))) | False \ simple config.path (config.prefix & "\"" & (slice n 0 30) & "..\"") "#f6a" (Some (_ \ config.onEditLeaf config.path)))
| ConstructorValue { tag = tag } \ (isEditing | ConstructorValue { tag = tag } \ (isEditing
| True \ textInput { | True \ textInput {

Loading…
Cancel
Save