aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/selection_functions/result_transformation.rs
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2025-10-31 11:29:08 +0000
committertslil <tslil@posteo.de>2025-10-31 11:55:36 +0000
commitf7c23142e8c73732fe3f75d03dc36e905f28715b (patch)
tree7dc1e0c6919cc9469f013b6015d4dc9356c7a9c9 /rprt-engine/src/selection_functions/result_transformation.rs
parentc16e21d82e3b398c572e48ccbf4ff896086425ef (diff)
Refactor selection functions into a module, one function per file
Diffstat (limited to 'rprt-engine/src/selection_functions/result_transformation.rs')
-rw-r--r--rprt-engine/src/selection_functions/result_transformation.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/rprt-engine/src/selection_functions/result_transformation.rs b/rprt-engine/src/selection_functions/result_transformation.rs
new file mode 100644
index 0000000..89a071a
--- /dev/null
+++ b/rprt-engine/src/selection_functions/result_transformation.rs
@@ -0,0 +1,77 @@
+use crate::buffer::BufferID;
+use crate::selection::{Interval, Selection};
+use crate::selection_functions::types::{SFArguments, SFError, SFResult};
+use crate::state::EditorState;
+
+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)
+ }
+}