From c7687806a4896ae0311add8affff8b2567afcd31 Mon Sep 17 00:00:00 2001 From: mattwparas Date: Sat, 24 Jun 2023 22:24:52 -0700 Subject: [PATCH] update to use safer APIs --- Cargo.lock | 96 ++++++++------- helix-core/Cargo.toml | 2 +- helix-core/src/extensions.rs | 5 + helix-term/Cargo.toml | 2 +- helix-term/src/commands.rs | 24 ++++ helix-term/src/commands/engine.rs | 43 +++++++ helix-tui/Cargo.toml | 2 +- helix-tui/src/extension.rs | 2 + helix-view/Cargo.toml | 2 +- helix.scm | 189 ++++++++++++++---------------- 10 files changed, 217 insertions(+), 150 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f049027b5..def244c8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,8 +34,8 @@ dependencies = [ "abi_stable_shared", "as_derive_utils", "core_extensions", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "rustc_version", "syn 1.0.109", "typed-arena", @@ -131,8 +131,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff3c96645900a44cf11941c111bd08a6573b0e2f9f69bc9264b179d8fae753c4" dependencies = [ "core_extensions", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "syn 1.0.109", ] @@ -521,10 +521,10 @@ dependencies = [ "cc", "codespan-reporting", "once_cell", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "scratch", - "syn 2.0.15", + "syn 2.0.18", ] [[package]] @@ -539,9 +539,9 @@ version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", ] [[package]] @@ -778,9 +778,9 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", ] [[package]] @@ -1964,8 +1964,8 @@ checksum = "a1d849148dbaf9661a6151d1ca82b13bb4c4c128146a88d05253b38d4e2f496c" dependencies = [ "beef", "fnv", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "regex-syntax 0.6.29", "syn 1.0.109", ] @@ -2173,9 +2173,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406" dependencies = [ "unicode-ident", ] @@ -2227,11 +2227,11 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.26" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" dependencies = [ - "proc-macro2 1.0.56", + "proc-macro2 1.0.60", ] [[package]] @@ -2458,9 +2458,9 @@ version = "1.0.160" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", ] [[package]] @@ -2480,9 +2480,9 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcec881020c684085e55a25f7fd888954d56609ef363479dc5a1305eb0d40cab" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", ] [[package]] @@ -2645,6 +2645,7 @@ dependencies = [ "serde_json", "slotmap", "smallvec", + "steel-derive", "steel-gen", "steel-parser", "termimad", @@ -2654,6 +2655,15 @@ dependencies = [ "which", ] +[[package]] +name = "steel-derive" +version = "0.2.0" +dependencies = [ + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", +] + [[package]] name = "steel-gen" version = "0.2.0" @@ -2702,19 +2712,19 @@ version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "unicode-ident", ] [[package]] name = "syn" -version = "2.0.15" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "unicode-ident", ] @@ -2789,9 +2799,9 @@ version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", ] [[package]] @@ -2893,9 +2903,9 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", ] [[package]] @@ -3133,8 +3143,8 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "syn 1.0.109", "wasm-bindgen-shared", ] @@ -3145,7 +3155,7 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" dependencies = [ - "quote 1.0.26", + "quote 1.0.28", "wasm-bindgen-macro-support", ] @@ -3155,8 +3165,8 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ - "proc-macro2 1.0.56", - "quote 1.0.26", + "proc-macro2 1.0.60", + "quote 1.0.28", "syn 1.0.109", "wasm-bindgen-backend", "wasm-bindgen-shared", diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index 212528ede..ce82ace21 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -48,7 +48,7 @@ chrono = { version = "0.4", default-features = false, features = ["alloc", "std" etcetera = "0.7" textwrap = "0.16.0" -steel-core = { path = "../../../steel/crates/steel-core", version = "0.2.0", features = ["modules", "anyhow", "blocking_requests", "dylibs"] } +steel-core = { path = "../../../steel/crates/steel-core", version = "0.2.0", features = ["modules", "anyhow", "blocking_requests"] } [dev-dependencies] diff --git a/helix-core/src/extensions.rs b/helix-core/src/extensions.rs index bcf6359b2..aa6dc227f 100644 --- a/helix-core/src/extensions.rs +++ b/helix-core/src/extensions.rs @@ -1,7 +1,12 @@ +use steel::gc::unsafe_erased_pointers::CustomReference; + impl steel::rvals::Custom for crate::Position {} struct SRopeSlice<'a>(crate::RopeSlice<'a>); +steel::custom_reference!(SRopeSlice<'a>); +impl<'a> CustomReference for SRopeSlice<'a> {} + impl<'a> SRopeSlice<'a> { pub fn char_to_byte(&self, pos: usize) -> usize { self.0.char_to_byte(pos) diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index d30daeaa2..7155f49eb 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -67,7 +67,7 @@ grep-regex = "0.1.11" grep-searcher = "0.1.11" # plugin support -steel-core = { path = "../../../steel/crates/steel-core", version = "0.2.0", features = ["modules", "anyhow", "blocking_requests", "dylibs"] } +steel-core = { path = "../../../steel/crates/steel-core", version = "0.2.0", features = ["modules", "anyhow", "blocking_requests"] } dlopen = "0.1.8" dlopen_derive = "0.1.4" diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index ec9b7cd13..214a73821 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -54,6 +54,7 @@ use movement::Movement; use crate::{ args, + commands::engine::CallbackQueue, compositor::{self, Component, Compositor}, filter_picker_entry, job::Callback, @@ -189,6 +190,29 @@ macro_rules! static_commands { impl MappableCommand { pub fn execute(&self, cx: &mut Context) { + log::info!("Running command"); + + // TODO: Move this out to a standalone function + while let Some(callback) = CallbackQueue::dequeue() { + log::info!("Found callback: {}", callback); + + if let Err(e) = ENGINE.with(|x| { + let mut guard = x.borrow_mut(); + + { + let res = guard.run_with_reference::( + cx, + "*context*", + &format!("({} *context*)", callback), + ); + + res + } + }) { + cx.editor.set_error(format!("{}", e)); + } + } + match &self { // TODO: @Matt - Add delegating to the engine to run scripts here Self::Typable { name, args, doc: _ } => { diff --git a/helix-term/src/commands/engine.rs b/helix-term/src/commands/engine.rs index 233375ff6..e8c69deb6 100644 --- a/helix-term/src/commands/engine.rs +++ b/helix-term/src/commands/engine.rs @@ -165,6 +165,10 @@ pub fn run_initialization_script(cx: &mut Context) { pub static KEYBINDING_QUEUE: Lazy = Lazy::new(|| SharedKeyBindingsEventQueue::new()); +// pub static CALLBACK_QUEUE: Lazy = Lazy::new(|| ) + +pub static CALLBACK_QUEUE: Lazy = Lazy::new(|| CallbackQueue::new()); + pub static EXPORTED_IDENTIFIERS: Lazy = Lazy::new(|| ExportedIdentifiers::default()); @@ -207,6 +211,27 @@ impl Item for SteelVal { } } +pub struct CallbackQueue { + queue: Arc>>, +} + +impl CallbackQueue { + pub fn new() -> Self { + Self { + queue: Arc::new(Mutex::new(VecDeque::new())), + } + } + + pub fn enqueue(value: String) { + CALLBACK_QUEUE.queue.lock().unwrap().push_back(value); + } + + // Dequeue should probably be a R/W lock? + pub fn dequeue() -> Option { + CALLBACK_QUEUE.queue.lock().unwrap().pop_front() + } +} + /// In order to send events from the engine back to the configuration, we can created a shared /// queue that the engine and the config push and pull from. Alternatively, we could use a channel /// directly, however this was easy enough to set up. @@ -252,6 +277,8 @@ impl Custom for PromptEvent {} impl<'a> CustomReference for Context<'a> {} +steel::custom_reference!(Context<'a>); + fn get_editor<'a>(cx: &'a mut Context<'a>) -> &'a mut Editor { cx.editor } @@ -389,6 +416,8 @@ fn configure_engine() -> std::rc::Rc std::rc::Rc (hash "normal" (hash modifier bindings)) - (value->jsexpr-string) - (helix.keybindings.set-keybindings!))) - - - -(define-syntax minor-mode! - (syntax-rules (=>) - [(minor-mode! modifier (key => function)) - (make-minor-mode! modifier (minor-mode-cruncher (key => function)))] - - [(minor-mode! modifier (key => (function ...))) - (make-minor-mode! modifier (minor-mode-cruncher (key => (function ...))))] - - [(minor-mode! modifier (key => function) remaining ...) - (make-minor-mode! modifier (minor-mode-cruncher (key => function) remaining ...))] - - [(minor-mode! modifier (key => (function ...)) remaining ...) - (make-minor-mode! modifier (minor-mode-cruncher (key => function) ... remaining ...))] - - - - )) + (~> (hash "normal" (hash modifier bindings)) + (value->jsexpr-string) + (helix.keybindings.set-keybindings!))) -(define-syntax minor-mode-cruncher - (syntax-rules (=>) +(define-syntax minor-mode! + (syntax-rules (=>) + [(minor-mode! modifier (key => function)) + (make-minor-mode! modifier (minor-mode-cruncher (key => function)))] + + [(minor-mode! modifier (key => (function ...))) + (make-minor-mode! modifier (minor-mode-cruncher (key => (function ...))))] + [(minor-mode! modifier (key => function) remaining ...) + (make-minor-mode! modifier (minor-mode-cruncher (key => function) remaining ...))] - [(minor-mode-cruncher (key => (function ...))) - (hash key (map (lambda (x) - (string-append ":" (symbol->string x))) - (quote (function ...))))] + [(minor-mode! modifier (key => (function ...)) remaining ...) + (make-minor-mode! modifier (minor-mode-cruncher (key => function) ... remaining ...))])) - [(minor-mode-cruncher (key => function)) - (hash key (string-append ":" (symbol->string (quote function))))] +(define-syntax minor-mode-cruncher + (syntax-rules (=>) - [(minor-mode-cruncher (key => (function ...)) remaining ...) - (hash-insert - (minor-mode-cruncher remaining ...) - key (map (lambda (x) - (string-append ":" (symbol->string x))) - (quote (function ...))))] + [(minor-mode-cruncher (key => (function ...))) + (hash key (map (lambda (x) (string-append ":" (symbol->string x))) (quote (function ...))))] - [(minor-mode-cruncher (key => function) remaining ...) - (hash-insert - (minor-mode-cruncher remaining ...) - key - (string-append ":" (symbol->string (quote function))))] + [(minor-mode-cruncher (key => function)) + (hash key (string-append ":" (symbol->string (quote function))))] + [(minor-mode-cruncher (key => (function ...)) remaining ...) + (hash-insert (minor-mode-cruncher remaining ...) + key + (map (lambda (x) (string-append ":" (symbol->string x))) (quote (function ...))))] - - )) + [(minor-mode-cruncher (key => function) remaining ...) + (hash-insert (minor-mode-cruncher remaining ...) + key + (string-append ":" (symbol->string (quote function))))])) ;;@doc ;; Highlight to the matching paren (define (highlight-to-matching-paren cx) - (helix.static.select_mode cx) - (helix.static.match_brackets cx)) + (helix.static.select_mode cx) + (helix.static.match_brackets cx)) (define (run-expr cx) - (define current-selection (helix.static.current_selection cx)) - (when (or (equal? "(" current-selection) - (equal? ")" current-selection)) - (highlight-to-matching-paren cx) - (helix.static.run-in-engine! cx (helix.static.current-highlighted-text! cx)) - (helix.static.normal_mode cx))) - + (define current-selection (helix.static.current_selection cx)) + (when (or (equal? "(" current-selection) (equal? ")" current-selection)) + (highlight-to-matching-paren cx) + (helix.static.run-in-engine! cx (helix.static.current-highlighted-text! cx)) + (helix.static.normal_mode cx))) (define (run-highlight cx) - (helix.static.run-in-engine! cx (helix.static.current-highlighted-text! cx))) + (helix.static.run-in-engine! cx (helix.static.current-highlighted-text! cx))) ;;@doc ;; Delete the s-expression matching this bracket ;; If the current selection is not on a bracket, this is a no-op (define (delete-sexpr cx) - (define current-selection (helix.static.current_selection cx)) - (when (or (equal? "(" current-selection) - (equal? ")" current-selection)) - (highlight-to-matching-paren cx) - (helix.static.delete_selection cx))) + (define current-selection (helix.static.current_selection cx)) + (when (or (equal? "(" current-selection) (equal? ")" current-selection)) + (highlight-to-matching-paren cx) + (helix.static.delete_selection cx))) ; (minor-mode! "+" ("l" => lam) ; ("q" => (set-theme-dracula lam))) -(minor-mode! "P" ("l" => lam) - ("p" => highlight-to-matching-paren) - ("d" => delete-sexpr) - ("r" => run-expr)) +(minor-mode! "P" + ("l" => lam) + ("p" => highlight-to-matching-paren) + ("d" => delete-sexpr) + ("r" => run-expr)) (make-minor-mode! "+" (hash "l" ":lam")) (define (git-status cx) - (helix.run-shell-command cx '("git" "status") helix.PromptEvent::Validate)) + (helix.run-shell-command cx '("git" "status") helix.PromptEvent::Validate)) (minor-mode! "G" ("s" => git-status)) (define (reload-helix-scm cx) - (helix.static.run-in-engine! cx - (string-append "(require \"" (helix.static.get-helix.scm-path) "\")"))) \ No newline at end of file + (helix.static.run-in-engine! cx + (string-append "(require \"" (helix.static.get-helix.scm-path) "\")")))