From 0d073ba82e2147d248f3dae1ff81860f7116b38e Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 7 Jan 2025 16:34:58 +0100 Subject: [PATCH] make ListSegments a ListView instead of SequenceView --- lib-nested-core/src/editors/list/editor.rs | 7 +- lib-nested-core/src/editors/list/mod.rs | 3 +- lib-nested-core/src/editors/list/segment.rs | 105 ++++++++++++++------ lib-nested-tty/src/editors/list.rs | 17 ++-- 4 files changed, 83 insertions(+), 49 deletions(-) diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs index 9846026..37beecc 100644 --- a/lib-nested-core/src/editors/list/editor.rs +++ b/lib-nested-core/src/editors/list/editor.rs @@ -139,7 +139,8 @@ impl ListEditor { .set_editor(editor.clone()) .set_nav(editor.clone()) .set_cmd(editor.clone()) - .set_diag(e.get_edittree_seq() + .set_diag(e.get_edittree_list() + .to_sequence() .enumerate() .map(|(idx, item_editor)| { let idx = *idx; @@ -201,10 +202,6 @@ impl ListEditor { }) } - pub fn get_edittree_seq(&self) -> OuterViewPort> { - self.get_edittree_list().to_sequence() - } - /* pub fn get_data(&self) -> Arc> { let data_view = self.get_data_port(); diff --git a/lib-nested-core/src/editors/list/mod.rs b/lib-nested-core/src/editors/list/mod.rs index b788aae..4f10367 100644 --- a/lib-nested-core/src/editors/list/mod.rs +++ b/lib-nested-core/src/editors/list/mod.rs @@ -9,8 +9,7 @@ pub mod ctx; pub use { cursor::{ListCursor, ListCursorMode}, editor::ListEditor, - segment::{ListSegment, ListSegmentSequence}, + segment::{ListSegment, ListSegments}, cmd::ListCmd, ctx::init_ctx }; - diff --git a/lib-nested-core/src/editors/list/segment.rs b/lib-nested-core/src/editors/list/segment.rs index 6f25782..0245daf 100644 --- a/lib-nested-core/src/editors/list/segment.rs +++ b/lib-nested-core/src/editors/list/segment.rs @@ -4,7 +4,9 @@ use { Observer, ObserverBroadcast, ObserverExt, OuterViewPort, View, ViewPort, singleton::*, sequence::*, + list::* }, + buffer::vec::*, projection::projection_helper::* }, crate::{ @@ -15,6 +17,7 @@ use { std::sync::RwLock, }; +#[derive(Clone)] pub enum ListSegment { InsertCursor, Item { @@ -25,24 +28,22 @@ pub enum ListSegment { /* todo: switch to ListView instead of SequenceView */ -pub struct ListSegmentSequence { - data: Arc>, +pub struct ListSegments { + data: Arc>, cursor: Arc>, cur_cursor: ListCursor, - port: ViewPort>, - cast: Arc>>>, + port: ViewPort>, + cast: Arc>>>, proj_helper: ProjectionHelper, } -impl View for ListSegmentSequence { - type Msg = usize; +impl View for ListSegments { + type Msg = ListDiff; } -impl SequenceView for ListSegmentSequence { - type Item = ListSegment; - +impl ListView for ListSegments { fn len(&self) -> Option { let l = self.data.len()?; match self.cur_cursor.mode { @@ -53,7 +54,7 @@ impl SequenceView for ListSegmentSequence { } } - fn get(&self, idx: &usize) -> Option { + fn get(&self, idx: &usize) -> Option { let c = self.cursor.get(); Some(if let Some(cur) = c.idx { match c.mode { @@ -88,41 +89,82 @@ impl SequenceView for ListSegmentSequence { } } -impl ListSegmentSequence { +impl ListSegments { pub fn new( cursor_port: OuterViewPort>, - data_port: OuterViewPort>, + data_port: OuterViewPort>, ) -> Arc> { let out_port = ViewPort::new(); let mut proj_helper = ProjectionHelper::new(out_port.update_hooks.clone()); - let proj = Arc::new(RwLock::new(ListSegmentSequence { + let proj = Arc::new(RwLock::new(ListSegments { cur_cursor: cursor_port.get_view().get(), port: out_port.clone(), cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| { - let _old_cursor = s.cur_cursor; - s.cur_cursor = s.cursor.get(); + let old_cursor = s.cur_cursor; + let new_cursor = s.cursor.get(); + s.cur_cursor = new_cursor; - // todo: optimize - s.cast.notify_each(0..=s.data.len().unwrap_or(0) + 1); + if old_cursor.mode == ListCursorMode::Insert { + if let Some(idx) = old_cursor.idx { + s.cast.notify(&ListDiff::Remove(idx as usize)); + } + } + if new_cursor.mode == ListCursorMode::Insert { + if let Some(idx) = new_cursor.idx { + s.cast.notify(&ListDiff::Insert { idx: idx as usize, val: ListSegment::InsertCursor }); + } + } }), - data: proj_helper.new_sequence_arg(1, data_port, |s: &mut Self, idx| { + data: proj_helper.new_list_arg(1, data_port, |s: &mut Self, diff| { + let mut target_idx = match diff { + ListDiff::Clear => 0, + ListDiff::Remove(idx) => *idx, + ListDiff::Insert { idx, val } => *idx, + ListDiff::Update { idx, val } => *idx + }; + let mut cur_dist = 0; + if let Some(cur_idx) = s.cur_cursor.idx { - match s.cur_cursor.mode { - ListCursorMode::Insert => { - if *idx < cur_idx as usize { - s.cast.notify(idx); - } else { - s.cast.notify(&(*idx + 1)); - } - } - _ => { - s.cast.notify(idx); + if s.cur_cursor.mode == ListCursorMode::Insert { + if cur_idx < target_idx as isize { + target_idx += 1; } } - } else { - s.cast.notify(idx); + + cur_dist = target_idx as isize - cur_idx; + } + + match diff { + ListDiff::Clear => { + s.cast.notify(&ListDiff::Clear); + + if s.cur_cursor.mode == ListCursorMode::Insert { + s.cast.notify(&ListDiff::Insert { idx: 0, val: ListSegment::InsertCursor }); + } + } + ListDiff::Remove(idx) => { + s.cast.notify(&ListDiff::Remove(target_idx)); + } + ListDiff::Insert { idx, val } => { + s.cast.notify(&ListDiff::Insert { + idx: target_idx, + val: ListSegment::Item { + editor: val.clone(), + cur_dist + } + }); + } + ListDiff::Update { idx, val } => { + s.cast.notify(&ListDiff::Update { + idx: target_idx, + val: ListSegment::Item { + editor: val.clone(), + cur_dist + } + }) + } } }), cast: out_port.inner().get_broadcast(), @@ -131,11 +173,10 @@ impl ListSegmentSequence { proj.write().unwrap().proj_helper.set_proj(&proj); out_port.inner().set_view(Some(proj.clone())); - proj } - pub fn get_view(&self) -> OuterViewPort> { + pub fn get_view(&self) -> OuterViewPort> { self.port.outer() } } diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index 63b4ca0..8da07cd 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -1,6 +1,6 @@ use { r3vi::{ - view::{ViewPort, OuterViewPort, sequence::*}, + view::{ViewPort, OuterViewPort, sequence::*, list::*}, projection::decorate_sequence::*, }, nested::{ @@ -67,19 +67,19 @@ impl PTYListStyle { } } - pub fn get_seg_seq_view(&self, editor: &ListEditor) -> OuterViewPort>> { - let seg_seq = ListSegmentSequence::new( + pub fn get_seg_view_list(&self, editor: &ListEditor) -> OuterViewPort>> { + let seg_seq = ListSegments::new( editor.get_cursor_port(), - editor.get_edittree_seq() + editor.get_edittree_list() ); let se = seg_seq.read().unwrap(); se.get_view().map(move |segment| segment.display_view()) } pub fn pty_view(&self, editor: &ListEditor) -> OuterViewPort { - let seg_seq = ListSegmentSequence::new( + let seg_seq = ListSegments::new( editor.get_cursor_port(), - editor.get_edittree_seq() + editor.get_edittree_list() ); let seg_seq0 = seg_seq.read().unwrap(); @@ -88,6 +88,7 @@ impl PTYListStyle { seg_seq .map(move |segment| segment.display_view()) + .to_sequence() .separate(make_label(&self.style.1)) .wrap(make_label(&self.style.0), make_label(&self.style.2)) .to_grid_horizontal() @@ -163,10 +164,6 @@ impl PTYListController { node.ctrl.close_char.set(close_char); } - pub fn get_data_port(&self) -> OuterViewPort> { - self.editor.read().unwrap().get_edittree_seq() - } - pub fn clear(&mut self) { self.editor.write().unwrap().clear(); }