Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {transpileJavaScript} from "@observablehq/notebook-kit";
import {Runtime} from "@observablehq/runtime";
import inspector from "object-inspect";
import {parse} from "acorn";
import {group} from "d3-array";
import {group, groups} from "d3-array";
import {dispatch as d3Dispatch} from "d3-dispatch";
import * as stdlib from "./stdlib.js";
import {OUTPUT_MARK} from "./constant.js";
Expand Down Expand Up @@ -46,12 +46,16 @@ function inspect(value, {limit = 200, quote = "double", indent = null} = {}) {
return string;
}

function format(value, options) {
const string = inspect(value, options);
function embed(string) {
const lines = string.split("\n");
return lines.map((line) => `${PREFIX} ${line}`).join("\n");
}

function format(value, options) {
const string = inspect(value, options);
return embed(string);
}

export function createRuntime(initialCode) {
let code = initialCode;
let prevCode = null;
Expand All @@ -71,10 +75,15 @@ export function createRuntime(initialCode) {
for (const node of nodes) {
const start = node.start;
const {values} = node.state;
if (values.length) {
const output = values.map(({value, options}) => format(value, options)).join("\n") + "\n";
changes.push({from: start, insert: output});
const groupValues = groups(values, (v) => v.options?.key);
let output = "";
for (let i = 0; i < groupValues.length; i++) {
const [key, values] = groupValues[i];
const f = values.map(({value, options}) => inspect(value, options));
output += key === undefined ? f : key + ": " + f;
output += i === groupValues.length - 1 ? "" : "\n";
}
if (output) changes.push({from: start, insert: embed(output) + "\n"});
}

dispatch(changes);
Expand Down
18 changes: 18 additions & 0 deletions test/js/echo-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const echoKey = `binarySearch('g', ["a", "b", "c", "d"], echo);

function binarySearch(key, array, echo=() => {}) {
let lo = 0;
let hi = array.length - 1;
while (lo <= hi) {
const mi = Math.floor((lo + hi) / 2);
const val = array[mi];
echo(lo, {key: "lo"});
echo(hi, {key: "hi"});
echo(mi, {key: "mi"});
echo(val, {key: "val"});
if (val < key) lo = mi + 1;
else if (val > key) hi = mi - 1;
else return mi;
}
return -1;
}`;
1 change: 1 addition & 0 deletions test/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export {mandelbrotSet} from "./mandelbrot-set.js";
export {matrixRain} from "./matrix-rain.js";
export {jsDocString} from "./js-doc-string.js";
export {commentLink} from "./comment-link.js";
export {echoKey} from "./echo-key.js";
Loading