63 lines
1.9 KiB
Rust
63 lines
1.9 KiB
Rust
use eframe::egui;
|
|
use egui_code_editor::{CodeEditor, ColorTheme, Syntax};
|
|
use std::collections::BTreeSet;
|
|
|
|
/// Returns a [`Syntax`] definition for the Rhai scripting language.
|
|
///
|
|
/// Keyword sets are sourced from the official
|
|
/// [rhaiscript/sublime-rhai](https://github.com/rhaiscript/sublime-rhai) grammar (MPL-2.0).
|
|
pub(crate) fn rhai_syntax() -> Syntax {
|
|
Syntax {
|
|
language: "Rhai",
|
|
case_sensitive: true,
|
|
comment: "//",
|
|
comment_multiline: ["/*", "*/"],
|
|
hyperlinks: BTreeSet::new(),
|
|
keywords: BTreeSet::from([
|
|
"if", "else", "switch", "for", "in", "loop", "do", "while", "until", "break",
|
|
"continue", "return", "throw", "try", "catch", "let", "const", "fn", "import",
|
|
"export", "as", "private",
|
|
]),
|
|
// Boolean literals and context keywords get a distinct type color.
|
|
types: BTreeSet::from(["true", "false", "this", "global"]),
|
|
// Built-in Rhai functions highlighted as special identifiers.
|
|
special: BTreeSet::from([
|
|
"print",
|
|
"debug",
|
|
"type_of",
|
|
"is_def_var",
|
|
"is_def_fn",
|
|
"is_shared",
|
|
"call",
|
|
"curry",
|
|
"eval",
|
|
]),
|
|
}
|
|
}
|
|
|
|
/// Renders the script code editor and header bar inside `ui`.
|
|
///
|
|
/// Returns `true` when the user clicks "Back", signalling the caller to save
|
|
/// `content` back to `board.scripts` and close the editor.
|
|
pub(crate) fn show(ui: &mut egui::Ui, name: &str, content: &mut String) -> bool {
|
|
let mut done = false;
|
|
|
|
ui.horizontal(|ui| {
|
|
if ui.button("◄ Back").clicked() {
|
|
done = true;
|
|
}
|
|
ui.label(format!("Editing: {name}"));
|
|
});
|
|
ui.separator();
|
|
|
|
CodeEditor::default()
|
|
.id_source("script_editor")
|
|
.with_rows(30)
|
|
.with_fontsize(14.0)
|
|
.with_theme(ColorTheme::GRUVBOX)
|
|
.with_syntax(rhai_syntax())
|
|
.show(ui, content);
|
|
|
|
done
|
|
}
|