A code editor for the browser, written in Rust

Rustaco edits code in a web page or a native window. The text model, the commands and the layout run in Rust, compiled to WebAssembly for the browser; syntax highlighting and the language services run in a Web Worker. The editor below is Rustaco itself, loaded from this page.

Ctrl+Space completes, F12 goes to a definition, F2 renames a local, Ctrl+Shift+O lists the symbols.

Size and speed

The figures come from the benchmarks in the repository (docs/performance.md), measured on a Windows desktop in Chromium, Firefox and WebKit.

368 KB The editor module, gzipped. Each language module (235 to 630 KB) downloads the first time a file needs it.
0.5 ms Main-thread time for a keystroke in a 10 MB file while parsing and a search run in the worker.
21 Languages: 17 through tree-sitter, SQL, Kotlin and Dockerfile through TextMate grammars, and Mermaid.

Against Monaco 0.57 on the same files, Rustaco loads as fast and its cost per keystroke does not grow with the file. In that comparison it still spent about one and a half times Monaco's main-thread time per keystroke, most of it in drawing the frame.

What the editor does

The feature set follows Monaco as a code editor. The API keeps Monaco's concepts (models, editors, decorations, providers) with char offsets in place of line and column pairs.

Editing

Multiple cursors and column selection, folding from the syntax tree, word wrap, a minimap, sticky scroll, find and replace with regular expressions, snippets with tab stops, IME input and a screen reader mirror. Keymaps use VS Code's command ids; a JetBrains preset is included.

Languages and LSP

Every bundled grammar also runs as a language server: completion from the grammar's keywords and the file's names, go to definition, references and rename within the file. Real language servers connect over a WebSocket through the LSP client, which is written in Rust and runs in the editor's WebAssembly module.

Diff editor

Side by side or inline, both sides editable, with char-level changes and moved blocks.

Rust crates

The text model, the layout, the highlighting and the LSP client are separate crates on crates.io and build without a browser. A native frontend on winit draws the same frames; it is younger than the web one and stays at version 0.x.

Install

The npm package holds the ES modules, the WebAssembly files and the TypeScript types. The crates follow semantic versioning from 1.0.0.

npm install rustaco
import { createEditor } from 'rustaco';

const editor = await createEditor(document.getElementById('editor'), {
  value: 'fn main() {}\n',
  language: 'rust',
});
cargo add rustaco-core rustaco-view rustaco-syntax rustaco-lsp

Only monospace fonts are supported: text sits on a grid of equal cells. Right-to-left text and touch input do not work yet. The guides for embedding in a page or a native application are in the repository, under https://github.com/object-pl/rustaco/tree/main/docs.