Add run Lua mode. Remove basic-setup Codemirror for customisation.

This commit is contained in:
Condorra 2024-10-06 22:42:19 +11:00
parent 2d17816ef5
commit a32f024f17
5 changed files with 183 additions and 31 deletions

View File

@ -4,7 +4,6 @@
<script type="importmap"> <script type="importmap">
{ {
"imports": { "imports": {
"codemirror": "./codemirror/index.js",
"@codemirror/view": "./@codemirror/view/index.js", "@codemirror/view": "./@codemirror/view/index.js",
"@codemirror/state": "./@codemirror/state/index.js", "@codemirror/state": "./@codemirror/state/index.js",
"@codemirror/language": "./@codemirror/language/index.js", "@codemirror/language": "./@codemirror/language/index.js",
@ -30,7 +29,6 @@
<link data-trunk rel="css" href="styles.css"/> <link data-trunk rel="css" href="styles.css"/>
<link data-trunk rel="copy-file" href="assets/fonts/JetBrainsMono-Regular.woff2"/> <link data-trunk rel="copy-file" href="assets/fonts/JetBrainsMono-Regular.woff2"/>
<link data-trunk rel="copy-file" href="node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff" data-target-path="fonts"/> <link data-trunk rel="copy-file" href="node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff" data-target-path="fonts"/>
<link data-trunk rel="copy-file" href="node_modules/codemirror/dist/index.js" data-target-path="codemirror"></link>
<link data-trunk rel="copy-file" href="node_modules/@codemirror/view/dist/index.js" data-target-path="@codemirror/view"></link> <link data-trunk rel="copy-file" href="node_modules/@codemirror/view/dist/index.js" data-target-path="@codemirror/view"></link>
<link data-trunk rel="copy-file" href="node_modules/@codemirror/state/dist/index.js" data-target-path="@codemirror/state"></link> <link data-trunk rel="copy-file" href="node_modules/@codemirror/state/dist/index.js" data-target-path="@codemirror/state"></link>
<link data-trunk rel="copy-file" href="node_modules/@codemirror/language/dist/index.js" data-target-path="@codemirror/language"></link> <link data-trunk rel="copy-file" href="node_modules/@codemirror/language/dist/index.js" data-target-path="@codemirror/language"></link>

23
package-lock.json generated
View File

@ -9,13 +9,18 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@codemirror/autocomplete": "^6.18.1",
"@codemirror/commands": "^6.6.2",
"@codemirror/language": "^6.10.3",
"@codemirror/legacy-modes": "^6.4.1", "@codemirror/legacy-modes": "^6.4.1",
"@codemirror/lint": "^6.8.2",
"@codemirror/search": "^6.5.6",
"@codemirror/state": "^6.4.1", "@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.34.1",
"@xterm/addon-fit": "^0.10.0", "@xterm/addon-fit": "^0.10.0",
"@xterm/xterm": "^5.5.0", "@xterm/xterm": "^5.5.0",
"bootstrap": "^5.3.3", "bootstrap": "^5.3.3",
"bootstrap-icons": "^1.11.3", "bootstrap-icons": "^1.11.3"
"codemirror": "^6.0.1"
} }
}, },
"node_modules/@codemirror/autocomplete": { "node_modules/@codemirror/autocomplete": {
@ -179,20 +184,6 @@
} }
] ]
}, },
"node_modules/codemirror": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/codemirror/-/codemirror-6.0.1.tgz",
"integrity": "sha512-J8j+nZ+CdWmIeFIGXEFbFPtpiYacFMDR8GlHK3IyHQJMCaVRfGx9NT+Hxivv1ckLWPvNdZqndbr/7lVhrf/Svg==",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/commands": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/lint": "^6.0.0",
"@codemirror/search": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0"
}
},
"node_modules/crelt": { "node_modules/crelt": {
"version": "1.0.6", "version": "1.0.6",
"resolved": "https://registry.npmjs.org/crelt/-/crelt-1.0.6.tgz", "resolved": "https://registry.npmjs.org/crelt/-/crelt-1.0.6.tgz",

View File

@ -9,12 +9,17 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@codemirror/autocomplete": "^6.18.1",
"@codemirror/commands": "^6.6.2",
"@codemirror/language": "^6.10.3",
"@codemirror/legacy-modes": "^6.4.1", "@codemirror/legacy-modes": "^6.4.1",
"@codemirror/lint": "^6.8.2",
"@codemirror/search": "^6.5.6",
"@codemirror/state": "^6.4.1", "@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.34.1",
"@xterm/addon-fit": "^0.10.0", "@xterm/addon-fit": "^0.10.0",
"@xterm/xterm": "^5.5.0", "@xterm/xterm": "^5.5.0",
"bootstrap": "^5.3.3", "bootstrap": "^5.3.3",
"bootstrap-icons": "^1.11.3", "bootstrap-icons": "^1.11.3"
"codemirror": "^6.0.1"
} }
} }

