From 16a0381adac6363e02387d30fd05e7ac6fcc03f7 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Wed, 22 Jan 2025 23:36:43 +0100 Subject: [PATCH] construct list items from Void repr --- lib-nested-core/src/editors/char/ctx.rs | 42 +++++++++++++++++ lib-nested-core/src/editors/char/mod.rs | 2 +- lib-nested-core/src/editors/digit/ctx.rs | 58 ++++++++++++++++++++++++ lib-nested-core/src/repr_tree/node.rs | 2 +- lib-nested-tty/src/editors/list.rs | 26 +++++++---- 5 files changed, 118 insertions(+), 12 deletions(-) diff --git a/lib-nested-core/src/editors/char/ctx.rs b/lib-nested-core/src/editors/char/ctx.rs index e689512..8ccc3e9 100644 --- a/lib-nested-core/src/editors/char/ctx.rs +++ b/lib-nested-core/src/editors/char/ctx.rs @@ -21,6 +21,46 @@ use { }; pub fn init_ctx( ctx: Arc> ) { + let char_void_to_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, "Char~Void"), + Context::parse(&ctx, "Char~EditTree"), + { + let ctx = ctx.clone(); + move |rt, σ| { + rt.write().unwrap().branches.remove(&Context::parse(&ctx, "Void")); + + { + let mut b = rt.write().unwrap().singleton_buffer::(); + if let Some(buf) = b { + // buffer already exists + } else { + // create char buffer + rt.write().unwrap().insert_leaf( + vec![].into_iter(), + ReprLeaf::from_singleton_buffer(SingletonBuffer::new('\0')) + ); + } + } + + let char_buf = rt.singleton_buffer::(); + eprintln!("c = {}",char_buf.get()); + let mut edittree = CharEditor::new_edit_tree( + ctx.clone(), + char_buf, + SingletonBuffer::::new(0).get_port() + ); + + rt.insert_leaf( + Context::parse(&ctx, "Char~EditTree"), + ReprLeaf::from_singleton_buffer( + SingletonBuffer::new(edittree) + ) + ); + + ctx.read().unwrap().setup_edittree(rt); + } + } + ); let char_morph_to_edittree = GenericReprTreeMorphism::new( Context::parse(&ctx, "Char"), @@ -90,6 +130,8 @@ pub fn init_ctx( ctx: Arc> ) { ); } }); + + ctx.write().unwrap().morphisms.add_morphism( char_void_to_edittree ); ctx.write().unwrap().morphisms.add_morphism( char_morph_to_edittree ); ctx.write().unwrap().morphisms.add_morphism( char_morph_from_edittree ); } diff --git a/lib-nested-core/src/editors/char/mod.rs b/lib-nested-core/src/editors/char/mod.rs index 26e3eaf..b4e88b0 100644 --- a/lib-nested-core/src/editors/char/mod.rs +++ b/lib-nested-core/src/editors/char/mod.rs @@ -38,7 +38,7 @@ impl ObjCommander for CharEditor { TreeNavResult::Exit } else { self.data.set(value); - TreeNavResult::Continue + TreeNavResult::Exit } } else { TreeNavResult::Exit diff --git a/lib-nested-core/src/editors/digit/ctx.rs b/lib-nested-core/src/editors/digit/ctx.rs index 692cbf5..3455239 100644 --- a/lib-nested-core/src/editors/digit/ctx.rs +++ b/lib-nested-core/src/editors/digit/ctx.rs @@ -19,6 +19,63 @@ pub fn init_ctx( ctx: Arc> ) { // todo: proper scoping of Radix variable ctx.write().unwrap().add_varname("Radix"); + let digit_make_edittree_from_void = GenericReprTreeMorphism::new( + Context::parse(&ctx, "~Void"), + Context::parse(&ctx, "~EditTree"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + let radix = + match σ.get( &laddertypes::TypeID::Var(ctx.read().unwrap().get_var_typeid("Radix").unwrap()) ) { + Some(TypeTerm::Num(n)) => *n as u32, + _ => 0 + }; + + let char_rt = src_rt.descend_create(Context::parse(&ctx, "Char")).unwrap(); + let char_buf = { + let mut b = char_rt.write().unwrap().singleton_buffer::(); + if let Some(buf) = b { + // buffer already exists + eprintln!("take existing char buffer"); + buf + } else { + // create char buffer + eprintln!("create char buffer"); + let char_buf = SingletonBuffer::::new('?'); + src_rt.insert_leaf( + Context::parse(&ctx, "Char"), + ReprLeaf::from_singleton_buffer(char_buf.clone()) + ); + char_buf + } + }; + + /* Create EditTree object + */ + //if src_rt.descend(Context::parse(&ctx, "EditTree")).is_none() { + eprintln!("create Digit-Editor"); + let mut edittree = DigitEditor::new( + ctx.clone(), + radix, + char_buf + ).into_node( + r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() + ); + + src_rt.write().unwrap() + .insert_branch( + ReprTree::from_singleton_buffer( + Context::parse(&ctx, "EditTree"), + SingletonBuffer::new(edittree) + ) + ); + + ctx.read().unwrap().setup_edittree( src_rt ); + //} + } + } + ); + let digit_make_edittree = GenericReprTreeMorphism::new( Context::parse(&ctx, ""), Context::parse(&ctx, "~EditTree"), @@ -235,6 +292,7 @@ pub fn init_ctx( ctx: Arc> ) { ctx.write().unwrap().morphisms.add_morphism( digit_make_edittree ); + ctx.write().unwrap().morphisms.add_morphism( digit_make_edittree_from_void ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_to_edittree ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_from_edittree ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_to_u64 ); diff --git a/lib-nested-core/src/repr_tree/node.rs b/lib-nested-core/src/repr_tree/node.rs index ad66008..eca002f 100644 --- a/lib-nested-core/src/repr_tree/node.rs +++ b/lib-nested-core/src/repr_tree/node.rs @@ -31,7 +31,7 @@ use { pub struct ReprTree { halo: TypeTerm, type_tag: TypeTerm, - branches: HashMap, + pub branches: HashMap, leaf: Option< ReprLeaf >, } diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index 8da07cd..2f55b4a 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -220,19 +220,25 @@ impl PTYListController { match cur.mode { ListCursorMode::Insert => { - let mut rt = ReprTree::new_arc(e.item_type.clone()); + let mut rt = ReprTree::new_arc(Context::parse(&ctx, "Void")); + rt.write().unwrap().set_halo(e.item_type.clone()); rt = e.item_builder.build_from(rt).expect("cant creat EditTree for item"); - let mut ne = rt.edittree(&ctx).get_mut(); + + ne.goto(TreeCursor::home()); match ne.send_cmd_obj(cmd_obj.clone()) { - TreeNavResult::Continue => { - drop(ne); - e.insert(rt); - TreeNavResult::Continue - } - TreeNavResult::Exit => { - TreeNavResult::Exit - } + TreeNavResult::Continue => { + e.insert(rt); + e.goto(TreeCursor{ + leaf_mode: ListCursorMode::Insert, + tree_addr: vec![ cur.idx.unwrap() as isize, 1 ] + }); + TreeNavResult::Continue + } + TreeNavResult::Exit => { + e.insert(rt); + TreeNavResult::Continue + } } }, ListCursorMode::Select => {