Skip to content

feat(docs): runnable example REPL per stdlib entry - #94

Merged
vito merged 8 commits into
mainfrom
docs-feedback-stdlib-example-repls
Jun 5, 2026
Merged

vito merged 8 commits into
mainfrom
docs-feedback-stdlib-example-repls

Conversation

@vito

@vito vito commented Jun 5, 2026 •

Copy link
Copy Markdown
Owner

What

Docs feedback: "it would be great if each stdlib function had a runnable example."

This expands the Builtin DSL so each function/method can declare example usage, and uses that metadata to render a pre-seeded, runnable REPL for every entry on the stdlib reference page.

How

  • DSL — BuiltinDef gains an Example field and an .Example(code) builder on the function, method, and static-method builders.
  • Content — every one of the ~56 builtins now declares a one-line example a reader would actually type ("a,b,c".split(","), [1, 2, 3].map { x => x * 2 }, Random.int(1, 7), …), written in the core language so it runs in the browser REPL.
  • Reference — each generated card renders its example beneath the signature/description, chroma-highlighted at build time. It's a lazy REPL: a static snippet until the reader presses Run, at which point playground.js upgrades it in place into a live REPL seeded with the code and evaluates it once (state then carries across entries, like the CLI). Deferring keeps a page full of examples cheap — no textareas or wasm until something is actually run, and it degrades to a plain code block without JavaScript.
  • Tests — pkg/dang/stdlib_examples_test.go fails if any builtin lacks an example, and parses + type-checks + evaluates every example through the same path as the docs REPL, so a snippet can never drift from the implementation.

The page stays scannable (signature + description + one tiny example), matching the section's > Meta intent, rather than stacking ~56 always-on editors.

Notes

go test ./pkg/dang/ passes. The two tests/ failures in this worktree (TestNeovimHighlights, TestZedHighlightQueryCompatibility) are pre-existing — they read untracked editors/ files absent here — and are unrelated to this change.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

vito added 4 commits June 5, 2026 15:53
Add an Example field to BuiltinDef and an .Example(code) builder method
on the function, method, and static-method builders. The string is a
tiny, self-contained snippet of Dang that exercises the builtin; the
docs reference renders it as a pre-seeded, runnable REPL.

Document the new field in the builtin-dsl skill, including the rule that
every builtin must declare one and the backtick-escaping needed when an
example contains a Dang regex literal.

Signed-off-by: Alex Suraci <[email protected]>
Give every stdlib function, method, and static method an .Example(...):
a one-line snippet a reader would actually type, evaluated in the core
language so it runs in the browser REPL without GraphQL imports.

Two tests in stdlib_examples_test.go keep these honest: one fails if any
builtin lacks an example, the other parses, type-checks, and evaluates
each example through the same path as the docs REPL, so a snippet can
never drift from the implementation it documents.

Signed-off-by: Alex Suraci <[email protected]>
Each generated stdlib card now carries its builtin's example beneath the
signature and description, chroma-highlighted at build time so it reads
as a normal code block and still works without JavaScript.

The example is a lazy REPL: it stays a static snippet until the reader
presses Run, at which point playground.js upgrades it in place into a
live REPL seeded with the code and evaluates it once. Deferring keeps a
page full of examples cheap — no textareas or wasm until something is
actually run. highlightSignature becomes the more general highlightDang,
now shared by the signature cards and the examples.

Signed-off-by: Alex Suraci <[email protected]>
The stdlib example chrome already draws a border and rounded corners
around each block, but the inner pre.dang-repl-fallback inherited its
own border and border-radius from the general pre rule, doubling up the
outline. Reset border and border-radius to none so the snippet relies on
the surrounding chrome.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the docs-feedback-stdlib-example-repls branch from 0f0f1ce to 8d81fda Compare June 5, 2026 15:59
vito added 4 commits June 5, 2026 16:23
The lazy stdlib example REPLs evaluate their seed synchronously on Run,
before loadTreeSitter() resolves, so the first transcript entry was
rendered as plain text and never repainted — the example appeared to
lose its syntax highlighting on Run. Re-highlight existing transcript
entries (not just the live input) when tree-sitter finishes loading.

Signed-off-by: Alex Suraci <[email protected]>
The stdlib reference is the one page with no eager REPL or playground, so
its lazy examples are the first thing to load tree-sitter — and they
build and run the seed synchronously on Run, before highlighting is
ready, leaving the first transcript entry unpainted. Warm the shared
highlighter when the reader hovers or focuses the Run button so it is
ready by click time. The page stays lazy: nothing loads until the reader
reaches for Run.

Signed-off-by: Alex Suraci <[email protected]>
The input kept showing the submitted source for the whole duration of
evaluation (while it read "Running…"), since it was only cleared in the
loadDang().then callback. Clear it right after the source is captured
and echoed into the transcript, so the prompt empties immediately on
submit instead of after the eval returns.

Signed-off-by: Alex Suraci <[email protected]>
The REPL input now keeps a per-session history of submitted sources.
ArrowUp recalls older entries and ArrowDown walks back toward the live
draft, which is preserved so it returns intact. Recall only triggers when
the caret is on the first line (up) or last line (down), so multi-line
editing still moves the caret normally. Reset clears the history.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit f0e856e into main Jun 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant