From ca346b1416d1848770dcec67a90467670829d646 Mon Sep 17 00:00:00 2001 From: mattwparas Date: Wed, 12 Jul 2023 21:39:27 -0700 Subject: [PATCH] fix versions --- Cargo.toml | 3 + helix-core/Cargo.toml | 3 +- helix-core/src/indent.rs | 16 ++-- helix-term/Cargo.toml | 2 +- helix-term/src/commands/engine.rs | 122 +++++++++++++++++++++++++++++- helix-tui/Cargo.toml | 2 +- helix-view/Cargo.toml | 2 +- 7 files changed, 134 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 48f83d965..0997f8335 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,9 @@ default-members = [ "helix-term" ] +[workspace.dependencies] +steel-core = { path = "../../steel/crates/steel-core", version = "0.4.0", features = ["modules", "anyhow", "blocking_requests"] } + [profile.release] lto = "thin" debug = true diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index 71394db63..8b6a65e50 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -48,8 +48,7 @@ chrono = { version = "0.4", default-features = false, features = ["alloc", "std" etcetera = "0.8" textwrap = "0.16.0" -steel-core = { path = "../../../steel/crates/steel-core", version = "0.4.0", features = ["modules", "anyhow", "blocking_requests"] } - +steel-core = { workspace = true } [dev-dependencies] quickcheck = { version = "1", default-features = false } diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index caf3f3bec..6f449cd4f 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -960,16 +960,16 @@ pub fn custom_indent_for_newline( let line = text_up_to_cursor.line(cursor); // We want to ignore comments - if let Some(l) = line.as_str() { - if l.starts_with(";") { - if cursor == 0 { - break; - } - cursor -= 1; - - continue; + let l = std::borrow::Cow::from(line); + if l.trim_start().starts_with(";") { + if cursor == 0 { + break; } + + cursor -= 1; + + continue; } // log::info!("Line: {}", line); diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 7397e2aa2..2f35bf5c6 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.4.0", features = ["modules", "anyhow", "blocking_requests"] } +steel-core = { workspace = true } dlopen = "0.1.8" dlopen_derive = "0.1.4" diff --git a/helix-term/src/commands/engine.rs b/helix-term/src/commands/engine.rs index 371a81930..15fa89189 100644 --- a/helix-term/src/commands/engine.rs +++ b/helix-term/src/commands/engine.rs @@ -1,10 +1,14 @@ use fuzzy_matcher::FuzzyMatcher; use helix_core::{graphemes, shellwords::Shellwords, Selection, Tendril}; use helix_view::{ - document::Mode, editor::Action, extension::document_id_to_usize, input::KeyEvent, Document, - DocumentId, Editor, + document::Mode, + editor::{Action, ConfigEvent}, + extension::document_id_to_usize, + input::KeyEvent, + Document, DocumentId, Editor, }; use once_cell::sync::Lazy; +use serde_json::Value; use steel::{ gc::unsafe_erased_pointers::CustomReference, rvals::{AsRefMutSteelValFromRef, FromSteelVal, IntoSteelVal}, @@ -16,6 +20,7 @@ use std::{ borrow::Cow, collections::{HashMap, VecDeque}, marker::PhantomData, + ops::Deref, path::PathBuf, sync::Mutex, }; @@ -97,6 +102,14 @@ impl ScriptingEngine { .and_then(|x| x.to_str()) }; + let doc_id = { + let current_focus = cx.editor.tree.focus; + let view = cx.editor.tree.get(current_focus); + let doc = &view.doc; + + doc + }; + if let Some(extension) = extension { let special_buffer_map = "*buffer-or-extension-keybindings*"; @@ -115,6 +128,29 @@ impl ScriptingEngine { } } + // reverse-buffer-map -> label -> keybinding map + let value = ENGINE.with(|x| x.borrow().extract_value("*reverse-buffer-map*").clone()); + + if let Ok(SteelVal::HashMapV(map)) = value { + if let Some(label) = map.get(&SteelVal::IntV(document_id_to_usize(doc_id) as isize)) { + let special_buffer_map = "*buffer-or-extension-keybindings*"; + + let value = ENGINE.with(|x| x.borrow().extract_value(special_buffer_map).clone()); + + if let Ok(SteelVal::HashMapV(map)) = value { + if let Some(value) = map.get(label) { + if let SteelVal::Custom(inner) = value { + if let Some(_) = steel::rvals::as_underlying_type::( + inner.borrow().as_ref(), + ) { + return Some(value.clone()); + } + } + } + } + } + } + None } @@ -686,7 +722,11 @@ fn configure_engine() -> std::rc::Rckeymap", string_to_embedded_keymap); // Use this to get at buffer specific keybindings - engine.register_value("*buffer-or-extension-keybindings*", SteelVal::Void); + engine.register_value( + "*buffer-or-extension-keybindings*", + SteelVal::empty_hashmap(), + ); + engine.register_value("*reverse-buffer-map*", SteelVal::empty_hashmap()); // Find the workspace engine.register_fn("helix-find-workspace", || { @@ -937,6 +977,20 @@ fn configure_engine() -> std::rc::Rc], event: PromptEvent| { + let mut cx = compositor::Context { + editor: cx.editor, + scroll: None, + jobs: cx.jobs, + }; + + set_options(&mut cx, args, event) + }; + + module.register_fn("set-options", func); + } + module.register_value( "PromptEvent::Validate", PromptEvent::Validate.into_steelval().unwrap(), @@ -1438,3 +1492,65 @@ fn create_directory(path: String) { // log::info!("Enqueueing callback!"); // } + +/// Change config at runtime. Access nested values by dot syntax, for +/// example to disable smart case search, use `:set search.smart-case false`. +fn set_options( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + if args.len() % 2 != 0 { + anyhow::bail!("Bad arguments. Usage: `:set key field`"); + } + + let mut config = serde_json::json!(&cx.editor.config().deref()); + // let key_error = || anyhow::anyhow!("Unknown key `{}`", key); + // let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", arg); + + for args in args.chunks_exact(2) { + let (key, arg) = (&args[0].to_lowercase(), &args[1]); + + let key_error = || anyhow::anyhow!("Unknown key `{}`", key); + let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", arg); + + // let mut config = serde_json::json!(&cx.editor.config().deref()); + let pointer = format!("/{}", key.replace('.', "/")); + let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; + + *value = if value.is_string() { + // JSON strings require quotes, so we can't .parse() directly + Value::String(arg.to_string()) + } else { + arg.parse().map_err(field_error)? + }; + } + + // let (key, arg) = (&args[0].to_lowercase(), &args[1]); + + // let key_error = || anyhow::anyhow!("Unknown key `{}`", key); + // let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", arg); + + // let mut config = serde_json::json!(&cx.editor.config().deref()); + // let pointer = format!("/{}", key.replace('.', "/")); + // let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; + + // *value = if value.is_string() { + // // JSON strings require quotes, so we can't .parse() directly + // Value::String(arg.to_string()) + // } else { + // arg.parse().map_err(field_error)? + // }; + let config = + serde_json::from_value(config).map_err(|_| anyhow::anyhow!("Could not parse config"))?; + + cx.editor + .config_events + .0 + .send(ConfigEvent::Update(config))?; + Ok(()) +} diff --git a/helix-tui/Cargo.toml b/helix-tui/Cargo.toml index ace8df276..1b3e66171 100644 --- a/helix-tui/Cargo.toml +++ b/helix-tui/Cargo.toml @@ -26,4 +26,4 @@ once_cell = "1.18" log = "~0.4" helix-view = { version = "0.6", path = "../helix-view", features = ["term"] } helix-core = { version = "0.6", path = "../helix-core" } -steel-core = { path = "../../../steel/crates/steel-core", version = "0.4.0", features = ["modules", "anyhow", "blocking_requests"] } \ No newline at end of file +steel-core = { workspace = true } diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 318edd489..f5ef51683 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -46,7 +46,7 @@ which = "4.4" parking_lot = "0.12.1" # plugin support -steel-core = { path = "../../../steel/crates/steel-core", version = "0.4.0", features = ["modules", "anyhow", "blocking_requests"] } +steel-core = { workspace = true } [target.'cfg(windows)'.dependencies]