From 10bff8a32cfba56ef31671883b44c21fab64266b Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sat, 11 Nov 2023 18:00:26 +0100 Subject: [PATCH] list editor: add some bounds checks and debug messages --- nested/src/editors/list/cmd.rs | 4 ++++ nested/src/editors/list/editor.rs | 34 ++++++++++++++++++++++++--- nested/src/editors/list/nav.rs | 5 ++-- nested/src/editors/list/pty_editor.rs | 2 +- nested/src/editors/typeterm/mod.rs | 2 +- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/nested/src/editors/list/cmd.rs b/nested/src/editors/list/cmd.rs index 6c10b30..d991d5d 100644 --- a/nested/src/editors/list/cmd.rs +++ b/nested/src/editors/list/cmd.rs @@ -58,6 +58,8 @@ impl ObjCommander for ListEditor { } else if let Some(cmd) = cmd_repr.get_view::>() { + eprintln!("pty-list-editor some list cmmd"); + let cur = self.cursor.get(); drop(cmd_repr); @@ -125,6 +127,7 @@ impl ObjCommander for ListEditor { ListCursorMode::Insert => { match cmd.get() { ListCmd::DeletePxev => { + eprintln!("INSERT: delete pxev"); self.delete_pxev(); TreeNavResult::Continue } @@ -147,6 +150,7 @@ impl ObjCommander for ListEditor { } } } else { + eprintln!("ptylist: cursor has no idx"); TreeNavResult::Exit } } diff --git a/nested/src/editors/list/editor.rs b/nested/src/editors/list/editor.rs index 7875864..0ba346b 100644 --- a/nested/src/editors/list/editor.rs +++ b/nested/src/editors/list/editor.rs @@ -194,6 +194,7 @@ impl ListEditor { } } + /// is the element-type also a list-like editor (i.e. impls TreeNav) pub fn is_listlist(&self) -> bool { self.ctx.read().unwrap().is_list_type(&self.typ) } @@ -233,6 +234,7 @@ impl ListEditor { /// insert a new element pub fn insert(&mut self, item: Arc>) { eprintln!("list insert"); + item.read().unwrap().depth.0.set_view( self.depth.map(|d| d+1).get_view() ); @@ -277,6 +279,27 @@ impl ListEditor { ); self.data.remove(idx); } + + /* in case the split leaves an empty item-list + * as a last element, remove it + */ +/* + if self.is_listlist() { + if idx > 0 && idx < self.data.len()+1 { + /* we are in insert mode, + * get element before cursor + */ + let prev_idx = idx - 1; + let prev_node = self.data.get(prev_idx); + let prev_node = prev_node.read().unwrap(); + + if prev_node.get_data_view::>(vec![].into_iter()).iter().count() == 0 { + drop(prev_node); + self.data.remove(prev_idx); + } + } + } + */ } } @@ -314,7 +337,7 @@ impl ListEditor { drop(item); tail_node.goto(TreeCursor::home()); - if cur.tree_addr.len() > 2 { + if cur.tree_addr.len() > 1 { tail_node.dn(); } @@ -364,13 +387,15 @@ impl ListEditor { ); } + + // fixme: is it oc0 or old_cur ?? if oc0.tree_addr.len() > 1 { pxv_editor.goto(TreeCursor { tree_addr: vec![ old_cur.tree_addr[0], 0 ], leaf_mode: ListCursorMode::Insert }); pxv_editor.send_cmd_obj(ListCmd::DeletePxev.into_repr_tree( &self.ctx )); - } else { + } else if oc0.tree_addr.len() > 0 { pxv_editor.goto(TreeCursor { tree_addr: vec![ old_cur.tree_addr[0] ], leaf_mode: ListCursorMode::Insert @@ -421,17 +446,20 @@ impl ListEditor { ); } + // fixme: is it oc0 or old_cur ?? if oc0.tree_addr.len() > 1 { cur_editor.goto(TreeCursor { tree_addr: vec![ old_cur.tree_addr[0], -1 ], leaf_mode: ListCursorMode::Insert }); cur_editor.send_cmd_obj(ListCmd::DeleteNexd.into_repr_tree( &self.ctx )); - } else { + } else if oc0.tree_addr.len() > 0 { cur_editor.goto(TreeCursor { tree_addr: vec![ old_cur.tree_addr[0] ], leaf_mode: ListCursorMode::Insert }); + } else { + cur_editor.goto(TreeCursor::none()); } } diff --git a/nested/src/editors/list/nav.rs b/nested/src/editors/list/nav.rs index 9842ef9..31d0cc2 100644 --- a/nested/src/editors/list/nav.rs +++ b/nested/src/editors/list/nav.rs @@ -195,6 +195,7 @@ impl TreeNav for ListEditor { TreeNavResult::Exit } else if direction.y > 0 { // dn + eprintln!("dn: data.len() = {}", self.data.len()); self.cursor.set(ListCursor { mode: if self.data.len() > 0 { cur.leaf_mode } else { ListCursorMode::Insert }, idx: Some(0) @@ -210,18 +211,18 @@ impl TreeNav for ListEditor { 1 => { if direction.y > 0 { // dn + if cur.tree_addr[0] < self.data.len() as isize { if self.data .get_mut(cur.tree_addr[0] as usize) .write().unwrap() .goby(Vector2::new(direction.x, direction.y)) == TreeNavResult::Continue { - let res = self.cursor.set(ListCursor { + self.cursor.set(ListCursor { mode: ListCursorMode::Select, idx: Some(cur.tree_addr[0]) }); self.set_leaf_mode(cur.leaf_mode); - res } } diff --git a/nested/src/editors/list/pty_editor.rs b/nested/src/editors/list/pty_editor.rs index ccb5812..c85b9aa 100644 --- a/nested/src/editors/list/pty_editor.rs +++ b/nested/src/editors/list/pty_editor.rs @@ -176,7 +176,7 @@ impl PTYListController { match new_edit.send_cmd_obj(cmd_obj.clone()) { TreeNavResult::Continue => { - e.insert(Arc::new(RwLock::new(new_edit))); + e.insert(Arc::new(RwLock::new(new_edit.clone()))); TreeNavResult::Continue } TreeNavResult::Exit => { diff --git a/nested/src/editors/typeterm/mod.rs b/nested/src/editors/typeterm/mod.rs index 172cb26..b53b0a8 100644 --- a/nested/src/editors/typeterm/mod.rs +++ b/nested/src/editors/typeterm/mod.rs @@ -40,7 +40,7 @@ pub struct TypeTermEditor { depth: OuterViewPort>, buf: SingletonBuffer< TypeTerm >, - + // editing/parsing state state: State,