Code

Loading Ebi…
type PreviewEdit = "type" | "morph"
let effect: PreviewEdit = "type"
let pattern = r"0[0-9]"
let replacement = "04"
mut row = 1
mut column = 1
mut cursor_shape: "block" | "bar" = "block"
sig cursors: List<TextCursor> = [TextCursor(row, column, shape=cursor_shape)]
fn highlight(radius: Length): CodeTheme {
    return CodeTheme(selection=Color("#3584e470"), inactive_opacity=0.65, selection_radius=radius)
}

let view = <Code lang="python" width={84%} h-134 font_size={13pt} line_height={18pt}
    theme={highlight(0pt)} cursors={cursors} line_numbers={true} scope_indicators={true}
    position={[0pt, -14pt]} x_align={Align.Center} y_align={Align.Center}>
    def summarize(items):
        total = sum(items)
        return f"{total:02}"

    def report(items):
        label = summarize(items)
        return f"Total: {label}"
</Code>
let command_hint = <Text font={Font(["monospace"])} font_size={14pt} fill="blue2"
    position={[0pt, 81pt]} x_align={Align.Center} y_align={Align.Center}>NORMAL</Text>
view
command_hint
wait(450ms)
command_hint.edit("3Gf0v")
row = 3
column = 21
view.range(regex="0", within=view.range(name="summarize")).select(duration=250ms, ease="smooth")
wait(350ms)
command_hint.edit("l")
column = 22
view.range(regex=pattern).select(duration=250ms, ease="smooth")
view.style(theme=highlight(2pt), duration=500ms, ease="smooth")
wait(400ms)
command_hint.edit("<Esc>")
view.select(duration=200ms)
command_hint.edit(":%s/" + pattern + "/" + replacement + "/")
view.range(regex=pattern).select(duration=250ms, ease="smooth")
wait(650ms)
cursor_shape = "bar"
view.range(regex=pattern).edit(code=replacement, duration=750ms, animation=effect)
wait(400ms)
cursor_shape = "block"
command_hint.edit("ggzc")
row = 1
column = 1
view.range(name="summarize").fold(duration=150ms, ease="smooth")
wait(650ms)
command_hint.edit("zo")
view.range(name="summarize").unfold(duration=150ms, ease="smooth")
wait(900ms)

The preview follows a Vim editing sequence. v starts a characterwise selection, l extends it, :%s replaces the regex match, and zc and zo close and open the summarize fold. Change replacement or try effect = "morph" in the source.

Give inline source a lang and pass it as code={source} or as a literal XML body. Use src="file.py" to load a file relative to the scene source. Fixed dimensions let you scroll or wrap long lines.

A Code defaults to monospace. Text defaults to sans-serif.

Both accept a list of cursors. Lines and columns start at 1. A "bar" cursor marks an insertion point. A "block" cursor covers the next character by default. Set color, width, or radius to customize either shape.

<Text cursors={[
    TextCursor(1, 1, shape="block", color=Color("#62a0ea66")),
    TextCursor(1, 6, width=2pt, radius=1pt),
]}>Ready</Text>

Patch and edit

CodeStyle, CodeTheme and CodeSpan name the records returned by their same-named constructors. Text.edit and Code.edit default to 0s. Folding and unfolding default to 100ms. Other timed methods default to 300ms. Set ease to change the pacing of a timed edit. It defaults to "spring".

view.edit(text?: String, font?: Font, animation: EditAnimation = "morph"): Animation
code.patch(patch?: String, src?: String, animation: EditAnimation = "morph",
           file?: String, range?: CodeRange): Animation
selection.edit(code?: String, src?: String, animation: EditAnimation = "morph",
               file?: String): Animation

Use edit(text, font=...) on a Text or Code to replace its content on the timeline. Use edit(font=...) to change the font and keep the current text. For text driven by a signal, bind the font to a signal to change it over time. A Code highlights the replacement in its current language. Use selection.edit(code=...) to replace a source range, or pass a unified diff string to code.patch(patch=...). Either method accepts a replacement or patch file through src=... in place of inline content. Specify file when the diff contains changes for multiple files.

  • type keeps the shared prefix of a changed run, backspaces to the first difference, then types the replacement.
  • morph moves surviving text and fades the characters that enter or leave.

