fastframe-ui-editor
A code editor for fastframe-ui: line numbers, syntax
colours for a handful of common languages, the current line highlighted,
and find, in the theme’s colours. It is egui’s own multi-line TextEdit
with a small highlighter, so the cursor, selection, clipboard, undo and IME
work as in any egui field.
It adds no dependencies; it is a crate of its own so apps that never edit code skip its code.
[dependencies]
fastframe-ui-editor = { git = "https://github.com/crmne/fastframe", tag = "v0.19.0" }
Use
use fastframe_ui_editor::{CodeEditor, Language};
fn editor(ui: &mut egui::Ui, code: &mut String) {
CodeEditor::new(code)
.language(Language::from_extension("rs"))
.max_height(400.0)
.show(ui);
}
Ctrl+F (Cmd+F on a Mac) in the editor opens the find bar, as does
.open_find(true) from a menu: Enter and Shift+Enter move between matches,
Escape closes it. .readonly(true) keeps the text selectable and
searchable but unchanged. Lines do not wrap; the editor scrolls both ways
with its line numbers fixed.
Languages: Rust, Python, Ruby, JavaScript and TypeScript, JSON, TOML, YAML and shell. The highlighter recognises comments, strings, numbers, keywords, constants, types, calls and keys by each language’s few rules, not a grammar, which is what an editor’s colours need.
Styles
A CodeEditorStyle picks the colours: one per kind of token, and the
background, gutter, current line and find highlights. egui’s TextEdit
draws the text and the editor lays out the rest, so a style is a few lines.
code_editor_styles::Themed (the default) takes the syntax colours from
the theme’s hues; code_editor_styles::Monochrome leaves code in the
foreground with comments muted. Set one with .style(&...), or for a part
of the window with ui.code_editor_style(..., |ui| ...).
Why its own highlighter
Measured on the browser build of the fastframe gallery (8,235,946 bytes, 2,873,668 gzipped, before any editor), with each option used by one story. The first three rows only colour a snippet; the last is the whole editor:
| Option | Adds | Adds, gzipped |
|---|---|---|
egui_extras’ syntax_highlighting with syntect |
1,482,534 bytes | 736,929 bytes |
egui_extras’ syntax_highlighting without syntect |
38,372 bytes | 7,906 bytes |
| egui_code_editor 0.5 | 56,359 bytes | 18,393 bytes |
| this crate: the whole editor (gutter, find bar, current line) and its gallery story with seven sample files | 69,659 bytes | 15,743 bytes |
syntect colours more languages better but costs more than a megabyte for
the browser. egui_extras’ fallback highlighter is small, but it is a
private detail of egui_extras: it gives back a finished LayoutJob in its
own CodeTheme, with no token kinds to theme or to map onto fastframe’s
colours, and only strings, keywords, comments and calls. egui_code_editor
brings its own themes and frame, re-tokenises the text into a Vec every
frame, and depends on opener to open links it finds in the code, which
starts processes. A highlighter of a few hundred lines, cached until the
text changes, is lighter than all three and themes cleanly.
Licence
MIT.
Related crates
Draws the interface, like this one:
- egui components and widgets: Buttons, tables, dialogs, pickers and the rest, with four looks, 21 bundled theme files, light and dark.
- Dockable panels: Drag tabs to move, split or float them, drawn in the look and theme of the rest.
- Diff viewer: Unified or side by side, with word highlights and folded lines, fast on long files.
All 27 fastframe crates, by what they do for an app.