Skip to content

Edit Engine

@plyy/astro-edit-engine (packages/astro-edit-engine) turns visual edits into .astro source rewrites. It parses files with @astrojs/compiler, so edits land at the exact source location Astro’s dev-server annotations point to. See Visual Editing for how it fits into the editor flow.

  • readElementAt / readElementFromAnnotation — resolve an element (ElementInfo) from a file plus source location or data-astro-source-* annotation
  • applyEdit(source, op) — apply an EditOperation (text, classes, wrap, delete, …) and return an EditResult with the rewritten source
  • findComponentUsage — trace where a component is used across the project

The suite runs with bun test.

The package exposes a Bun CLI (src/cli.ts) with four commands:

Terminal window
astro-edit read <file> <loc> # element info at an annotation location (JSON)
astro-edit usage <file> <componentPath> # component usage info (JSON)
astro-edit edit <file> '<opJson>' # apply an EditOperation, write the file
astro-edit serve # daemon mode (see below)

astro-edit serve reads one JSON request per stdin line and writes one JSON response per stdout line. This keeps the process — and the compiler’s WASM — warm, so editor operations cost about a millisecond instead of a full process spawn. The desktop app keeps a persistent serve process alive (src-tauri/src/engine.rs) and routes its edit commands through it.