If a node has only one child we were previously greedily positioning it on the left edge of its allocated interval.
Now we look ahead past single-child descendants until we find a branch. Then we align vertically with it.
IYJ2W4KLBV65OP73D5SMFOMTDH2NDDK2REXRKXAWBCVJPWMXT4PQC xposition = function(curr, xlo, xhi)-- return the left margin to position a node atif curr.children == nil or #curr.children == 0 thenreturn xloendif #curr.children == 1 thenreturn xposition(Nodes[curr.children[1]], xlo, xhi)endlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#curr.children-1 dolocal child_id = curr.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal centerx = total_boundaries / (#curr.children-1)return centerx - 620/2end
{"xposition":675,"on.mouse_press":618,"on.draw":632,"box_height":667,"line_height":365,"compute_layout":619,"Viewport":303,"dehtml":663,"update_editor_box":430,"Nodes":593,"vx":5,"on.text_input":587,"on.mouse_release":586,"schema1_of_y":366,"on.update":368,"on.keychord_press":644,"split_lines":469,"to_node":611,"y_of_schema1":364,"A":582,"add_node":650,"scale":7,"render_node_and_descendants":670,"initialize_editor":338,"add_edge":575,"ensure_cursor_node_within_viewport":643,"font":353,"Surface":588,"vy":8,"on.initialize":665,"to_text":180,"B":666,"fw_app":"mastodon-unfurl","Cursor_node":172,"Root":654,"add_thick_line":400,"ntracks":600,"copy_shape":396,"compute_ntracks":598,"fw_parent":674,"on.code_change":578,"load_from_iterator":463,"Input_filename":436,"header":651,"render_thread_to_surface":655,"on":1}
xposition = function(curr, xlo, xhi)-- return the left margin to position a node atprint(curr.id, curr.children)if #curr.children == 0 thenreturn xloendif #curr.children == 1 thenreturn xposition(Nodes[curr.children[1]], xlo, xhi)endlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#curr.children-1 dolocal child_id = curr.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal centerx = total_boundaries / (#curr.children-1)return centerx - 620/2end
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":673,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":670,"xposition":674,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}
xposition = function(curr, xlo, xhi)-- return the left margin to position a node atprint(curr.id, curr.children, #curr.children)if #curr.children == 0 thenreturn xloendif #curr.children == 1 thenreturn xposition(Nodes[curr.children[1]], xlo, xhi)endlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#curr.children-1 dolocal child_id = curr.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal centerx = total_boundaries / (#curr.children-1)return centerx - 620/2end
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":672,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":670,"xposition":673,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}
xposition = function(curr, xlo, xhi)-- return the left margin to position a node atif #curr.children == 0 thenreturn xloendif #curr.children == 1 thenreturn xposition(Nodes[curr.children[1]], xlo, xhi)endlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#curr.children-1 dolocal child_id = curr.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal centerx = total_boundaries / (#curr.children-1)return centerx - 620/2end
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":671,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":670,"xposition":672,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}
xposition = function(curr, xlo, xhi)-- return the left margin to position a node atif #curr.children <= 1 thenreturn xloendlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#curr.children-1 dolocal child_id = curr.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal centerx = total_boundaries / (#curr.children-1)return centerx - 620/2end
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":670,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":670,"xposition":671,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}
render_node_and_descendants = function(id, y, xlo, xhi, grandparent_surface_node)-- draw a parent 'id' and its children-- also draw an edge from grandparent to parent if provided-- position the root between xlo and xhi to be closer to children with fewer trackslocal parent = Nodes[id]if parent.children == nil thenparent.surface_node = add_node(xlo, y, parent, grandparent_surface_node)returnendlocal parent_xlo = xposition(parent, xlo, xhi)parent.surface_node = add_node(parent_xlo, y, parent, grandparent_surface_node)local parent_height = box_height(parent.surface_node)local curr_boundary = xlofor _,child_id in ipairs(parent.children) dolocal child = Nodes[child_id]render_node_and_descendants(child_id, y + parent_height + 50, curr_boundary, curr_boundary + 620*child.ntracks, parent.surface_node)curr_boundary = curr_boundary + 620*child.ntracksendend
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":669,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":670,"xposition":669,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}
xposition = function(curr, xlo, xhi)-- return the left margin to position a node atif #node.children <= 1 thenreturn xloendlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#parent.children-1 dolocal child_id = parent.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal parent_x = total_boundaries / (#parent.children-1)return parent_x - 620/2end
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":668,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":646,"xposition":669,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}
xposition = function(nodes, curr, xlo, xhi)-- return the left margin to position a node atif #node.children <= 1 thenreturn xloendlocal total_boundaries = 0local curr_boundary = xlofor child_idx = 1,#parent.children-1 dolocal child_id = parent.children[child_idx]local child = Nodes[child_id]curr_boundary = curr_boundary + 620 * child.ntrackstotal_boundaries = total_boundaries + curr_boundaryendlocal parent_x = total_boundaries / (#parent.children-1)return parent_x - 620/2end
{"box_height":667,"B":666,"compute_layout":619,"ntracks":600,"on.code_change":578,"add_thick_line":400,"render_thread_to_surface":655,"dehtml":663,"on":1,"update_editor_box":430,"load_from_iterator":463,"fw_parent":667,"on.mouse_release":586,"split_lines":469,"on.update":368,"Surface":588,"on.keychord_press":644,"to_text":180,"ensure_cursor_node_within_viewport":643,"Nodes":593,"y_of_schema1":364,"to_node":611,"header":651,"initialize_editor":338,"Root":654,"add_node":650,"schema1_of_y":366,"Viewport":303,"copy_shape":396,"render_node_and_descendants":646,"xposition":668,"line_height":365,"vx":5,"Cursor_node":172,"scale":7,"on.mouse_press":618,"add_edge":575,"fw_app":"mastodon-unfurl","compute_ntracks":598,"on.draw":632,"Input_filename":436,"on.text_input":587,"on.initialize":665,"vy":8,"A":582,"font":353}