mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
748 lines
30 KiB
HTML
748 lines
30 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>structure-editors</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="alternate" type="text/markdown" href="/structure-editors.md">
|
|
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/structure-editors">
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/structure-editors.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>structure-editors</h1><p class="lede">Structure editor design patterns - projectional editing, syntax-directed editing, edit actions, cursor management, rendering, text workflow integration</p><h1>Structure Editors: Design Patterns</h1>
|
|
<p><strong>Scope</strong>: Projectional editing, edit actions, zipper cursors, rendering, text integration, educational tools <strong>Lines</strong>: ~400 <strong>Last Updated</strong>: 2025-10-25</p>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Activate this skill when:</p>
|
|
<ul><li>Designing structure editors (projectional editors)</li><li>Building syntax-directed development tools</li><li>Creating educational programming environments</li><li>Implementing domain-specific language editors</li><li>Understanding alternatives to text-based editing</li><li>Integrating structure editing with traditional workflows</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>Projectional Editing vs Text Editing</h3>
|
|
<p><strong>Key difference</strong>: Edit AST directly vs edit text and parse</p>
|
|
<pre><code class="lang-python">from dataclasses import dataclass
|
|
from typing import Union, List, Optional
|
|
|
|
# Text editor approach
|
|
class TextEditor:
|
|
"""
|
|
Traditional text editing:
|
|
|
|
1. User types characters
|
|
2. Editor stores as string
|
|
3. Parser parses on demand (or continuously)
|
|
4. Show syntax errors
|
|
|
|
Problems:
|
|
- Intermediate states invalid ("if tr")
|
|
- Parser errors cryptic
|
|
- Lost productivity (syntax errors block progress)
|
|
"""
|
|
text: str
|
|
cursor_position: int
|
|
|
|
def insert_char(self, ch: str):
|
|
"""Insert character at cursor"""
|
|
self.text = (self.text[:self.cursor_position] +
|
|
ch +
|
|
self.text[self.cursor_position:])
|
|
self.cursor_position += 1
|
|
|
|
def parse(self) -> Optional['AST']:
|
|
"""Try to parse - may fail!"""
|
|
try:
|
|
return parse(self.text)
|
|
except SyntaxError:
|
|
return None # Invalid!
|
|
|
|
# Structure editor approach
|
|
class StructureEditor:
|
|
"""
|
|
Projectional editing:
|
|
|
|
1. User performs edit actions
|
|
2. Editor stores as AST
|
|
3. Project AST to display
|
|
4. Always valid!
|
|
|
|
Benefits:
|
|
- Always syntactically correct
|
|
- Can't introduce syntax errors
|
|
- Type-directed editing (know what's valid)
|
|
"""
|
|
ast: 'Expr'
|
|
cursor: 'ZContext' # Zipper
|
|
|
|
def construct_if(self):
|
|
"""
|
|
Construct if-expression
|
|
|
|
Before: ⦇⦈ (hole)
|
|
After: if ⦇⦈ then ⦇⦈ else ⦇⦈
|
|
|
|
Always valid AST!
|
|
"""
|
|
self.ast = IfExpr(
|
|
EmptyHole(),
|
|
EmptyHole(),
|
|
EmptyHole()
|
|
)
|
|
|
|
def project(self) -> str:
|
|
"""Project AST to text for display"""
|
|
return pretty_print(self.ast)
|
|
|
|
print("Structure editing: AST → Display (always valid)")</code></pre>
|
|
<h3>Edit Actions and Edit Contexts</h3>
|
|
<p><strong>Edit actions</strong>: Operations on AST that preserve well-formedness</p>
|
|
<pre><code class="lang-python">from enum import Enum
|
|
|
|
class EditAction(Enum):
|
|
# Movement
|
|
MOVE_PARENT = "parent"
|
|
MOVE_CHILD_1 = "child1"
|
|
MOVE_CHILD_2 = "child2"
|
|
|
|
# Construction
|
|
CONSTRUCT_NUM = "num"
|
|
CONSTRUCT_VAR = "var"
|
|
CONSTRUCT_LAM = "lam"
|
|
CONSTRUCT_AP = "ap"
|
|
CONSTRUCT_IF = "if"
|
|
CONSTRUCT_PLUS = "plus"
|
|
|
|
# Deletion
|
|
DELETE = "del"
|
|
|
|
# Refinement
|
|
FINISH = "finish" # Remove hole wrapper
|
|
|
|
class EditContext:
|
|
"""
|
|
Context for edit actions
|
|
|
|
Determines which actions are valid at cursor
|
|
"""
|
|
|
|
@staticmethod
|
|
def available_actions(cursor_expr: 'Expr', expected_type: 'Type') -> List[EditAction]:
|
|
"""
|
|
Which actions are valid at current position?
|
|
|
|
Depends on:
|
|
- What's under cursor
|
|
- Expected type
|
|
- Surrounding context
|
|
"""
|
|
actions = []
|
|
|
|
# Always can move (if not at root)
|
|
actions.append(EditAction.MOVE_PARENT)
|
|
|
|
# If empty hole: can construct anything
|
|
if isinstance(cursor_expr, EmptyHole):
|
|
actions.extend([
|
|
EditAction.CONSTRUCT_NUM,
|
|
EditAction.CONSTRUCT_VAR,
|
|
EditAction.CONSTRUCT_LAM,
|
|
EditAction.CONSTRUCT_IF,
|
|
])
|
|
|
|
# If has children: can move down
|
|
if isinstance(cursor_expr, (Lam, Ap, If, Plus)):
|
|
actions.append(EditAction.MOVE_CHILD_1)
|
|
if isinstance(cursor_expr, (Ap, If, Plus)):
|
|
actions.append(EditAction.MOVE_CHILD_2)
|
|
|
|
# Can always delete (replace with hole)
|
|
actions.append(EditAction.DELETE)
|
|
|
|
return actions
|
|
|
|
print("Edit actions: Only valid operations available")</code></pre>
|
|
<h3>Cursor Management with Zippers</h3>
|
|
<p><strong>Zipper</strong>: Navigate AST efficiently</p>
|
|
<pre><code class="lang-python">@dataclass
|
|
class Zipper:
|
|
"""
|
|
Zipper: Focus on one node, remember path back
|
|
|
|
Components:
|
|
- Focus: Current subtree under cursor
|
|
- Context: Path back to root (one-hole contexts)
|
|
"""
|
|
focus: 'Expr'
|
|
context: 'Context'
|
|
|
|
def go_up(self) -> Optional['Zipper']:
|
|
"""Move cursor to parent"""
|
|
match self.context:
|
|
case Top():
|
|
return None # Already at root
|
|
case Parent(parent_type, siblings, parent_context):
|
|
# Plug hole in parent
|
|
new_focus = reconstruct(parent_type, self.focus, siblings)
|
|
return Zipper(new_focus, parent_context)
|
|
|
|
def go_down(self, child_index: int) -> Optional['Zipper']:
|
|
"""Move cursor to nth child"""
|
|
match self.focus:
|
|
case Lam(param, body):
|
|
if child_index == 0:
|
|
new_context = LamContext(param, self.context)
|
|
return Zipper(body, new_context)
|
|
case Ap(func, arg):
|
|
if child_index == 0:
|
|
return Zipper(func, ApFuncContext(arg, self.context))
|
|
elif child_index == 1:
|
|
return Zipper(arg, ApArgContext(func, self.context))
|
|
# ... other cases
|
|
|
|
return None # Invalid child index
|
|
|
|
def replace(self, new_focus: 'Expr') -> 'Zipper':
|
|
"""Replace focused node"""
|
|
return Zipper(new_focus, self.context)
|
|
|
|
# Example: Navigation
|
|
def example_navigation():
|
|
"""
|
|
Expression: (λx. x + 1) 5
|
|
|
|
Zipper positions:
|
|
1. Focus: (λx. x + 1) 5, Context: Top
|
|
2. Focus: λx. x + 1, Context: ApFunc(5, Top)
|
|
3. Focus: x + 1, Context: Lam("x", ApFunc(5, Top))
|
|
4. Focus: x, Context: PlusLeft(1, Lam("x", ApFunc(5, Top)))
|
|
"""
|
|
pass
|
|
|
|
print("Zipper: Efficient navigation with O(1) up/down")</code></pre>
|
|
<h3>Rendering and Layout</h3>
|
|
<p><strong>Project AST to display</strong>: Pretty-printing with cursor indication</p>
|
|
<pre><code class="lang-python">class Renderer:
|
|
"""
|
|
Render AST as text with cursor
|
|
|
|
Considerations:
|
|
- Indentation
|
|
- Parentheses (minimal)
|
|
- Highlighting (syntax, types, cursor)
|
|
- Multi-line layout
|
|
"""
|
|
|
|
def render(self, zipper: Zipper) -> str:
|
|
"""
|
|
Render with cursor indicated
|
|
|
|
Example:
|
|
if ▸true then 1 else 2
|
|
^^^ cursor here
|
|
"""
|
|
# Reconstruct full expression
|
|
full_expr = self.reconstruct_root(zipper)
|
|
|
|
# Pretty print with cursor marker
|
|
return self.pretty_print_with_cursor(full_expr, zipper.focus)
|
|
|
|
def pretty_print_with_cursor(self, expr: 'Expr', cursor_focus: 'Expr') -> str:
|
|
"""
|
|
Pretty print, mark cursor position
|
|
|
|
Uses boxes/layout algorithm
|
|
"""
|
|
match expr:
|
|
case Num(n):
|
|
marker = "▸" if expr == cursor_focus else ""
|
|
return f"{marker}{n}"
|
|
|
|
case Lam(param, body):
|
|
body_str = self.pretty_print_with_cursor(body, cursor_focus)
|
|
marker = "▸" if expr == cursor_focus else ""
|
|
return f"{marker}λ{param}. {body_str}"
|
|
|
|
case Ap(func, arg):
|
|
func_str = self.pretty_print_with_cursor(func, cursor_focus)
|
|
arg_str = self.pretty_print_with_cursor(arg, cursor_focus)
|
|
marker = "▸" if expr == cursor_focus else ""
|
|
return f"{marker}({func_str} {arg_str})"
|
|
|
|
# ... other cases
|
|
|
|
def layout_boxes(self, expr: 'Expr') -> 'Box':
|
|
"""
|
|
Box layout algorithm (Haskell-style)
|
|
|
|
Determine:
|
|
- Horizontal vs vertical layout
|
|
- Indentation
|
|
- Line breaking
|
|
"""
|
|
pass
|
|
|
|
print("Rendering: AST → pretty text with cursor")</code></pre>
|
|
<h3>Selection and Multi-Cursor</h3>
|
|
<p><strong>Selection</strong>: Mark range in AST</p>
|
|
<pre><code class="lang-python">@dataclass
|
|
class Selection:
|
|
"""
|
|
Selection in structure editor
|
|
|
|
Two approaches:
|
|
1. Path-based: (start_path, end_path)
|
|
2. Subtree-based: Select whole subtree
|
|
"""
|
|
root: 'Expr'
|
|
selected_paths: List[List[int]] # Multiple cursors!
|
|
|
|
def delete_selection(self) -> 'Expr':
|
|
"""
|
|
Delete selected nodes
|
|
|
|
Replace each with hole
|
|
"""
|
|
result = self.root
|
|
for path in self.selected_paths:
|
|
result = replace_at_path(result, path, EmptyHole())
|
|
return result
|
|
|
|
def extract_selection(self) -> 'Expr':
|
|
"""
|
|
Extract selection to variable
|
|
|
|
Before: e₁ + (e₂ + e₃) [select e₂ + e₃]
|
|
After: let x = e₂ + e₃ in e₁ + x
|
|
"""
|
|
pass
|
|
|
|
# Multi-cursor editing
|
|
class MultiCursor:
|
|
"""
|
|
Multiple cursors in structure editor
|
|
|
|
Example: Rename all occurrences
|
|
- Select variable x
|
|
- Add cursor at each occurrence
|
|
- Type new name
|
|
- All occurrences updated simultaneously
|
|
"""
|
|
cursors: List[Zipper]
|
|
|
|
def apply_action(self, action: EditAction):
|
|
"""Apply action to all cursors"""
|
|
self.cursors = [self.apply_single(c, action) for c in self.cursors]
|
|
|
|
print("Selection: Subtree-based, supports multi-cursor")</code></pre>
|
|
<h3>Integration with Text Workflows</h3>
|
|
<p><strong>Hybrid approach</strong>: Structure editor with text escape hatch</p>
|
|
<pre><code class="lang-python">class HybridEditor:
|
|
"""
|
|
Combine structure editing with text editing
|
|
|
|
Modes:
|
|
1. Structure mode (default): Edit AST
|
|
2. Text mode: Edit as text, re-parse on commit
|
|
|
|
Users can drop to text for familiar workflow
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.mode = "structure"
|
|
self.ast = EmptyHole()
|
|
self.text_buffer = None
|
|
|
|
def enter_text_mode(self):
|
|
"""
|
|
Switch to text mode
|
|
|
|
- Serialize AST to text
|
|
- Let user edit text freely
|
|
- Parse on exit
|
|
"""
|
|
self.mode = "text"
|
|
self.text_buffer = serialize_to_text(self.ast)
|
|
# User now edits text_buffer
|
|
|
|
def exit_text_mode(self):
|
|
"""
|
|
Return to structure mode
|
|
|
|
- Parse text buffer
|
|
- If success: Update AST
|
|
- If error: Show errors, stay in text mode
|
|
"""
|
|
try:
|
|
new_ast = parse(self.text_buffer)
|
|
self.ast = new_ast
|
|
self.mode = "structure"
|
|
self.text_buffer = None
|
|
except SyntaxError as e:
|
|
# Show error, let user fix
|
|
show_error(e)
|
|
|
|
def copy_as_text(self) -> str:
|
|
"""
|
|
Copy AST as text (for external tools)
|
|
|
|
Enables interop with text-based tools:
|
|
- Version control (git)
|
|
- Code review tools
|
|
- grep, sed, etc.
|
|
"""
|
|
return serialize_to_text(self.ast)
|
|
|
|
print("Hybrid: Structure editing + text mode escape hatch")</code></pre>
|
|
<h3>Educational Use Cases</h3>
|
|
<p><strong>Structure editors for learning</strong>: Reduce cognitive load</p>
|
|
<pre><code class="lang-python">class EducationalEditor:
|
|
"""
|
|
Structure editor for teaching programming
|
|
|
|
Benefits:
|
|
- No syntax errors distract from learning
|
|
- Type-guided construction teaches types
|
|
- Example-driven development workflow
|
|
- Visual execution (step through)
|
|
"""
|
|
|
|
def beginner_mode(self):
|
|
"""
|
|
Beginner mode features:
|
|
|
|
1. Limited action palette
|
|
- Only show simple actions initially
|
|
- Unlock more as learner progresses
|
|
|
|
2. Inline help
|
|
- "This hole expects a number"
|
|
- "You can fill this with: 1, 2, x+1, ..."
|
|
|
|
3. Example-driven
|
|
- Show example inputs/outputs
|
|
- Let learner fill holes to match examples
|
|
|
|
4. Instant feedback
|
|
- Type errors shown immediately
|
|
- Evaluation results live
|
|
"""
|
|
pass
|
|
|
|
def visual_execution(self, program: 'Expr'):
|
|
"""
|
|
Visual step-through execution
|
|
|
|
- Highlight current redex
|
|
- Show evaluation steps
|
|
- Inspect values at each step
|
|
|
|
Helps learners understand evaluation
|
|
"""
|
|
pass
|
|
|
|
print("Educational: Structure editors reduce syntax burden")</code></pre>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Pattern 1: Progressive Disclosure</h3>
|
|
<pre><code class="lang-python">def progressive_disclosure():
|
|
"""
|
|
Show only relevant actions at each point
|
|
|
|
Beginner:
|
|
- construct num
|
|
- construct var
|
|
- construct +
|
|
|
|
Intermediate:
|
|
- + construct if
|
|
- + construct lambda
|
|
|
|
Advanced:
|
|
- + construct let
|
|
- + construct match
|
|
- + construct type
|
|
|
|
Gradually unlock complexity
|
|
"""
|
|
print("Progressive: Start simple, add complexity as needed")</code></pre>
|
|
<h3>Pattern 2: Template-Based Construction</h3>
|
|
<pre><code class="lang-python">def template_construction(template_name: str) -> 'Expr':
|
|
"""
|
|
Common patterns as templates
|
|
|
|
Templates:
|
|
- "if-then-else" → if ⦇⦈ then ⦇⦈ else ⦇⦈
|
|
- "let-in" → let x = ⦇⦈ in ⦇⦈
|
|
- "fold" → fold ⦇⦈ ⦇⦈ ⦇⦈
|
|
|
|
Reduces repetitive construction
|
|
"""
|
|
templates = {
|
|
"if": IfExpr(EmptyHole(), EmptyHole(), EmptyHole()),
|
|
"let": LetExpr("x", EmptyHole(), EmptyHole()),
|
|
}
|
|
return templates.get(template_name, EmptyHole())
|
|
|
|
print("Templates: Common patterns as single action")</code></pre>
|
|
<h3>Pattern 3: Type-Directed Palette</h3>
|
|
<pre><code class="lang-python">def type_directed_palette(expected_type: 'Type', context: dict) -> List[str]:
|
|
"""
|
|
Show only actions that produce expected type
|
|
|
|
Expected: Bool
|
|
Show:
|
|
- true, false (literals)
|
|
- x == y (if x, y : comparable)
|
|
- not ⦇⦈ (constructor)
|
|
- ⦇⦈ && ⦇⦈ (operator)
|
|
|
|
Hide:
|
|
- + (produces Num, not Bool)
|
|
- lambda (produces function)
|
|
"""
|
|
palette = []
|
|
|
|
if expected_type == TBool():
|
|
palette.extend(["true", "false", "not", "&&", "||"])
|
|
|
|
if expected_type == TNum():
|
|
palette.extend(["0", "1", "+", "*", "-"])
|
|
|
|
# Variables of matching type
|
|
for var, typ in context.items():
|
|
if consistent(typ, expected_type):
|
|
palette.append(var)
|
|
|
|
return palette
|
|
|
|
print("Type-directed: Only show valid constructions")</code></pre>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Edit Action Categories</h3>
|
|
<p>| Category | Actions | Purpose | |----------|---------|---------| | Movement | parent, child(n) | Navigate AST | | Construction | num, var, lam, ap, if, + | Build structure | | Deletion | del | Remove (→ hole) | | Refinement | finish, refine | Transform holes |</p>
|
|
<h3>Zipper Operations</h3>
|
|
<pre><code>go_up() : Move to parent (O(1))
|
|
go_down(n) : Move to nth child (O(1))
|
|
replace(e) : Replace focus (O(1))
|
|
reconstruct() : Build root (O(depth))</code></pre>
|
|
<h3>Rendering Strategies</h3>
|
|
<pre><code>Inline: Single line (a + b)
|
|
Block: Multi-line with indentation
|
|
if cond
|
|
then branch1
|
|
else branch2
|
|
Hybrid: Smart line breaking</code></pre>
|
|
<hr>
|
|
<h2>Anti-Patterns</h2>
|
|
<p>❌ <strong>Forcing structure-only</strong>: Users need text mode sometimes ✅ Provide text mode escape hatch</p>
|
|
<p>❌ <strong>Ignoring copy/paste</strong>: Users expect familiar operations ✅ Support clipboard operations (serialize/parse)</p>
|
|
<p>❌ <strong>Poor keyboard navigation</strong>: Mouse-only is slow ✅ Keyboard shortcuts for all actions</p>
|
|
<p>❌ <strong>No visual feedback</strong>: User doesn't know what's valid ✅ Show available actions, highlight cursor</p>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>hazelnut-calculus.md</code> - Formal edit action semantics</li><li><code>typed-holes-foundations.md</code> - Holes in structure editors</li><li><code>live-programming-holes.md</code> - Live programming with structure editors</li><li><code>typed-holes-interaction.md</code> - IDE integration patterns</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/structure-editors.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |