From be3eefc3a5ab575553d4d1b56e4203a6b692473b Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Wed, 29 Nov 2023 18:12:04 +0100 Subject: [PATCH] edit tree: split node struct into sub structs --- examples/tty-02-node/src/main.rs | 23 ++-- lib-nested-core/src/edit_tree/nav.rs | 4 - lib-nested-core/src/edit_tree/node.rs | 110 +++++++------------- lib-nested-core/src/editors/list/editor.rs | 10 +- lib-nested-core/src/editors/typeterm/mod.rs | 18 ++-- lib-nested-core/src/repr_tree/context.rs | 2 +- lib-nested-tty/src/editors/list.rs | 12 +-- lib-nested-tty/src/lib.rs | 2 +- 8 files changed, 72 insertions(+), 109 deletions(-) diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index 4a027f5..d6199a8 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -28,7 +28,7 @@ use { fn node_make_char_view( node: NestedNode ) -> NestedNode { - node.display + node.disp.view .write().unwrap() .insert_branch(ReprTree::new_leaf( Context::parse(&node.ctx, "TerminalView"), @@ -45,15 +45,13 @@ fn node_make_char_view( node } -fn node_make_list_view( +fn node_make_seq_view( mut node: NestedNode ) -> NestedNode { - eprintln!("add list display type"); - node.display + node.disp.view .write().unwrap() .insert_branch(ReprTree::new_leaf( Context::parse(&node.ctx, "TerminalView"), - node.data .read() .unwrap() @@ -65,8 +63,13 @@ fn node_make_list_view( .flatten() .into() )); + node +} -// nested_tty::editors::list::PTYListStyle::for_node( &mut node, ("(", ",", ")") ); +fn node_make_list_edit( + mut node: NestedNode +) -> NestedNode { + nested_tty::editors::list::PTYListStyle::for_node( &mut node, ("(", ",", ")") ); nested_tty::editors::list::PTYListController::for_node( &mut node, None, None ); node @@ -78,10 +81,9 @@ fn node_make_view( if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "Char") { node_make_char_view( node ) } else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { - node_make_list_view( node ) -// node + node_make_seq_view( node ) } else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { - node_make_list_view( node ) + node_make_list_edit( node ) } else { eprintln!("couldnt add view"); node @@ -130,7 +132,7 @@ async fn main() { let ctx = ctx.clone(); let mut node1 = node1.clone(); let mut node2 = node2.clone(); - move |ev| { + move |ev| { let mut node1 = node1.clone(); let mut node2 = node2.clone(); node1.send_cmd_obj( ev.to_repr_tree(&ctx) ); @@ -151,7 +153,6 @@ async fn main() { .offset(Vector2::new(5, 0)), ); - let label = ctx.read().unwrap().type_term_to_str( &node1.get_type() ); compositor.write().unwrap() .push(nested_tty::make_label( &label ).offset(Vector2::new(0, 2))); diff --git a/lib-nested-core/src/edit_tree/nav.rs b/lib-nested-core/src/edit_tree/nav.rs index f974c08..a47204d 100644 --- a/lib-nested-core/src/edit_tree/nav.rs +++ b/lib-nested-core/src/edit_tree/nav.rs @@ -36,10 +36,6 @@ pub enum TreeNavCmd { pxev_dn_qnexd } -impl TreeNavCmd { - -} - pub trait TreeNav { /* CORE */ diff --git a/lib-nested-core/src/edit_tree/node.rs b/lib-nested-core/src/edit_tree/node.rs index 2458766..84d0412 100644 --- a/lib-nested-core/src/edit_tree/node.rs +++ b/lib-nested-core/src/edit_tree/node.rs @@ -13,33 +13,31 @@ use { } }; -//* TODO: refactoring proposal -/* - -struct NestedNodeDisplay { +#[derive(Clone)] +pub struct NestedNodeDisplay { /// display view - pub view: Option< Arc> >, + pub view: Arc>, /// diagnostics - pub diag: Option< OuterViewPort> >, + pub diag: Option< OuterViewPort> >, /// depth - pub depth: SingletonBuffer< usize >, + pub depth: OuterViewPort>, } -struct NestedNodeEdit { +#[derive(Clone)] +pub struct NestedNodeEdit { /// abstract editor pub editor: SingletonBuffer< Option< Arc > - >, + >, - pub spillbuf: VecBuffer< NestedNode >, + pub spillbuf: Arc> > >>, /// commander & navigation pub cmd: SingletonBuffer< Option< Arc> > >, /// abstract data view - pub data: Arc>, pub close_char: SingletonBuffer< Option< char > >, @@ -49,7 +47,8 @@ struct NestedNodeEdit { >, } -pub struct NewNestedNode { +#[derive(Clone)] +pub struct NestedNode { /// context pub ctx: Arc>, @@ -62,57 +61,23 @@ pub struct NewNestedNode { /// editor & commander objects pub edit: NestedNodeEdit } -*/ - -#[derive(Clone)] -pub struct NestedNode { - /// context - pub ctx: Arc>, - - /// abstract data view - pub data: Arc>, - - /// display view - pub display: Arc>, - - /// diagnostics - pub diag: Option< OuterViewPort> >, - - /// depth - pub depth: OuterViewPort< dyn SingletonView >, - - /// abstract editor - pub editor: SingletonBuffer< - Option< Arc > - >, - - pub spillbuf: Arc>>>>, - - /// commander & navigation - pub cmd: SingletonBuffer< - Option< Arc> > - >, - pub close_char: SingletonBuffer< - Option< char > - >, - pub tree_nav: SingletonBuffer< - Option< Arc> > - >, -} impl NestedNode { pub fn new(ctx: Arc>, data: Arc>, depth: OuterViewPort>) -> Self { NestedNode { + disp: NestedNodeDisplay { + view: ReprTree::new_arc(Context::parse(&ctx, "Display")), + diag: None, + depth, + }, + edit: NestedNodeEdit { + editor: SingletonBuffer::new(None), + spillbuf: Arc::new(RwLock::new(Vec::new())), + cmd: SingletonBuffer::new(None), + close_char: SingletonBuffer::new(None), + tree_nav: SingletonBuffer::new(None), + }, data, - display: Arc::new(RwLock::new(ReprTree::new(Context::parse(&ctx, "Display")))), - diag: None, - depth, - editor: SingletonBuffer::new(None), - spillbuf: Arc::new(RwLock::new(Vec::new())), - cmd: SingletonBuffer::new(None), - close_char: SingletonBuffer::new(None), - tree_nav: SingletonBuffer::new(None), - ctx } } @@ -147,29 +112,29 @@ impl NestedNode { //\\//\\ pub fn set_editor(mut self, editor: Arc) -> Self { - self.editor.set(Some(editor)); + self.edit.editor.set(Some(editor)); self } pub fn set_cmd(mut self, cmd: Arc>) -> Self { - self.cmd.set(Some(cmd)); + self.edit.cmd.set(Some(cmd)); self } pub fn set_nav(mut self, nav: Arc>) -> Self { - self.tree_nav.set(Some(nav)); + self.edit.tree_nav.set(Some(nav)); self } pub fn set_diag(mut self, diag: OuterViewPort>) -> Self { - self.diag = Some(diag); + self.disp.diag = Some(diag); self } //\\//\\ pub fn get_diag(&self) -> OuterViewPort> { - self.diag.clone().unwrap_or(ViewPort::new().into_outer()) + self.disp.diag.clone().unwrap_or(ViewPort::new().into_outer()) } pub fn get_data_port<'a, V: View + ?Sized + 'static>(&'a self, type_str: impl Iterator) -> Option> @@ -200,7 +165,7 @@ impl NestedNode { */ pub fn get_edit(&self) -> Option>> { - if let Some(edit) = self.editor.get() { + if let Some(edit) = self.edit.editor.get() { if let Ok(edit) = edit.downcast::>() { Some(edit) } else { @@ -226,7 +191,7 @@ impl TreeType for NestedNode { impl TreeNav for NestedNode { fn get_cursor(&self) -> TreeCursor { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.read().unwrap().get_cursor() } else { TreeCursor::default() @@ -234,7 +199,7 @@ impl TreeNav for NestedNode { } fn get_addr_view(&self) -> OuterViewPort> { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.read().unwrap().get_addr_view() } else { OuterViewPort::default() @@ -242,7 +207,7 @@ impl TreeNav for NestedNode { } fn get_mode_view(&self) -> OuterViewPort> { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.read().unwrap().get_mode_view() } else { OuterViewPort::default() @@ -250,7 +215,7 @@ impl TreeNav for NestedNode { } fn get_cursor_warp(&self) -> TreeCursor { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.read().unwrap().get_cursor_warp() } else { TreeCursor::default() @@ -258,7 +223,7 @@ impl TreeNav for NestedNode { } fn get_height(&self, op: &TreeHeightOp) -> usize { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.read().unwrap().get_height( op ) } else { 0 @@ -266,7 +231,7 @@ impl TreeNav for NestedNode { } fn goby(&mut self, direction: Vector2) -> TreeNavResult { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.write().unwrap().goby(direction) } else { TreeNavResult::Exit @@ -274,7 +239,7 @@ impl TreeNav for NestedNode { } fn goto(&mut self, new_cursor: TreeCursor) -> TreeNavResult { - if let Some(tn) = self.tree_nav.get() { + if let Some(tn) = self.edit.tree_nav.get() { tn.write().unwrap().goto(new_cursor) } else { TreeNavResult::Exit @@ -303,7 +268,7 @@ impl ObjCommander for NestedNode { } else { TreeNavResult::Exit } - } else if let Some(cmd) = self.cmd.get() { + } else if let Some(cmd) = self.edit.cmd.get() { // todo: filter out tree-nav cmds and send them to tree_nav cmd.write().unwrap().send_cmd_obj(cmd_obj) } else { @@ -312,6 +277,7 @@ impl ObjCommander for NestedNode { } } + impl Diagnostics for NestedNode { fn get_msg_port(&self) -> OuterViewPort> { self.get_diag() diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs index 85533ae..087861b 100644 --- a/lib-nested-core/src/editors/list/editor.rs +++ b/lib-nested-core/src/editors/list/editor.rs @@ -131,7 +131,7 @@ impl ListEditor { .flatten() ); - node.spillbuf = e.spillbuf.clone(); + node.edit.spillbuf = e.spillbuf.clone(); node } @@ -229,7 +229,7 @@ impl ListEditor { /// insert a new element pub fn insert(&mut self, item: Arc>) { - item.read().unwrap().depth.0.set_view( + item.read().unwrap().disp.depth.0.set_view( self.depth.map(|d| d+1).get_view() ); @@ -306,7 +306,7 @@ impl ListEditor { self.set_leaf_mode(ListCursorMode::Insert); self.nexd(); - let mut b = item.spillbuf.write().unwrap(); + let mut b = item.edit.spillbuf.write().unwrap(); let mut tail_node = Context::make_node(&self.ctx, self.typ.clone(), self.depth.map(|d| d+1)).unwrap(); tail_node.goto(TreeCursor::home()); @@ -364,7 +364,7 @@ impl ListEditor { let old_cur = pxv_editor.get_cursor(); - let data = cur_editor.spillbuf.read().unwrap(); + let data = cur_editor.edit.spillbuf.read().unwrap(); for x in data.iter() { pxv_editor.send_cmd_obj( ReprTree::new_leaf( @@ -422,7 +422,7 @@ impl ListEditor { leaf_mode: ListCursorMode::Insert }); - let data = nxd_editor.spillbuf.read().unwrap(); + let data = nxd_editor.edit.spillbuf.read().unwrap(); for x in data.iter() { cur_editor.send_cmd_obj( diff --git a/lib-nested-core/src/editors/typeterm/mod.rs b/lib-nested-core/src/editors/typeterm/mod.rs index 0b8b3b6..abaaaab 100644 --- a/lib-nested-core/src/editors/typeterm/mod.rs +++ b/lib-nested-core/src/editors/typeterm/mod.rs @@ -168,8 +168,8 @@ impl TypeTermEditor { node.goto(TreeCursor::home()); - let _editor = node.editor.get(); - self.close_char.set(node.close_char.get()); + let _editor = node.edit.editor.get(); + self.close_char.set(node.edit.close_char.get()); self.cur_node.set(node); self.state = new_state; } @@ -204,7 +204,7 @@ impl TypeTermEditor { cur_node: SingletonBuffer::new(cur_node.clone()), close_char: SingletonBuffer::new(None), spillbuf: Arc::new(RwLock::new(Vec::new())), - depth: cur_node.depth.clone() + depth: cur_node.disp.depth.clone() }; /* FIXME let view = editor.cur_node @@ -215,24 +215,24 @@ impl TypeTermEditor { .to_grid() .flatten(); */ - let _cc = editor.cur_node.get().close_char; + let _cc = editor.cur_node.get().edit.close_char; let editor = Arc::new(RwLock::new(editor)); - let mut super_node = NestedNode::new(ctx, data, cur_node.depth) + let mut super_node = NestedNode::new(ctx, data, cur_node.disp.depth) // .set_view(view) .set_nav(editor.clone()) .set_cmd(editor.clone()) .set_editor(editor.clone()); - editor.write().unwrap().close_char = super_node.close_char.clone(); - super_node.spillbuf = editor.read().unwrap().spillbuf.clone(); + editor.write().unwrap().close_char = super_node.edit.close_char.clone(); + super_node.edit.spillbuf = editor.read().unwrap().spillbuf.clone(); super_node } fn forward_spill(&mut self) { let node = self.cur_node.get(); - let mut buf = node.spillbuf.write().unwrap(); + let mut buf = node.edit.spillbuf.write().unwrap(); for n in buf.iter() { self.spillbuf.write().unwrap().push(n.clone()); } @@ -371,7 +371,7 @@ impl TypeTermEditor { * that has same state & child-node as current node. */ let old_edit_node = TypeTermEditor::new_node( self.ctx.clone(), SingletonBuffer::new(0).get_port() ); - old_node.depth.0.set_view( old_edit_node.depth.map(|x|x).get_view() ); + old_node.disp.depth.0.set_view( old_edit_node.disp.depth.map(|x|x).get_view() ); let old_edit_clone = old_edit_node.get_edit::().unwrap(); old_edit_clone.write().unwrap().set_state( self.state ); diff --git a/lib-nested-core/src/repr_tree/context.rs b/lib-nested-core/src/repr_tree/context.rs index 6eaffd6..56d331d 100644 --- a/lib-nested-core/src/repr_tree/context.rs +++ b/lib-nested-core/src/repr_tree/context.rs @@ -260,7 +260,7 @@ impl Context { drop(dict); self.add_morphism(morphism_pattern, Arc::new(move |node, dst_type| { - mk_editor(node.ctx.clone(), dst_type, node.depth) + mk_editor(node.ctx.clone(), dst_type, node.disp.depth) })); } diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index 0fff8ec..fb3f9a4 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -46,7 +46,7 @@ impl DisplaySegment for ListSegment { atom .add_style_back(bg_style_from_depth(select)) .add_style_back(TerminalStyle::bold(select==1)) - .add_style_back(fg_style_from_depth(e.depth.get_view().get())) + .add_style_back(fg_style_from_depth(e.disp.depth.get_view().get())) }) } } @@ -92,7 +92,7 @@ impl PTYListStyle { } pub fn for_node(node: &mut NestedNode, style: (&str, &str, &str)) { - node.display + node.disp.view .write().unwrap() .insert_branch(ReprTree::new_leaf( Context::parse(&node.ctx, "TerminalView"), @@ -152,10 +152,10 @@ impl PTYListController { } let editor = node.get_edit::().unwrap(); - let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.depth.clone() ))); + let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.disp.depth.clone() ))); - node.cmd.set(Some(controller.clone())); - node.close_char.set(close_char); + node.edit.cmd.set(Some(controller.clone())); + node.edit.close_char.set(close_char); } pub fn get_data_port(&self) -> OuterViewPort> { @@ -235,7 +235,7 @@ impl PTYListController { ListCursorMode::Select => { if let Some(item) = e.get_item_mut() { let res = item.write().unwrap().send_cmd_obj(cmd_obj.clone()); - let child_close_char = item.read().unwrap().close_char.get(); + let child_close_char = item.read().unwrap().edit.close_char.get(); match res { TreeNavResult::Continue => TreeNavResult::Continue, diff --git a/lib-nested-tty/src/lib.rs b/lib-nested-tty/src/lib.rs index b8b780f..e7c03a3 100644 --- a/lib-nested-tty/src/lib.rs +++ b/lib-nested-tty/src/lib.rs @@ -44,7 +44,7 @@ use std::sync::{Arc, RwLock}; impl DisplaySegment for nested::edit_tree::NestedNode { fn display_view(&self) -> OuterViewPort { - if let Some( tv_repr ) = self.display + if let Some( tv_repr ) = self.disp.view .read().unwrap() .descend( Context::parse(&self.ctx, "TerminalView") ) {