From f582c8b7bddc7842b87b37a272ca16814da3e229 Mon Sep 17 00:00:00 2001 From: Nik Revenco Date: Sun, 15 Jun 2025 17:42:58 +0100 Subject: [PATCH] feat: add API for selection and ranges (#33) * feat: add API for selection and ranges * fix: use `no_context!` macro for declaring these functions * chore: rename accessors to structs --- helix-core/src/selection.rs | 3 ++ helix-term/src/commands/engine/steel.rs | 48 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 5bde08e31..e66a87008 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -16,6 +16,7 @@ use helix_stdx::range::is_subset; use helix_stdx::rope::{self, RopeSliceExt}; use smallvec::{smallvec, SmallVec}; use std::{borrow::Cow, iter, slice}; +use steel::rvals::Custom; /// A single selection range. /// @@ -62,6 +63,8 @@ pub struct Range { pub old_visual_position: Option<(u32, u32)>, } +impl Custom for Range {} + impl Range { pub fn new(anchor: usize, head: usize) -> Self { Self { diff --git a/helix-term/src/commands/engine/steel.rs b/helix-term/src/commands/engine/steel.rs index 209fa0899..8fe725d63 100644 --- a/helix-term/src/commands/engine/steel.rs +++ b/helix-term/src/commands/engine/steel.rs @@ -487,6 +487,54 @@ fn load_static_commands(engine: &mut Engine, generate_sources: bool) { } }; + macro_rules! no_context { + ($name:expr, $function:expr, $doc:expr) => {{ + module.register_fn($name, $function); + template_function_no_context($name, $doc); + }}; + } + + no_context!( + "selection->primary-index", + |sel: Selection| sel.primary_index(), + "Returns index of the primary selection" + ); + no_context!( + "selection->primary-range", + |sel: Selection| sel.primary(), + "Returns the range for primary selection" + ); + no_context!( + "selection->ranges", + |sel: Selection| sel.ranges().to_vec(), + "Returns all ranges of the selection" + ); + no_context!( + "range-anchor", + |range: Range| range.anchor, + "Get the anchor of the range: the side that doesn't move when extending." + ); + no_context!( + "range->from", + |range: Range| range.from(), + "Get the start of the range" + ); + no_context!( + "range-head", + |range: Range| range.head, + "Get the head of the range, moved when extending." + ); + no_context!( + "range->to", + |range: Range| range.to(), + "Get the end of the range" + ); + no_context!( + "range->span", + |range: Range| (range.from(), range.to()), + "Get the span of the range (from, to)" + ); + module.register_fn("get-helix-scm-path", get_helix_scm_path); module.register_fn("get-init-scm-path", get_init_scm_path);