From 0bcfd7a65aeb8696c79530df49f29fc520f93bf1 Mon Sep 17 00:00:00 2001
From: Michael Sippel <micha@fragmental.art>
Date: Fri, 9 Aug 2024 23:00:31 +0200
Subject: [PATCH] call edittree_hook in morphisms already, add edittree()
 getter in ReprTreeExt to further simplify application code

---
 examples/tty-02-digit/src/main.rs          | 46 +++++++---------------
 examples/tty-03-string/src/main.rs         | 12 +-----
 examples/tty-04-posint/src/main.rs         | 42 +++++++++-----------
 lib-nested-core/src/editors/digit/ctx.rs   |  2 +
 lib-nested-core/src/editors/list/ctx.rs    |  2 +
 lib-nested-core/src/editors/list/editor.rs |  4 +-
 lib-nested-core/src/repr_tree/context.rs   |  4 +-
 lib-nested-core/src/repr_tree/mod.rs       |  5 +++
 lib-nested-tty/src/editors/list.rs         |  5 +--
 9 files changed, 49 insertions(+), 73 deletions(-)

diff --git a/examples/tty-02-digit/src/main.rs b/examples/tty-02-digit/src/main.rs
index 60f6032..ad50898 100644
--- a/examples/tty-02-digit/src/main.rs
+++ b/examples/tty-02-digit/src/main.rs
@@ -84,30 +84,15 @@ async fn main() {
         }
     );
 
-    /* setup TTY-Display for DigitEditor
-     *  and call the hook defined above with `set_edittree_hook()`
-     *
-     */
-    let edittree_digit = ctx.read().unwrap()
-        .setup_edittree(
-            rt_digit.clone(),
-            SingletonBuffer::new(0).get_port()
-        ).unwrap();
-
-    let mut digit_u64_buffer = rt_digit
-        .descend(Context::parse(&ctx, "ℤ_2^64~machine.UInt64")).unwrap()
-        .singleton_buffer::<u64>();
-
     /* setup terminal
      */
     let app = TTYApplication::new({
         /* event handler
          */
         let ctx = ctx.clone();
-
-        let mut edittree_digit = edittree_digit.clone();
+        let digit_edittree = rt_digit.edittree( &ctx );
         move |ev| {
-            edittree_digit.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx));
+            digit_edittree.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx));
         }
     });
 
@@ -124,27 +109,26 @@ async fn main() {
                 .map_item(|p, a| {
                     a.add_style_back(TerminalStyle::fg_color(((25 * p.x % 255) as u8, 200, 0)))
                 })
-                .offset(Vector2::new(5, 0)),
-        );
+                .offset(Vector2::new(5, 0)));
 
         let label_str = ctx.read().unwrap().type_term_to_str(&rt_digit.read().unwrap().get_type());
         comp.push(
             nested_tty::make_label(&label_str)
             .map_item(|_pt,atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90))))