View File

@ -99,13 +99,49 @@ fn fetch_initial_editor_state() -> Rc<EditorViewState> {
} }
} }
pub fn try_run_script(script: &str, global_memo: &GlobalMemoCell) -> anyhow::Result<()> {
let script = load_file_contents(script)?;
global_memo
.lua_engine
.borrow_mut()
.execute(&script)
.map_err(Error::msg)?;
Ok(())
}
fn run_script<F>(script: &str, global_memo: &GlobalMemoCell, set_err: Rc<F>)
where
F: Fn(Option<&str>),
{
match try_run_script(script, global_memo) {
Ok(()) => {
set_err(None);
}
Err(e) => {
set_err(Some(&format!("Script error: {}", e)));
}
}
}
#[function_component(EditorNav)] #[function_component(EditorNav)]
fn editor_nav(props: &EditorViewDetailProps) -> Html { fn editor_nav(props: &EditorViewDetailProps) -> Html {
let global_memo = props.global_memo.clone();
let global_layout = props.global_layout.clone(); let global_layout = props.global_layout.clone();
let frame = props.frame.clone(); let frame = props.frame.clone();
let current_script = props.editor_state.open_file.clone();
let set_err_state = props.editor_state.clone();
let set_err = Rc::new(move |msg: Option<&str>| {
let mut new_state = (*set_err_state.as_ref()).clone();
new_state.error_msg = msg.map(|v| AttrValue::from(String::from(v)));
set_err_state.set(new_state.into());
});
html! { html! {
<div class="editornav"> <div class="editornav">
<nav class="navbar navbar-expand-lg bg-body-tertiary"> <nav class="navbar navbar-expand-lg bg-body-tertiary">
<button class="btn" aria-label="Run" title="Run"
onclick={move |_ev| run_script(&current_script, &global_memo, set_err.clone())}>
<i class="bi bi-file-earmark-play"></i></button>
<button class="btn" aria-label="New file" title="New file"><i class="bi bi-file-earmark-plus"></i></button> <button class="btn" aria-label="New file" title="New file"><i class="bi bi-file-earmark-plus"></i></button>
<div class="flex-fill"/> <div class="flex-fill"/>
<button class="btn" onclick={move |_ev| close_editor(&frame, &global_layout)} aria-label="Close Editor" title="Close editor"><i class="bi bi-arrow-return-left"></i></button> <button class="btn" onclick={move |_ev| close_editor(&frame, &global_layout)} aria-label="Close Editor" title="Close editor"><i class="bi bi-arrow-return-left"></i></button>
@ -142,16 +178,9 @@ struct EditorStateConfig {
pub extensions: Vec<CMExtension>, pub extensions: Vec<CMExtension>,
} }
#[wasm_bindgen(module = codemirror)] #[wasm_bindgen(getter_with_clone)]
extern "C" { struct HighlightingConfig {
#[derive(Clone)] pub fallback: bool,
type CMExtension;
#[wasm_bindgen(constructor)]
fn new(settings: EditorViewConfig) -> EditorView;
#[wasm_bindgen(js_name = basicSetup, thread_local)]
static BASIC_SETUP: CMExtension;
} }
#[wasm_bindgen(module = "@codemirror/state")] #[wasm_bindgen(module = "@codemirror/state")]
@ -165,12 +194,22 @@ extern "C" {
#[wasm_bindgen(method, getter)] #[wasm_bindgen(method, getter)]
fn doc(st: &EditorState) -> CMDocument; fn doc(st: &EditorState) -> CMDocument;
#[wasm_bindgen(js_namespace = ["EditorState", "allowMultipleSelections"], js_name = of)]
fn editorstate_allow_multiple_selections_of(v: bool) -> CMExtension;
} }
#[wasm_bindgen(module = "@codemirror/view")] #[wasm_bindgen(module = "@codemirror/view")]
extern "C" { extern "C" {
type EditorView; type EditorView;
type ViewUpdate; type ViewUpdate;
#[derive(Clone)]
type KeyBinding;
#[derive(Clone)]
type CMExtension;
#[wasm_bindgen(constructor)]
fn new(settings: EditorViewConfig) -> EditorView;
#[wasm_bindgen(js_namespace = ["EditorView", "updateListener"], js_name = of)] #[wasm_bindgen(js_namespace = ["EditorView", "updateListener"], js_name = of)]
fn editorview_updatelistener_of(cb: &Closure<dyn FnMut(ViewUpdate)>) -> CMExtension; fn editorview_updatelistener_of(cb: &Closure<dyn FnMut(ViewUpdate)>) -> CMExtension;
@ -189,6 +228,26 @@ extern "C" {
#[wasm_bindgen(method, getter)] #[wasm_bindgen(method, getter)]
fn view(upd: &ViewUpdate) -> EditorView; fn view(upd: &ViewUpdate) -> EditorView;
#[wasm_bindgen(js_name = highlightSpecialChars)]
fn highlight_special_chars() -> CMExtension;
#[wasm_bindgen(js_name = drawSelection)]
fn draw_selection() -> CMExtension;
#[wasm_bindgen(js_name = highlightActiveLine)]
fn highlight_active_line() -> CMExtension;
#[wasm_bindgen(js_name = dropCursor)]
fn drop_cursor() -> CMExtension;
#[wasm_bindgen(js_name = rectangularSelection)]
fn rectangular_selection() -> CMExtension;
#[wasm_bindgen(js_name = crosshairCursor)]
fn crosshair_cursor() -> CMExtension;
#[wasm_bindgen(js_name = lineNumbers)]
fn line_numbers() -> CMExtension;
#[wasm_bindgen(js_name = highlightActiveLineGutter)]
fn highlight_active_line_gutter() -> CMExtension;
#[wasm_bindgen(js_namespace = keymap, js_name = of)]
fn keymap_of(keys: Vec<KeyBinding>) -> CMExtension;
} }
#[wasm_bindgen(module = "@codemirror/legacy-modes/mode/lua")] #[wasm_bindgen(module = "@codemirror/legacy-modes/mode/lua")]
@ -201,9 +260,66 @@ extern "C" {
extern "C" { extern "C" {
#[derive(Clone)] #[derive(Clone)]
type StreamLanguage; type StreamLanguage;
#[derive(Clone)]
type CMHighlightStyle;
#[wasm_bindgen(js_namespace = StreamLanguage, js_name = define)] #[wasm_bindgen(js_namespace = StreamLanguage, js_name = define)]
fn streamlanguage_define(input: StreamLanguage) -> CMExtension; fn streamlanguage_define(input: StreamLanguage) -> CMExtension;
#[wasm_bindgen(js_name = defaultHighlightStyle, thread_local)]
static DEFAULT_HIGHLIGHT_STYLE: CMHighlightStyle;
#[wasm_bindgen(js_name = syntaxHighlighting)]
fn syntax_highlighting(style: CMHighlightStyle, config: HighlightingConfig) -> CMExtension;
#[wasm_bindgen(js_name = indentOnInput)]
fn indent_on_input() -> CMExtension;
#[wasm_bindgen(js_name = bracketMatching)]
fn bracket_matching() -> CMExtension;
#[wasm_bindgen(js_name = foldGutter)]
fn fold_gutter() -> CMExtension;
#[wasm_bindgen(js_name = foldKeymap, thread_local)]
static FOLD_KEYMAP: Vec<KeyBinding>;
}
#[wasm_bindgen(module = "@codemirror/commands")]
extern "C" {
#[wasm_bindgen(js_name = defaultKeymap, thread_local)]
static DEFAULT_KEYMAP: Vec<KeyBinding>;
#[wasm_bindgen(js_name = historyKeymap, thread_local)]
static HISTORY_KEYMAP: Vec<KeyBinding>;
#[wasm_bindgen]
fn history() -> CMExtension;
}
#[wasm_bindgen(module = "@codemirror/autocomplete")]
extern "C" {
#[wasm_bindgen]
fn autocompletion() -> CMExtension;
#[wasm_bindgen(js_name = closeBrackets)]
fn close_brackets() -> CMExtension;
#[wasm_bindgen(js_name = completionKeymap, thread_local)]
static COMPLETION_KEYMAP: Vec<KeyBinding>;
#[wasm_bindgen(js_name = closeBracketsKeymap, thread_local)]
static CLOSE_BRACKETS_KEYMAP: Vec<KeyBinding>;
}
#[wasm_bindgen(module = "@codemirror/search")]
extern "C" {
#[wasm_bindgen(js_name = searchKeymap, thread_local)]
static SEARCH_KEYMAP: Vec<KeyBinding>;
#[wasm_bindgen(js_name = highlightSelectionMatches)]
fn highlight_selection_matches() -> CMExtension;
}
#[wasm_bindgen(module = "@codemirror/lint")]
extern "C" {
#[wasm_bindgen(js_name = lintKeymap, thread_local)]
static LINT_KEYMAP: Vec<KeyBinding>;
} }
fn try_save_document(file: &str, contents: &str) -> anyhow::Result<()> { fn try_save_document(file: &str, contents: &str) -> anyhow::Result<()> {
@ -262,7 +378,41 @@ fn editor_area(props: &EditorViewDetailProps) -> Html {
doc: load_file_contents(open_file) doc: load_file_contents(open_file)
.unwrap_or_else(|e| format!("Error loading content: {}", e.to_string())), .unwrap_or_else(|e| format!("Error loading content: {}", e.to_string())),
extensions: vec![ extensions: vec![
BASIC_SETUP.with(CMExtension::clone), // BASIC_SETUP.with(CMExtension::clone),
line_numbers(),
highlight_active_line_gutter(),
highlight_special_chars(),
history(),
fold_gutter(),
draw_selection(),
drop_cursor(),
editorstate_allow_multiple_selections_of(true),
indent_on_input(),
syntax_highlighting(
DEFAULT_HIGHLIGHT_STYLE.with(CMHighlightStyle::clone),
HighlightingConfig { fallback: true },
),
bracket_matching(),
close_brackets(),
autocompletion(),
rectangular_selection(),
crosshair_cursor(),
highlight_active_line(),
highlight_selection_matches(),
keymap_of(
vec![
&CLOSE_BRACKETS_KEYMAP,
&DEFAULT_KEYMAP,
&SEARCH_KEYMAP,
&HISTORY_KEYMAP,
&FOLD_KEYMAP,
&COMPLETION_KEYMAP,
&LINT_KEYMAP,
]
.into_iter()
.flat_map(|k| k.with(|v| v.clone()).into_iter())
.collect(),
),
StreamLanguage::streamlanguage_define(LUA.with(StreamLanguage::clone)), StreamLanguage::streamlanguage_define(LUA.with(StreamLanguage::clone)),
editorview_updatelistener_of(&closures.change_closure), editorview_updatelistener_of(&closures.change_closure),
], ],

View File

@ -5,6 +5,7 @@ use std::rc::Rc;
use logging::LoggingEngine; use logging::LoggingEngine;
use parsing::ParsedCommand; use parsing::ParsedCommand;
use term_split::TermSplit; use term_split::TermSplit;
use web_sys::console;
use yew::prelude::*; use yew::prelude::*;
pub mod command_handler; pub mod command_handler;
@ -21,6 +22,7 @@ pub mod telnet;
pub mod term_split; pub mod term_split;
pub mod timer_host; pub mod timer_host;
pub mod websocket; pub mod websocket;
use crate::editor_view::try_run_script;
use crate::frame_view::*; use crate::frame_view::*;
use crate::lua_engine::{install_lua_globals, LuaState}; use crate::lua_engine::{install_lua_globals, LuaState};
use crate::split_panel::*; use crate::split_panel::*;
@ -82,6 +84,12 @@ fn app() -> Html {
use_memo((), |_| { use_memo((), |_| {
install_lua_globals(&global_memo, global_layout.setter()) install_lua_globals(&global_memo, global_layout.setter())
.expect("Couldn't install Lua globals"); .expect("Couldn't install Lua globals");
match try_run_script("init.lua", &global_memo) {
Ok(_) => {}
Err(e) => {
console::log_1(&format!("Error running init.lua: {}", e).into());
}
}
}); });
html! { html! {