aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rprt-engine/src/selection_function.rs182
1 files changed, 75 insertions, 107 deletions
diff --git a/rprt-engine/src/selection_function.rs b/rprt-engine/src/selection_function.rs
index ac3d221..c46e781 100644
--- a/rprt-engine/src/selection_function.rs
+++ b/rprt-engine/src/selection_function.rs
@@ -1,5 +1,5 @@
use crate::buffer::BufferID;
-use crate::selection::{Selection, VectoriseError};
+use crate::selection::{Interval, Selection, VectoriseError};
use crate::state::EditorState;
use thiserror::Error;
@@ -52,6 +52,80 @@ macro_rules! selection_function {
}
// ----------------------------------------------------------------
+pub fn complement_transform(es: &EditorState, result: Selection, _: &SFArguments) -> SFResult {
+ fn complement_rank0(
+ es: &EditorState,
+ buffer_id: BufferID,
+ pos: usize,
+ ) -> Result<Selection, SFError> {
+ if let Some(buf) = es.get_buffer(buffer_id) {
+ let mut ranges = Vec::new();
+ if pos > 0 {
+ ranges.push(Interval::new(0, pos));
+ }
+ let max_pos = buf.max_pos();
+ if pos + 1 < max_pos {
+ ranges.push(Interval::new(pos + 1, max_pos));
+ }
+ Ok(Selection::Ranges { buffer_id, ranges })
+ } else {
+ Err(SFError::BufferNotFound(buffer_id))
+ }
+ }
+
+ fn complement_rank1(
+ es: &EditorState,
+ buffer_id: BufferID,
+ interval: &Interval,
+ ) -> Result<Selection, SFError> {
+ if let Some(buf) = es.get_buffer(buffer_id) {
+ let max_pos = buf.max_pos();
+ if interval.start == 0 && interval.end >= max_pos {
+ return Ok(Selection::empty());
+ }
+
+ let mut ranges = Vec::new();
+ if interval.start == 0 {
+ if interval.end < max_pos {
+ ranges.push(Interval::new(interval.end, max_pos));
+ }
+ } else if interval.end == max_pos {
+ ranges.push(Interval::new(0, interval.start));
+ } else {
+ ranges.push(Interval::new(0, interval.start));
+ ranges.push(Interval::new(interval.end, max_pos));
+ }
+ Ok(Selection::Ranges { buffer_id, ranges })
+ } else {
+ Err(SFError::BufferNotFound(buffer_id))
+ }
+ }
+
+ result.vectorise(es, complement_rank0, complement_rank1)
+}
+
+pub fn conditional_transform(
+ _: &EditorState,
+ mut result: Selection,
+ arg: &SFArguments,
+) -> SFResult {
+ if !result.is_empty() {
+ Ok(match arg {
+ // TODO: think about this niladic case?
+ SFArguments::Niladic { .. } => Selection::empty(),
+ SFArguments::Monadic { es: _, left } => left.clone(),
+ SFArguments::Dyadic {
+ es: _,
+ left,
+ right: _,
+ } => left.clone(),
+ })
+ } else {
+ Ok(result)
+ }
+}
+
+// ----------------------------------------------------------------
pub fn empty_sequential(_: SFArguments) -> SFResult {
Ok(Selection::empty())
@@ -98,109 +172,3 @@ selection_function! {
pos: int.end,
})
}
-
-// pub fn transform_conditional(sf: Self) -> Self {
-// fn condition(mut pred: Selection, res: Selection) -> Selection {
-// if pred.is_empty() {
-// pred
-// } else {
-// res
-// }
-// }
-
-// let dyadic: Option<Box<dyn Fn(&EditorState, &Selection, &Selection) -> Result<_, _>>> =
-// if let Some(dyadic) = sf.dyadic {
-// Some(Box::new(move |es, left, right| {
-// Ok(condition((dyadic)(es, left, right)?, left.clone()))
-// }))
-// } else {
-// None
-// };
-
-// let monadic: Box<dyn Fn(&EditorState, &Selection) -> Result<_, _>> =
-// Box::new(move |es, left| Ok(condition((sf.monadic)(es, left)?, left.clone())));
-
-// SelectionFunction {
-// niladic: sf.niladic,
-// monadic,
-// dyadic,
-// }
-// }
-
-// pub fn complement_transform(sf: Self) -> Self {
-// fn complement_rank0(
-// es: &EditorState,
-// buffer_id: BufferID,
-// pos: usize,
-// ) -> Result<Selection, SFError> {
-// if let Some(buf) = es.get_buffer(buffer_id) {
-// let mut ranges = Vec::new();
-// if pos > 0 {
-// ranges.push(Interval::new(0, pos));
-// }
-// let max_pos = buf.max_pos();
-// if pos + 1 < max_pos {
-// ranges.push(Interval::new(pos + 1, max_pos));
-// }
-// Ok(Selection::Ranges { buffer_id, ranges })
-// } else {
-// Err(SFError::BufferNotFound(buffer_id))
-// }
-// }
-
-// fn complement_rank1(
-// es: &EditorState,
-// buffer_id: BufferID,
-// interval: &Interval,
-// ) -> Result<Selection, SFError> {
-// if let Some(buf) = es.get_buffer(buffer_id) {
-// let max_pos = buf.max_pos();
-// if interval.start == 0 && interval.end >= max_pos {
-// return Ok(Selection::empty());
-// }
-
-// let mut ranges = Vec::new();
-// if interval.start == 0 {
-// if interval.end < max_pos {
-// ranges.push(Interval::new(interval.end, max_pos));
-// }
-// } else if interval.end == max_pos {
-// ranges.push(Interval::new(0, interval.start));
-// } else {
-// ranges.push(Interval::new(0, interval.start));
-// ranges.push(Interval::new(interval.end, max_pos));
-// }
-// Ok(Selection::Ranges { buffer_id, ranges })
-// } else {
-// Err(SFError::BufferNotFound(buffer_id))
-// }
-// }
-
-// fn complement(
-// es: &EditorState,
-// selection: Selection,
-// ) -> Result<Selection, VectoriseError<SFError>> {
-// Selection::vectorise(complement_rank0, complement_rank1, es, &selection)
-// }
-
-// let dyadic: Option<Box<dyn Fn(&EditorState, &Selection, &Selection) -> Result<_, _>>> =
-// if let Some(dyadic) = sf.dyadic {
-// Some(Box::new(move |es, left, right| {
-// complement(es, (dyadic)(es, left, right)?)
-// }))
-// } else {
-// None
-// };
-
-// let monadic: Box<dyn Fn(&EditorState, &Selection) -> Result<_, _>> =
-// Box::new(move |es, left| complement(es, (sf.monadic)(es, left)?));
-
-// let niladic: Box<dyn Fn(&EditorState) -> Result<_, _>> =
-// Box::new(move |es| complement(es, (sf.niladic)(es)?));
-
-// SelectionFunction {
-// niladic,
-// monadic,
-// dyadic,
-// }
-// }