From 20674aa65148558fb849eb1bb9c8a5e4a0cf483d Mon Sep 17 00:00:00 2001 From: tslil Date: Wed, 22 Oct 2025 21:02:01 +0100 Subject: Implement two basic selection functions --- rprt-engine/src/selection_function.rs | 77 +++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-) (limited to 'rprt-engine/src/selection_function.rs') diff --git a/rprt-engine/src/selection_function.rs b/rprt-engine/src/selection_function.rs index 6f05650..3da61c9 100644 --- a/rprt-engine/src/selection_function.rs +++ b/rprt-engine/src/selection_function.rs @@ -4,12 +4,15 @@ use crate::state::EditorState; use thiserror::Error; #[derive(Error, Debug)] -pub enum SFError {} +pub enum SFError { + #[error("Could not find buffer {0}")] + BufferNotFound(BufferID), +} struct SFComponents { niladic: Box Result>, - monadic_rank0: Box Result>, - monadic_rank1: Box Result>, + monadic_rank0: Box Result>, + monadic_rank1: Box Result>, dyadic: Option Result>>, } @@ -31,13 +34,71 @@ impl From for SelectionFunction> { } else { None }; - + let niladic = Box::new(move |editor_state: &EditorState| { + (sfc.niladic)(editor_state).map_err(VectoriseError::ProcessingError) + }); + let monadic = Box::new(Selection::vectorise(sfc.monadic_rank0, sfc.monadic_rank1)); Self { - niladic: Box::new(move |editor_state: &EditorState| { - (sfc.niladic)(editor_state).map_err(VectoriseError::ProcessingError) + niladic, + monadic, + dyadic, + } + } +} + +impl SelectionFunction> { + pub fn empty() -> Self { + SFComponents { + niladic: Box::new(|_| Ok(Selection::empty())), + monadic_rank0: Box::new(|_, _, _| Ok(Selection::empty())), + monadic_rank1: Box::new(|_, _, _| Ok(Selection::empty())), + dyadic: None, + } + .into() + } + + pub fn end_sequential() -> Self { + fn get_end_position(es: &EditorState, buffer_id: BufferID) -> Result { + if let Some(buf) = es.get_buffer(buffer_id) { + Ok(Selection::Position { + buffer_id: buffer_id, + pos: buf.max_pos(), + }) + } else { + Err(SFError::BufferNotFound(buffer_id)) + } + } + + SFComponents { + niladic: Box::new(|es| get_end_position(es, es.current_buffer_id)), + monadic_rank0: Box::new(|es, buffer_id, _| get_end_position(es, buffer_id)), + monadic_rank1: Box::new(|es, buffer_id, _| get_end_position(es, buffer_id)), + dyadic: None, + } + .into() + } + + pub fn end_structural() -> Self { + SFComponents { + niladic: Box::new(|es| { + if let Some(buf) = es.current_buffer() { + Ok(Selection::Position { + buffer_id: es.current_buffer_id, + pos: buf.max_pos(), + }) + } else { + Err(SFError::BufferNotFound(es.current_buffer_id)) + } + }), + monadic_rank0: Box::new(|_, buffer_id, pos| Ok(Selection::Position { buffer_id, pos })), + monadic_rank1: Box::new(|_, buffer_id, int| { + Ok(Selection::Position { + buffer_id, + pos: int.end, + }) }), - monadic: Box::new(Selection::vectorise(sfc.monadic_rank0, sfc.monadic_rank1)), - dyadic: dyadic, + dyadic: None, } + .into() } } -- cgit v1.2.3