-            .offset(Vector2::new(1, 1))
-        );
-        comp.push(
-            edittree_digit.get().read().unwrap().display_view()
-            .offset(Vector2::new(3,2))
-        );
-        comp.push(
-            digit_u64_buffer.get_port().map(
-                |d| nested_tty::make_label(&format!("Digit value={}", d))
-            )
+            .offset(Vector2::new(1, 1)));
+
+        comp.push(rt_digit
+            .edittree( &ctx ).get().read().unwrap()
+            .display_view()
+            .offset(Vector2::new(3,2)));
+
+        comp.push(rt_digit
+            .descend(Context::parse(&ctx, "ℤ_2^64~machine.UInt64")).unwrap()
+            .view_u64()
+            .map(|d| nested_tty::make_label(&format!("Digit value={}", d)))
             .to_grid()
             .flatten()
-            .offset(Vector2::new(5,3))
-        );
+            .offset(Vector2::new(5,3)));
     }
 
     /* write the changes in the view of `term_port` to the terminal
diff --git a/examples/tty-03-string/src/main.rs b/examples/tty-03-string/src/main.rs
index 3cdc66e..d5f8ff9 100644
--- a/examples/tty-03-string/src/main.rs
+++ b/examples/tty-03-string/src/main.rs
@@ -61,14 +61,6 @@ async fn main() {
     // .. avoid cycle of projections..
     rt_string.write().unwrap().detach(&ctx);
 
-    /* Setup the Editor-View for this ReprTree
-     */
-    let edittree_list = ctx.read().unwrap()
-        .setup_edittree(
-            rt_string.clone(),
-            SingletonBuffer::new(0).get_port()
-        ).unwrap();
-
     /* In order to get access to the values that are modified by the Editor,
      * we apply a morphism that, given the List of Edit-Trees, extracts
      * the value from each EditTree and shows them in a ListView.
@@ -117,7 +109,7 @@ async fn main() {
     /* setup terminal
      */
     let app = TTYApplication::new({
-        let edittree_list = edittree_list.clone();
+        let edittree_list = rt_string.edittree(&ctx).clone();
 
         /* event handler
          */
@@ -145,7 +137,7 @@ async fn main() {
                 .offset(Vector2::new(1,1)));
 
         comp.push(
-            edittree_list.get()
+            rt_string.edittree(&ctx).get()
                 .read().unwrap()
                 .display_view()
                 .offset(Vector2::new(3,2)));
diff --git a/examples/tty-04-posint/src/main.rs b/examples/tty-04-posint/src/main.rs
index 011ea32..906791e 100644
--- a/examples/tty-04-posint/src/main.rs
+++ b/examples/tty-04-posint/src/main.rs
@@ -81,27 +81,21 @@ async fn main() {
         }
     );
 
-    let edittree_hex_be_list = ctx.read().unwrap()
-        .setup_edittree(
-            rt_int.descend(Context::parse(&ctx,"
-                <PosInt 16 BigEndian>
-                ~ <Seq <Digit 16>>
-                ~ <List <Digit 16>>
-                ~ <List Char>
-            ")).expect("descend"),
-            SingletonBuffer::new(0).get_port()
-        ).unwrap().get();
+    let edittree_hex_be_list =
+        rt_int
+            .descend(Context::parse(&ctx, "
+                    <PosInt 16 BigEndian>
+                    ~ <Seq~List <Digit 16>~Char>
+                ")).unwrap()
+            .edittree( &ctx );
 
-    let edittree_dec_be_list = ctx.read().unwrap()
-        .setup_edittree(
-            rt_int.descend(Context::parse(&ctx,"
-                <PosInt 10 BigEndian>
-                ~ <Seq <Digit 10>>
-                ~ <List <Digit 10>>
-                ~ <List Char>
-            ")).expect("descend"),
-            SingletonBuffer::new(0).get_port()
-        ).unwrap().get();
+    let edittree_dec_be_list =
+        rt_int
+            .descend(Context::parse(&ctx, "
+                    <PosInt 10 BigEndian>
+                    ~ <Seq~List <Digit 10>~Char>
+                ")).unwrap()
+            .edittree( &ctx );
 
     let hex_digits_view = rt_int.descend(Context::parse(&ctx, "
               <PosInt 16 LittleEndian>
@@ -130,15 +124,15 @@ async fn main() {
     /* list of both editors
      */
     let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "<Seq Char>"));
-    list_editor.data.push( edittree_hex_be_list.clone() );
-    list_editor.data.push( edittree_dec_be_list.clone() );
+    list_editor.data.push( edittree_hex_be_list.get() );
+    list_editor.data.push( edittree_dec_be_list.get() );
     let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port());
 
     /* cursors are a bit screwed initially so fix them up
      * TODO: how to fix this generally?
      */
-    edittree_hex_be_list.write().unwrap().goto(TreeCursor::none());
-    edittree_dec_be_list.write().unwrap().goto(TreeCursor::none());
+    edittree_hex_be_list.get().write().unwrap().goto(TreeCursor::none());
+    edittree_dec_be_list.get().write().unwrap().goto(TreeCursor::none());
     edittree.goto(TreeCursor{
         leaf_mode: nested::editors::list::ListCursorMode::Insert,
         tree_addr: vec![0,0]
diff --git a/lib-nested-core/src/editors/digit/ctx.rs b/lib-nested-core/src/editors/digit/ctx.rs
index d2d22ae..c62b297 100644
--- a/lib-nested-core/src/editors/digit/ctx.rs
+++ b/lib-nested-core/src/editors/digit/ctx.rs
@@ -51,6 +51,8 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
                                 SingletonBuffer::new(Arc::new(RwLock::new(edittree)))
                             )
                         );
+
+                    ctx.read().unwrap().setup_edittree( src_rt );
                 }
         }
     );
diff --git a/lib-nested-core/src/editors/list/ctx.rs b/lib-nested-core/src/editors/list/ctx.rs
index 56f0cf5..e4c35fa 100644
--- a/lib-nested-core/src/editors/list/ctx.rs
+++ b/lib-nested-core/src/editors/list/ctx.rs
@@ -54,6 +54,8 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
                             SingletonBuffer::new(Arc::new(RwLock::new(edittree_list)))
                         )
                     );
+
+                    ctx.read().unwrap().setup_edittree( &src_rt );
                 } else {
                     eprintln!("no item type");
                 }
diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs
index 04cc63b..64c931f 100644
--- a/lib-nested-core/src/editors/list/editor.rs
+++ b/lib-nested-core/src/editors/list/editor.rs
@@ -321,8 +321,8 @@ impl ListEditor {
                 let rt = ReprTree::new_arc(self.typ.clone());
                 let mut et = self.ctx.read().unwrap()
                     .setup_edittree(
-                        rt,
-                        self.depth.map(|d| d+1)
+                        &rt
+//                      ,  self.depth.map(|d| d+1)
                     );
 
                 if let Some(edittree) = et.as_mut(){
diff --git a/lib-nested-core/src/repr_tree/context.rs b/lib-nested-core/src/repr_tree/context.rs
index 5e93f51..cca1894 100644
--- a/lib-nested-core/src/repr_tree/context.rs
+++ b/lib-nested-core/src/repr_tree/context.rs
@@ -206,8 +206,8 @@ impl Context {
 
     pub fn setup_edittree(
         &self,
-        rt: Arc<RwLock<ReprTree>>,
-        depth: OuterViewPort<dyn SingletonView<Item = usize>>
+        rt: &Arc<RwLock<ReprTree>>
+//        depth: OuterViewPort<dyn SingletonView<Item = usize>>
     ) -> Option<SingletonBuffer<Arc<RwLock<EditTree>>>> {
         if let Some(new_edittree) =
             rt.descend(self.type_term_from_str("EditTree").unwrap())
diff --git a/lib-nested-core/src/repr_tree/mod.rs b/lib-nested-core/src/repr_tree/mod.rs
index 5d05bf0..8af48f8 100644
--- a/lib-nested-core/src/repr_tree/mod.rs
+++ b/lib-nested-core/src/repr_tree/mod.rs
@@ -58,6 +58,11 @@ pub trait ReprTreeExt {
 
     fn singleton_buffer<T: Clone + Send + Sync + 'static>(&self) -> SingletonBuffer<T>;
     fn vec_buffer<T: Clone + Send + Sync + 'static>(&self) -> VecBuffer<T>;
+
+    fn edittree(&self, ctx: &Arc<RwLock<Context>>) -> SingletonBuffer< Arc<RwLock<crate::edit_tree::EditTree>> > {
+        self.descend(Context::parse(&ctx, "EditTree")).unwrap()
+            .singleton_buffer()
+    }
 }
 
 impl ReprTreeExt for Arc<RwLock<ReprTree>> {
diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs
index c9fc849..189125c 100644
--- a/lib-nested-tty/src/editors/list.rs
+++ b/lib-nested-tty/src/editors/list.rs
@@ -236,10 +236,7 @@ impl PTYListController {
                     }
                 );
 
-                let new_edittree = ctx.setup_edittree(
-                    rt,
-                    self.depth.map(|d| d+1)
-                );
+                let new_edittree = ctx.setup_edittree( &rt );
 
                 if let Some(new_edittree) = new_edittree {