From b045c3068b39826923128e991f7b26104fd02303 Mon Sep 17 00:00:00 2001 From: Gabor Pihaj Date: Sat, 6 Jul 2024 10:33:28 +0100 Subject: [PATCH 1/4] Add switch-replace command to be able to switch to an existing buffer --- helix-term/src/commands/engine/steel.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/helix-term/src/commands/engine/steel.rs b/helix-term/src/commands/engine/steel.rs index fa69a1d22..1ed9f5251 100644 --- a/helix-term/src/commands/engine/steel.rs +++ b/helix-term/src/commands/engine/steel.rs @@ -702,6 +702,7 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { // Arity 1 module.register_fn("editor->doc-id", cx_get_document_id); module.register_fn("editor-switch!", cx_switch); + module.register_fn("editor-switch-replace!", cx_switch_replace); module.register_fn("editor-set-focus!", |cx: &mut Context, view_id: ViewId| { cx.editor.focus(view_id) }); @@ -757,6 +758,7 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { template_function_arity_1("editor->doc-id"); template_function_arity_1("editor-switch!"); + template_function_arity_1("editor-switch-replace!"); template_function_arity_1("editor-set-focus!"); template_function_arity_1("editor-set-mode!"); template_function_arity_1("editor-doc-in-view?"); @@ -2266,6 +2268,10 @@ fn cx_switch(cx: &mut Context, doc_id: DocumentId) { cx.editor.switch(doc_id, Action::VerticalSplit) } +fn cx_switch_replace(cx: &mut Context, doc_id: DocumentId) { + cx.editor.switch(doc_id, Action::Replace) +} + fn cx_get_mode(cx: &mut Context) -> Mode { cx.editor.mode } From a06824308b9513a37cdfeeadf27c4ee9efc79f5b Mon Sep 17 00:00:00 2001 From: Gabor Pihaj Date: Sat, 6 Jul 2024 10:46:54 +0100 Subject: [PATCH 2/4] Implement editor-switch-action instead of hardcoded replace --- helix-term/src/commands/engine/steel.rs | 22 ++++++++++++++++++---- helix-view/src/extension.rs | 6 +++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/helix-term/src/commands/engine/steel.rs b/helix-term/src/commands/engine/steel.rs index 1ed9f5251..5f0834efe 100644 --- a/helix-term/src/commands/engine/steel.rs +++ b/helix-term/src/commands/engine/steel.rs @@ -702,7 +702,6 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { // Arity 1 module.register_fn("editor->doc-id", cx_get_document_id); module.register_fn("editor-switch!", cx_switch); - module.register_fn("editor-switch-replace!", cx_switch_replace); module.register_fn("editor-set-focus!", |cx: &mut Context, view_id: ViewId| { cx.editor.focus(view_id) }); @@ -711,6 +710,9 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { module.register_fn("set-scratch-buffer-name!", set_scratch_buffer_name); module.register_fn("editor-doc-exists?", cx_document_exists); + // Arity 2 + module.register_fn("editor-switch-action!", cx_switch_action); + // Arity 1 RegisterFn::< _, @@ -758,7 +760,6 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { template_function_arity_1("editor->doc-id"); template_function_arity_1("editor-switch!"); - template_function_arity_1("editor-switch-replace!"); template_function_arity_1("editor-set-focus!"); template_function_arity_1("editor-set-mode!"); template_function_arity_1("editor-doc-in-view?"); @@ -766,6 +767,19 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { template_function_arity_1("editor-doc-exists?"); template_function_arity_1("editor->get-document"); + let mut template_function_arity_2 = |name: &str| { + builtin_editor_command_module.push_str(&format!( + r#" +(provide {}) +(define ({} arg1 arg2) + (helix.{} *helix.cx* arg1 arg2)) +"#, + name, name, name + )); + }; + + template_function_arity_2("editor-switch-action!"); + let mut target_directory = helix_runtime_search_path(); if !target_directory.exists() { @@ -2268,8 +2282,8 @@ fn cx_switch(cx: &mut Context, doc_id: DocumentId) { cx.editor.switch(doc_id, Action::VerticalSplit) } -fn cx_switch_replace(cx: &mut Context, doc_id: DocumentId) { - cx.editor.switch(doc_id, Action::Replace) +fn cx_switch_action(cx: &mut Context, doc_id: DocumentId, action: Action) { + cx.editor.switch(doc_id, action) } fn cx_get_mode(cx: &mut Context) -> Mode { diff --git a/helix-view/src/extension.rs b/helix-view/src/extension.rs index 50f96e685..db71c3601 100644 --- a/helix-view/src/extension.rs +++ b/helix-view/src/extension.rs @@ -15,9 +15,7 @@ mod steel_implementations { use crate::{ document::Mode, editor::{ - BufferLine, CursorShapeConfig, FilePickerConfig, GutterConfig, IndentGuidesConfig, - LineEndingConfig, LineNumber, LspConfig, SearchConfig, SmartTabConfig, - StatusLineConfig, TerminalConfig, WhitespaceConfig, + Action, BufferLine, CursorShapeConfig, FilePickerConfig, GutterConfig, IndentGuidesConfig, LineEndingConfig, LineNumber, LspConfig, SearchConfig, SmartTabConfig, StatusLineConfig, TerminalConfig, WhitespaceConfig }, graphics::{Color, Rect, Style, UnderlineStyle}, input::Event, @@ -56,6 +54,8 @@ mod steel_implementations { impl Custom for ViewId {} impl CustomReference for Document {} + impl Custom for Action {} + impl Custom for FilePickerConfig {} impl Custom for StatusLineConfig {} impl Custom for SearchConfig {} From d611f7f264bae580b3b9bb2ebec67bcbebfb0dfd Mon Sep 17 00:00:00 2001 From: Gabor Pihaj Date: Sat, 6 Jul 2024 16:01:59 +0100 Subject: [PATCH 3/4] Create type constructor functions for editor::Action --- helix-term/src/commands/engine/steel.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/helix-term/src/commands/engine/steel.rs b/helix-term/src/commands/engine/steel.rs index 5f0834efe..e18b87e68 100644 --- a/helix-term/src/commands/engine/steel.rs +++ b/helix-term/src/commands/engine/steel.rs @@ -692,6 +692,12 @@ fn load_configuration_api(engine: &mut Engine, generate_sources: bool) { fn load_editor_api(engine: &mut Engine, generate_sources: bool) { let mut module = BuiltInModule::new("helix/core/editor"); + // Types + module.register_fn("Action::Load", || Action::Load); + module.register_fn("Action::Replace", || Action::Replace); + module.register_fn("Action::HorizontalSplit", || Action::HorizontalSplit); + module.register_fn("Action::VerticalSplit", || Action::VerticalSplit); + // Arity 0 module.register_fn("editor-focus", cx_current_focus); module.register_fn("editor-mode", cx_get_mode); @@ -730,6 +736,22 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { let mut builtin_editor_command_module = "(require-builtin helix/core/editor as helix.)".to_string(); + let mut template_function_type_constructor = |name: &str| { + builtin_editor_command_module.push_str(&format!( + r#" +(provide {}) +(define ({}) + (helix.{})) +"#, + name, name, name + )); + }; + + template_function_type_constructor("Action::Load"); + template_function_type_constructor("Action::Replace"); + template_function_type_constructor("Action::HorizontalSplit"); + template_function_type_constructor("Action::VerticalSplit"); + let mut template_function_arity_0 = |name: &str| { builtin_editor_command_module.push_str(&format!( r#" From 4f1d35dfbe027af0914050ecd457447372494cde Mon Sep 17 00:00:00 2001 From: Gabor Pihaj Date: Sat, 6 Jul 2024 16:30:25 +0100 Subject: [PATCH 4/4] Follow naming conventions --- helix-term/src/commands/engine/steel.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands/engine/steel.rs b/helix-term/src/commands/engine/steel.rs index e18b87e68..4ff3fde8b 100644 --- a/helix-term/src/commands/engine/steel.rs +++ b/helix-term/src/commands/engine/steel.rs @@ -693,10 +693,10 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { let mut module = BuiltInModule::new("helix/core/editor"); // Types - module.register_fn("Action::Load", || Action::Load); - module.register_fn("Action::Replace", || Action::Replace); - module.register_fn("Action::HorizontalSplit", || Action::HorizontalSplit); - module.register_fn("Action::VerticalSplit", || Action::VerticalSplit); + module.register_fn("Action/Load", || Action::Load); + module.register_fn("Action/Replace", || Action::Replace); + module.register_fn("Action/HorizontalSplit", || Action::HorizontalSplit); + module.register_fn("Action/VerticalSplit", || Action::VerticalSplit); // Arity 0 module.register_fn("editor-focus", cx_current_focus); @@ -747,10 +747,10 @@ fn load_editor_api(engine: &mut Engine, generate_sources: bool) { )); }; - template_function_type_constructor("Action::Load"); - template_function_type_constructor("Action::Replace"); - template_function_type_constructor("Action::HorizontalSplit"); - template_function_type_constructor("Action::VerticalSplit"); + template_function_type_constructor("Action/Load"); + template_function_type_constructor("Action/Replace"); + template_function_type_constructor("Action/HorizontalSplit"); + template_function_type_constructor("Action/VerticalSplit"); let mut template_function_arity_0 = |name: &str| { builtin_editor_command_module.push_str(&format!(