EditAnimation is "type" | "morph". Timed methods take an Easing such as "linear" or "smooth".

Select a range

code.range(name?: String, level?: Number, line?: Number, end_line?: Number,
           column?: Number, character?: Number, length?: Number,
           regex?: String, occurrence?: Number, within?: CodeRange): CodeRange
code.ranges(name?: String, level?: Number, line?: Number, end_line?: Number,
            column?: Number, character?: Number, length?: Number,
            regex?: String, within?: CodeRange): List<CodeRange>

range needs a selector and a unique match. ranges returns all matches, possibly empty; occurrence is only available on range.

SelectorMeaning
name="WorkQueue.push"Qualified declaration name; a unique bare name also works.
level=2Syntax depth.
line=24, end_line=28Source line interval.
line=24, column=8, length=5Text within a source line.
character=120, length=8Text at a global character offset.
regex="return [0-9]+", occurrence=2A regex match; use raw strings for regex braces.
within=code.range(name="WorkQueue")Restrict another selector to this interval.

Lines, columns, characters and occurrences start at 1. The end_line selector includes that line; returned CodeRange endpoints are exclusive. Bound ranges belong to one view/revision. Query again after a patch; inline chains resolve when their animation is placed.

Fold and style

let alternate: "fade" | "morph" = "fade"
let view = <Code lang="python" width={86%} h-114 font_size={12pt}
    line_numbers={true} scope_indicators={true}
    x_align={Align.Center} y_align={Align.Center}>
    def total(x, y):
        result = x + y
        return result

    print(total(2, 3))
</Code>
let scope = view.range(name="total")
view
wait(500ms)
scope.fold(duration=300ms, ease="linear")
wait(400ms)
scope.unfold(duration=300ms, ease="linear")
wait(500ms)
scope.fold(animation=alternate, duration=300ms, ease="linear")
wait(400ms)
scope.unfold(animation=alternate, duration=300ms, ease="linear")
wait(500ms)

The first fold and unfold use "collapse" and "expand" by default. The body’s visible height changes as the lines below move. The second pair uses "fade" to crossfade the layouts. Try alternate = "morph" to move surviving text and fade the changed pieces.

selection.fold(animation: "collapse" | "morph" | "fade" = "collapse"): Animation
selection.unfold(animation: "expand" | "morph" | "fade" = "expand"): Animation
selection.select(): Animation
selection.scroll_to(align: "nearest" | "start" | "center" | "end" = "nearest"): Animation
selection.style(style: CodeStyle): Animation
code.select(ranges?: List<CodeRange>): Animation
code.style(spans?: List<CodeSpan>, theme?: CodeTheme,
           line_numbers?: Bool, scope_indicators?: Bool): Animation

Folds affect one scope independently. A line or regex selection chooses its innermost containing scope. The same animation options work with code.fold(range=selection) and code.unfold(range=selection).

CodeStyle accepts fill, background, opacity, bold, italic, underline and strike; CodeTheme applies named capture rules, and CodeSpan overrides explicit source ranges. Later spans win. code.select() clears selection; code.style(spans=[]) clears span overrides.

Set selection on CodeTheme to control the highlight color and alpha, and selection_radius to control its corner radius. Use code.style(theme=..., duration=...) to animate either change while a range is selected. Set inactive_opacity=1 to keep the surrounding code at full opacity.

Line numbers and scope indicators default to hidden. Animate them with code.style(line_numbers=true, scope_indicators=true). Scope chevrons rotate with folding. A signal-owned property must be changed through its input, instead of a second style/patch writer.

Bundled languages: Ebi, Python, Rust, JavaScript, TypeScript, TSX, C/C++, JSON, Bash, TOML, YAML, SQL and plain text. Invalid displayed code remains visible with syntax diagnostics.