diff --git a/examples/tty-02-digit/src/main.rs b/examples/tty-02-digit/src/main.rs index 93a1b8e..102bb90 100644 --- a/examples/tty-02-digit/src/main.rs +++ b/examples/tty-02-digit/src/main.rs @@ -65,7 +65,7 @@ async fn main() { let ctx = ctx.clone(); let digit_edittree = rt_digit.edittree( &ctx ); move |ev| { - digit_edittree.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx)); + digit_edittree.get_mut().send_cmd_obj(ev.to_repr_tree(&ctx)); } }); @@ -91,7 +91,7 @@ async fn main() { .offset(Vector2::new(1,1))); comp.push(rt_digit - .edittree( &ctx ).get().read().unwrap() + .edittree( &ctx ).get() .display_view() .offset(Vector2::new(3,2))); diff --git a/examples/tty-03-string/src/main.rs b/examples/tty-03-string/src/main.rs index c3fa53e..3fad1a2 100644 --- a/examples/tty-03-string/src/main.rs +++ b/examples/tty-03-string/src/main.rs @@ -41,53 +41,34 @@ async fn main() { nested::editors::list::init_ctx( ctx.clone() ); nested_tty::setup_edittree_hook(&ctx); - /* Create a Representation-Tree of type + /* create a Representation-Tree of type */ - let mut ldata = VecBuffer::new(); - for c in "Hello World!".chars() { - let rt = ReprTree::from_singleton_buffer( - Context::parse(&ctx, "Char"), - SingletonBuffer::new(c)); - - ldata.push(rt); - } - - let mut ldata_rt = ReprTree::from_vec_buffer( - Context::parse(&ctx, ""), - ldata.clone() + let mut rt_data = ReprTree::from_str("Hello World!"); + rt_data.write().unwrap().set_halo( + Context::parse(&ctx, "") ); - ldata_rt.write().unwrap().set_halo( - Context::parse(&ctx, "~") - ); - - let mut ledit = nested::editors::list::ListEditor::with_data( - ctx.clone(), - Context::parse(&ctx, "Char"), - ldata - ); - ledit.update_item_reprtrees(&Context::parse(&ctx, "Char")); - ledit.update_item_reprtrees(&Context::parse(&ctx, "Char~EditTree")); - let mut ledit = ledit.into_node(SingletonBuffer::new(0).get_port()); - - nested_tty::editors::list::PTYListStyle::for_node(&mut ledit, ("<", "", ">")); - nested_tty::editors::list::PTYListController::for_node(&mut ledit, None, None); - let ledittree = SingletonBuffer::new(Arc::new(RwLock::new(ledit))); - + /* specify required representations + */ let rt_string_builder = ReprTreeBuilder::new( ctx.clone() ) .require(Context::parse(&ctx, "~")) .require(Context::parse(&ctx, "")) .require(Context::parse(&ctx, "~EditTree")) ; - let rt_string = rt_string_builder.build_from(ldata_rt).expect("could not build rt_string"); - eprintln!("rt_string = \n{}", rt_string.read().unwrap().fmt(&ctx, 0)); + // initially, populate from representation + let mut rt_string = rt_string_builder.build_from(rt_data) + .expect("could not build rt_string"); + + // hand over control to EditTree representation + rt_string_builder.update( &rt_string, Context::parse(&ctx, "~EditTree") ) + .expect("could not update rt_string"); + /* Now, get the ListView that serves our char-values. * This view is a projection created by the morphism that was called above. */ let mut chars_view = rt_string - .read().unwrap() .get_port::>() .unwrap(); @@ -108,13 +89,14 @@ async fn main() { /* setup terminal */ let app = TTYApplication::new({ - let edittree_list = ledittree.clone();//rt_string.edittree(&ctx).clone(); + let edittree_list = rt_string.edittree(&ctx).clone(); /* event handler */ let ctx = ctx.clone(); move |ev| { - edittree_list.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx)); + let cmd = ev.to_repr_tree(&ctx); + edittree_list.get_mut().send_cmd_obj(cmd); } }); @@ -129,23 +111,23 @@ async fn main() { { let mut comp = compositor.write().unwrap(); - let label_str = ctx.read().unwrap().type_term_to_str(&rt_string.read().unwrap().get_type()); + let label_str = ctx.read().unwrap() + .type_term_to_str(&rt_string.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)))) + .map_item(|_pt, atom| + atom.add_style_front(TerminalStyle::fg_color((90,90,90)))) .offset(Vector2::new(1,1))); comp.push( - //rt_string.edittree(&ctx).get() - ledittree.get() - .read().unwrap() + rt_string.edittree(&ctx).get() .display_view() .offset(Vector2::new(3,2))); comp.push( string_view_tty .offset(Vector2::new(5,3))); - } /* write the changes in the view of `term_port` to the terminal diff --git a/examples/tty-04-posint/src/main.rs b/examples/tty-04-posint/src/main.rs index 7ffbc87..ee7419e 100644 --- a/examples/tty-04-posint/src/main.rs +++ b/examples/tty-04-posint/src/main.rs @@ -22,11 +22,12 @@ use { nested_tty::{ DisplaySegment, TTYApplication, TerminalCompositor, TerminalStyle, TerminalView, - TerminalAtom, TerminalEvent + TerminalAtom, TerminalEvent, + edit_tree::cursor_widget::TreeNavExt }, r3vi::{ buffer::{singleton::*, vec::*}, - view::{port::UpdateTask, singleton::*, list::*, sequence::*}, + view::{port::UpdateTask, ViewPort, singleton::*, list::*, sequence::*}, projection::* }, std::sync::{Arc, RwLock}, @@ -49,11 +50,9 @@ async fn main() { let int_builder = ReprTreeBuilder::new( ctx.clone() ) .require(Context::parse(&ctx, "ℕ ~ ~ ~Char> ~ EditTree")) + .require(Context::parse(&ctx, + "ℕ ~ ~ ~Char> ~ EditTree")) /* - .require(Context::parse(&ctx, - "ℕ ~ ~ > ~ EditTree")) - .require(Context::parse(&ctx, - "ℕ ~ ~ EditTree")) .require(Context::parse(&ctx, "ℕ ~ ~ EditTree")) .require(Context::parse(&ctx, @@ -61,7 +60,7 @@ async fn main() { */ ; - let mut rt_int = nested::repr_tree::ReprTree::from_str("cff"); + let mut rt_int = nested::repr_tree::ReprTree::from_str("f"); rt_int.write().unwrap().set_halo( /* HALO TYPE */ Context::parse(&ctx, " @@ -72,36 +71,42 @@ async fn main() { ~ ") ); + rt_int = int_builder.build_from( rt_int ) + .expect("cant build initial repr tree"); eprintln!("rt_int = \n{}\n", rt_int.read().unwrap().fmt(&ctx, 0)); - rt_int = int_builder.build_from( rt_int ).expect("cant build initial repr tree"); - - eprintln!("rt_int = \n{}\n", rt_int.read().unwrap().fmt(&ctx, 0)); - - return; - /* list of editors */ let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "")); // add all desired editors to the list - for edit_leaf in int_builder.required_leaves { + for edit_leaf in int_builder.required_leaves.iter() { let leaf_rt = rt_int.descend(edit_leaf.clone()).unwrap(); list_editor.data.push(leaf_rt); } let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port()); + nested_tty::editors::list::PTYListController::for_node(&mut edittree, None, None); /* cursors are a bit screwed initially so fix them up * TODO: how to fix this generally? */ + for edit_leaf in int_builder.required_leaves.iter() { + let leaf_rt = rt_int.descend(edit_leaf.clone()).unwrap(); + leaf_rt.edittree(&ctx).get_mut().goto(TreeCursor::none()); + } + + let first_idx : usize = 1; + int_builder.update( &rt_int, int_builder.required_leaves[first_idx].clone() ); + edittree.goto(TreeCursor{ leaf_mode: nested::editors::list::ListCursorMode::Insert, - tree_addr: vec![0,0] + tree_addr: vec![first_idx as isize, 0] }); let edittree = Arc::new(RwLock::new(edittree)); + /* setup terminal */ let app = TTYApplication::new({ @@ -109,25 +114,20 @@ async fn main() { */ let ctx = ctx.clone(); let rt_int = rt_int.clone(); - let last_idx = RwLock::new(0); + let last_idx = RwLock::new(first_idx); + let int_builder = int_builder.clone(); + let edittree = edittree.clone(); move |ev| { let cur = edittree.read().unwrap().get_cursor(); if cur.tree_addr.len() > 0 { let mut li = last_idx.write().unwrap(); let ci = cur.tree_addr[0]; - if *li != ci { - /* - eprintln!("----------------------------------"); - set_master( - &ctx, - &rt_int, - editor_types.clone(), - ci as usize - ); - */ + if *li != ci as usize { + eprintln!("--------\nCHANGE MASTER EDITOR\n--------------------"); int_builder.update( &rt_int, int_builder.required_leaves[ci as usize].clone() ); - *li = ci; + eprintln!("-----\nDone\n----"); + *li = ci as usize; } } @@ -146,24 +146,35 @@ async fn main() { { let mut comp = compositor.write().unwrap(); + let et = edittree.read().unwrap(); + comp.push(et.get_cursor_widget()); + fn show_edit_tree( ctx: &Arc>, comp: &mut TerminalCompositor, rt: &Arc>, y: i16 ) { let rt_edittree = rt.descend(Context::parse(&ctx, "EditTree")).expect("descend"); let halo_type = rt_edittree.read().unwrap().get_halo_type().clone(); let edittree = rt_edittree.edittree( &ctx ); + + let box_port = ViewPort::new(); + let ascii_box = nested_tty::widgets::ascii_box::AsciiBox::new( + Vector2::new(30, 1), + edittree.get().display_view(), + box_port.inner() + ); + + comp.push( + box_port.outer() + .offset(Vector2::new(1,y))); comp.push( nested_tty::make_label( &ctx.read().unwrap().type_term_to_str(&halo_type) ) .map_item(|_pt, atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90)))) .offset(Vector2::new(1,y))); - - comp.push( edittree.get().read().unwrap().display_view() - .offset(Vector2::new(1,y+1))); } - let mut y = 1; + let mut y = 2; for t in int_builder.required_leaves.iter() { show_edit_tree(&ctx, &mut comp, &rt_int.descend(t.clone()).expect(""), y); - y += 3; + y += 4; } } diff --git a/examples/tty-05-dictionary/src/main.rs b/examples/tty-05-dictionary/src/main.rs index f5b4478..8536c11 100644 --- a/examples/tty-05-dictionary/src/main.rs +++ b/examples/tty-05-dictionary/src/main.rs @@ -181,6 +181,8 @@ async fn main() { } let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port()); + nested_tty::editors::list::PTYListController::for_node(&mut edittree, None, None); + edittree.goto(TreeCursor{ leaf_mode: nested::editors::list::ListCursorMode::Insert, tree_addr: vec![0, 0] @@ -244,8 +246,8 @@ async fn main() { } show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Mnemonic ~ ")).unwrap(), 1 ); - show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ℕ ~ ")).unwrap(), 3 ); - show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ℕ ~ ")).unwrap(), 5 ); + //show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ℕ ~ ")).unwrap(), 3 ); + //show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ℕ ~ ")).unwrap(), 5 ); } /* write the changes in the view of `term_port` to the terminal diff --git a/examples/tty-06-lines/src/main.rs b/examples/tty-06-lines/src/main.rs index 33a956e..357f593 100644 --- a/examples/tty-06-lines/src/main.rs +++ b/examples/tty-06-lines/src/main.rs @@ -148,7 +148,7 @@ impl LineEditor { ctx.clone(), Context::parse(&ctx, "") ); - let chars_seg_seq = ListSegmentSequence::new(chars_edit.get_cursor_port(), chars_edit.get_data_port()) + let chars_seg_seq = ListSegmentSequence::new(chars_edit.get_cursor_port(), chars_edit.get_edittree_seq() ) .read().unwrap().get_view(); let out_port = ViewPort::new(); @@ -234,7 +234,7 @@ impl LinesEditor { let lines_segments = nested::editors::list::ListSegmentSequence::new( list_edit.get_cursor_port(), - list_edit.get_data_port() + list_edit.get_edittree_seq() ).read().unwrap().get_view(); let lines_view = lines_segments .map({ @@ -284,7 +284,6 @@ impl LinesEditor { let chars_rt = self.make_line(line_value); let chars_edittree = chars_rt - .descend(Context::parse(&self.ctx, "EditTree")).unwrap() .edittree(&self.ctx).get() .read().unwrap().clone(); @@ -303,19 +302,24 @@ impl LinesEditor { ReprLeaf::from_view( line_port ) ); + let mut le_rt = ReprTree::from_singleton_buffer( + Context::parse(&self.ctx, "EditTree"), + SingletonBuffer::new( Arc::new(RwLock::new(line_edittree)) ) + ); + le_rt.write().unwrap().set_halo(Context::parse(&self.ctx, "Line")); + le_rt = ReprTree::rise(le_rt); + self.edit.write().unwrap() .get_edit::< ListEditor >().unwrap() .write().unwrap() .data - .push( Arc::new(RwLock::new(line_edittree)) ); + .push( le_rt ); } pub fn make_line(&self, line_value: &str) -> Arc> { let ctx = &self.ctx; - let mut rt_line = ReprTree::from_str( - Context::parse(&ctx, "~"), - line_value - ); + let mut rt_line = ReprTree::from_str(line_value); + rt_line.write().unwrap().set_halo(Context::parse(&ctx, "~")); // create Editor & transfer data to Editor ctx.read().unwrap().apply_morphism( diff --git a/lib-nested-core/src/editors/char/ctx.rs b/lib-nested-core/src/editors/char/ctx.rs index 8b04f4d..a4dd6db 100644 --- a/lib-nested-core/src/editors/char/ctx.rs +++ b/lib-nested-core/src/editors/char/ctx.rs @@ -33,6 +33,7 @@ pub fn init_ctx( ctx: Arc> ) { if let Some(buf) = b { // buffer already exists } else { + eprintln!("create char buffer"); // create char buffer rt.write().unwrap().insert_leaf( vec![].into_iter(), @@ -49,12 +50,10 @@ pub fn init_ctx( ctx: Arc> ) { ); rt.insert_leaf( - Context::parse(&ctx, "EditTree"), - ReprLeaf::from_singleton_buffer( - SingletonBuffer::new( - Arc::new(RwLock::new(edittree)) - ) - ) + Context::parse(&ctx, "Char~EditTree"), + ReprLeaf::from_singleton_buffer( + SingletonBuffer::new(edittree) + ) ); ctx.read().unwrap().setup_edittree(rt); @@ -71,15 +70,25 @@ pub fn init_ctx( ctx: Arc> ) { { let mut b = rt .descend(Context::parse(&ctx, "EditTree")).unwrap() - .view_singleton::>>(); + .singleton_buffer::(); + + if rt.write().unwrap().singleton_buffer::().is_none() { + eprintln!("insert singleton buffer"); + rt.insert_leaf( + Context::parse(&ctx, "Char"), + ReprLeaf::from_singleton_buffer( + SingletonBuffer::new('\0') + ) + ); + } rt.attach_leaf_to( Context::parse(&ctx, "Char"), - b.map(|x| - x.read().unwrap() - .get_edit::().unwrap() + b.get_port().map( + |x| x.get_edit::().unwrap() .read().unwrap() - .get()) + .get() + ) ); } }); diff --git a/lib-nested-core/src/editors/digit/ctx.rs b/lib-nested-core/src/editors/digit/ctx.rs index f78b962..c2250cd 100644 --- a/lib-nested-core/src/editors/digit/ctx.rs +++ b/lib-nested-core/src/editors/digit/ctx.rs @@ -47,7 +47,7 @@ pub fn init_ctx( ctx: Arc> ) { .insert_branch( ReprTree::from_singleton_buffer( Context::parse(&ctx, "EditTree"), - SingletonBuffer::new(Arc::new(RwLock::new(edittree))) + SingletonBuffer::new(edittree) ) ); @@ -85,7 +85,7 @@ pub fn init_ctx( ctx: Arc> ) { .insert_branch( ReprTree::from_singleton_buffer( Context::parse(&ctx, "EditTree"), - SingletonBuffer::new(Arc::new(RwLock::new(edittree))) + SingletonBuffer::new(edittree) ) ); @@ -105,7 +105,6 @@ pub fn init_ctx( ctx: Arc> ) { let port = edittree .get() - .read().unwrap() .get_edit::().unwrap() .read().unwrap() .get_char_port(); @@ -141,7 +140,7 @@ pub fn init_ctx( ctx: Arc> ) { if let Some(src_rt) = rt.descend(Context::parse(&ctx, "Char")) { /* insert projected view into ReprTree */ - let u64_view = + let u64_view = src_rt.view_char() .map(move |c| c.to_digit(radix).unwrap_or(0) as u64); @@ -177,7 +176,7 @@ pub fn init_ctx( ctx: Arc> ) { if radix <= 16 { /* insert projected view into ReprTree */ - let char_view = + let char_view = rt.descend(Context::parse(&ctx, "ℤ_2^64~machine.UInt64")) .unwrap() .view_u64() @@ -201,5 +200,3 @@ pub fn init_ctx( ctx: Arc> ) { ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_to_u64 ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_u64_to_char ); } - - diff --git a/lib-nested-core/src/editors/integer/ctx.rs b/lib-nested-core/src/editors/integer/ctx.rs index 1165543..09552ad 100644 --- a/lib-nested-core/src/editors/integer/ctx.rs +++ b/lib-nested-core/src/editors/integer/ctx.rs @@ -44,7 +44,7 @@ pub fn init_ctx(ctx: Arc>) { ~ ~ ℤ_2^64 ~ machine.UInt64 > ").apply_substitution(&|k|σ.get(k).cloned()).clone(), src_digits.reverse() - ); + ); } } ); @@ -72,7 +72,7 @@ pub fn init_ctx(ctx: Arc>) { ~ ~ ℤ_2^64 ~ machine.UInt64 > ").apply_substitution(&|k|σ.get(k).cloned()).clone(), src_digits.reverse() - ); + ); } } ); @@ -100,7 +100,7 @@ pub fn init_ctx(ctx: Arc>) { ~ ~ ℤ_2^64 ~ machine.UInt64 > ").apply_substitution(&|k|σ.get(k).cloned()).clone(), src_digits.reverse() - ); + ); } } ); @@ -167,7 +167,7 @@ pub fn init_ctx(ctx: Arc>) { dst_digits_port ); } - + } ); @@ -224,23 +224,21 @@ pub fn init_ctx(ctx: Arc>) { ) .unwrap() .edittree( &ctx ) - .get().clone() - .read().unwrap() - .clone(); + .get(); // clear display list_edittree.disp.view = ReprTree::new_arc(Context::parse(&ctx, "Display")); - +/* src_rt.insert_leaf( Context::parse(&ctx, " ~ EditTree") .apply_substitution(&|x| σ.get(x).cloned()).clone(), ReprLeaf::from_singleton_buffer( SingletonBuffer::new( - Arc::new(RwLock::new(list_edittree)) + list_edittree ) ) ); - +*/ ctx.read().unwrap().setup_edittree( &src_rt.descend( Context::parse(&ctx, "") @@ -262,22 +260,19 @@ pub fn init_ctx(ctx: Arc>) { .apply_substitution(&|x| σ.get(x).cloned()).clone()) .unwrap() .edittree( &ctx ) - .get().clone() - .read().unwrap() - .clone(); + .get_mut(); // clear display list_edittree.disp.view = ReprTree::new_arc(Context::parse(&ctx, "Display")); - +/* src_rt.insert_leaf( Context::parse(&ctx, " ~ >~EditTree") .apply_substitution(&|x| σ.get(x).cloned()).clone(), ReprLeaf::from_singleton_buffer( - SingletonBuffer::new( - Arc::new(RwLock::new(list_edittree)) - ) + SingletonBuffer::new(list_edittree) ) ); +*/ ctx.read().unwrap().setup_edittree( &src_rt.descend( @@ -294,9 +289,8 @@ pub fn init_ctx(ctx: Arc>) { ctx.write().unwrap().morphisms.add_morphism( posint_seq_morph_big_to_little ); ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_big_to_little ); - ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_little_to_big ); + ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_little_to_big ); ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_radix ); ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_from_u64 ); ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_to_u64 ); } - diff --git a/lib-nested-core/src/editors/list/cmd.rs b/lib-nested-core/src/editors/list/cmd.rs index 03b3e96..c1d29c9 100644 --- a/lib-nested-core/src/editors/list/cmd.rs +++ b/lib-nested-core/src/editors/list/cmd.rs @@ -50,7 +50,7 @@ impl ObjCommander for ListEditor { match cur.mode { ListCursorMode::Select => { if let Some(mut item) = self.get_cur_edittree() { - let mut item = item.write().unwrap(); + //let mut item = item.write().unwrap(); let item_cur = item.get_cursor(); match cmd.get() { diff --git a/lib-nested-core/src/editors/list/ctx.rs b/lib-nested-core/src/editors/list/ctx.rs index fd9656b..098c6b2 100644 --- a/lib-nested-core/src/editors/list/ctx.rs +++ b/lib-nested-core/src/editors/list/ctx.rs @@ -13,6 +13,185 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> +pub fn init_ctx__seq_to_list( ctx: &Arc> ) { + + let seq_morph_to_list_char = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, ""), + src_rt.view_seq::().to_list() + ); + } + } + ); + + let seq_morph_to_list_u64 = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, ""), + src_rt.view_seq::().to_list() + ); + } + } + ); + + ctx.write().unwrap().morphisms.add_morphism( seq_morph_to_list_char ); + ctx.write().unwrap().morphisms.add_morphism( seq_morph_to_list_u64 ); +} + +pub fn init_ctx__vec_to_list( ctx: &Arc> ) { + let list_morph_from_vec_char = GenericReprTreeMorphism::new( + Context::parse(&ctx, "~"), + Context::parse(&ctx, ""), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + let src_port = src_rt.descend( + Context::parse(&ctx, "~") + ) + .expect("descend") + .get_port::>>().unwrap(); + + src_rt.attach_leaf_to( Context::parse(&ctx, ""), src_port.to_list() ); + } + } + ); + + let list_morph_from_vec_rt = GenericReprTreeMorphism::new( + Context::parse(&ctx, "~"), + Context::parse(&ctx, ""), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + let src_port = src_rt.descend( + Context::parse(&ctx, "~") + ) + .expect("descend") + .get_port::>>().unwrap(); + + src_rt.attach_leaf_to( Context::parse(&ctx, ""), src_port.to_list() ); + } + } + ); + + ctx.write().unwrap().morphisms.add_morphism( list_morph_from_vec_char ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_from_vec_rt ); +} + +pub fn init_ctx__list_to_vec( ctx: &Arc> ) { + let list_morph_to_vec_char = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, ""), + src_rt.view_list::() + ); + } + } + ); + + let list_morph_to_vec_u64 = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, ""), + src_rt.view_list::() + ); + } + } + ); + + let list_morph_to_vec_reprtree = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, ""), + src_rt.view_list::< ReprTreeArc >() + ); + } + } + ); + + let list_morph_to_vec_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + let list_port = src_rt.get_port::>().unwrap(); + src_rt.attach_leaf_to( Context::parse(&ctx, ""), list_port ); + } + } + ); + + ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_reprtree ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_char ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_u64 ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_edittree ); +} + +pub fn init_ctx__unpack_reprtree( ctx: &Arc> ) { + let list_morph_rt_to_char = GenericReprTreeMorphism::new( + Context::parse(&ctx, " ~ "), + Context::parse(&ctx, ""), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, ""), + src_rt.descend(Context::parse(&ctx, "")).expect("cant descend") + .view_list::() + .map(|rt| { + rt.view_singleton::().get_view().get() + }) + ); + } + } + ); + + ctx.write().unwrap().morphisms.add_morphism( list_morph_rt_to_char ); +} + +pub fn init_ctx__pack_reprtree( ctx: &Arc> ) { + let list_morph_rt_from_char = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, " ~ "), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + src_rt.attach_leaf_to( + Context::parse(&ctx, "~"), + src_rt.view_list::() + .map(|c| { + ReprTree::from_singleton_buffer( + TypeTerm::TypeID(TYPEID_char), + SingletonBuffer::::new( *c ) + ) + }) + ); + } + } + ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_rt_from_char ); +} + pub fn init_ctx(ctx: Arc>) { ctx.write().unwrap().add_varname("Item"); @@ -24,28 +203,36 @@ pub fn init_ctx(ctx: Arc>) { move |src_rt, σ| { let item_id = laddertypes::TypeID::Var(ctx.read().unwrap().get_var_typeid("Item").unwrap()); if let Some( item_type ) = σ.get( &item_id ) { - let mut item_vec_rt = src_rt - .descend( - Context::parse(&ctx, "~") - .apply_substitution(&|id| σ.get(id).cloned()).clone() - ) + let mut item_rt_vec = src_rt + .descend(Context::parse(&ctx, "~")) .expect("cant descend src repr"); - let item_vec_buffer = item_vec_rt.vec_buffer::< ReprTreeArc >(); + let item_vec_buffer = item_rt_vec.vec_buffer::< ReprTreeArc >(); - let mut list_editor = ListEditor::with_data(ctx.clone(), item_type.clone(), item_vec_buffer); - let edittree_list = list_editor.into_node( - SingletonBuffer::::new(0).get_port() - ); - src_rt.insert_leaf( - Context::parse(&ctx, " ~ EditTree") - .apply_substitution(&|id| σ.get(id).cloned()).clone(), - - ReprLeaf::from_singleton_buffer( - SingletonBuffer::new(Arc::new(RwLock::new(edittree_list))) - ) - ); + if let Some(mut buf) = src_rt.descend_create(Context::parse(&ctx, "EditTree")).as_mut() + .expect("get_edittree rt") + .write().unwrap() + .singleton_buffer::() + { + let mut et = buf.get_mut(); + let mut list_editor = et.get_edit::().unwrap(); + let mut list_editor = list_editor.write().unwrap(); + list_editor.data = item_vec_buffer; + list_editor.update_item_reprtrees(&item_type); + } else { + let mut list_editor = ListEditor::with_data(ctx.clone(), item_type.clone(), item_vec_buffer); + list_editor.update_item_reprtrees(&item_type); + let mut edittree_list = list_editor.into_node( + SingletonBuffer::::new(0).get_port() + ); + src_rt.insert_branch( + ReprTree::from_singleton_buffer( + Context::parse(&ctx, "EditTree"), + SingletonBuffer::new(edittree_list) + ) + ); + } ctx.read().unwrap().setup_edittree( &src_rt ); } else { eprintln!("no item type"); @@ -53,176 +240,32 @@ pub fn init_ctx(ctx: Arc>) { } } ); -/* + let list_morph_editsetup3 = GenericReprTreeMorphism::new( Context::parse(&ctx, " ~ EditTree"), - Context::parse(&ctx, " ~ "), + Context::parse(&ctx, " ~ ~ "), { let ctx = ctx.clone(); move |src_rt, σ| { let edittree = src_rt.edittree( &ctx ); - let list_edit = edittree.get().read().unwrap().get_edit::< ListEditor >().unwrap(); - let edittree_items = list_edit.read().unwrap().data.get_port().to_list(); - - eprintln!("edittree_items.len() = {:?}", edittree_items.get_view().unwrap().len()); - - src_rt.attach_leaf_to( - Context::parse(&ctx, " ~ ") - .apply_substitution(&|x| σ.get(x).cloned()).clone(), - edittree_items + let list_edit = edittree.get().get_edit::< ListEditor >().unwrap(); + list_edit.write().unwrap().update_item_reprtrees(Context::parse(&ctx, "Item~EditTree") + .apply_substitution(&move |s| σ.get(s).cloned()) ); - } - } - ); -*/ - - let list_morph_rt_to_char = GenericReprTreeMorphism::new( - Context::parse(&ctx, " ~ "), - Context::parse(&ctx, ""), - { - let ctx = ctx.clone(); - move |src_rt, σ| { src_rt.attach_leaf_to( - Context::parse(&ctx, ""), - src_rt.descend(Context::parse(&ctx, "")).expect("cant descend") - .view_list::() - .map(|rt| { - rt.view_singleton::().get_view().get() - }) + Context::parse(&ctx, " ~ "), + list_edit.read().unwrap().data.get_port().to_list() ); } } ); - let list_morph_rt_from_char = GenericReprTreeMorphism::new( - Context::parse(&ctx, ""), - Context::parse(&ctx, " ~ "), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - src_rt.attach_leaf_to( - Context::parse(&ctx, "~"), - src_rt.view_list::() - .map({|c| { - ReprTree::from_singleton_buffer( - TypeTerm::TypeID(TYPEID_char), - SingletonBuffer::::new( *c ) - ) - }}) - ); - } - } - ); - - - let seq_morph_to_list_char = GenericReprTreeMorphism::new( - Context::parse(&ctx, ""), - Context::parse(&ctx, "~"), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - src_rt.attach_leaf_to( - Context::parse(&ctx, ""), - src_rt.view_seq::().to_list() - ); - } - } - ); - - let list_morph_to_vec_char = GenericReprTreeMorphism::new( - Context::parse(&ctx, ""), - Context::parse(&ctx, "~"), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - src_rt.attach_leaf_to( - Context::parse(&ctx, ""), - src_rt.view_list::() - ); - } - } - ); - - let seq_morph_to_list_u64 = GenericReprTreeMorphism::new( - Context::parse(&ctx, ""), - Context::parse(&ctx, "~"), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - src_rt.attach_leaf_to( - Context::parse(&ctx, ""), - src_rt.view_seq::().to_list() - ); - } - } - ); - - let list_morph_to_vec_u64 = GenericReprTreeMorphism::new( - Context::parse(&ctx, ""), - Context::parse(&ctx, "~"), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - src_rt.attach_leaf_to( - Context::parse(&ctx, ""), - src_rt.view_list::() - ); - } - } - ); - - let list_morph_from_vec_char = GenericReprTreeMorphism::new( - Context::parse(&ctx, "~"), - Context::parse(&ctx, ""), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - let src_port = src_rt.descend(Context::parse(&ctx, "~")).expect("descend") - .get_port::>>().unwrap(); - - src_rt.attach_leaf_to( Context::parse(&ctx, ""), src_port.to_list() ); - } - } - ); - - let list_morph_from_vec_rt = GenericReprTreeMorphism::new( - Context::parse(&ctx, "~"), - Context::parse(&ctx, ""), - { - let ctx = ctx.clone(); - move |src_rt, σ| { - let src_port = src_rt.descend(Context::parse(&ctx, "~")).expect("descend") - .get_port::>>().unwrap(); - - src_rt.attach_leaf_to( Context::parse(&ctx, ""), src_port.to_list() ); - } - } - ); - - - - - let list_morph_to_vec_edittree = GenericReprTreeMorphism::new( - Context::parse(&ctx, ""), - Context::parse(&ctx, " ~ "), - - { - let ctx = ctx.clone(); - move |src_rt, σ| { - let list_port = src_rt.get_port::> >>().unwrap(); - src_rt.attach_leaf_to( Context::parse(&ctx, ""), list_port ); - } - } - ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_editsetup1 ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_rt_to_char ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_rt_from_char ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_from_vec_char ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_from_vec_rt ); - ctx.write().unwrap().morphisms.add_morphism( seq_morph_to_list_char ); - ctx.write().unwrap().morphisms.add_morphism( seq_morph_to_list_u64 ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_char ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_u64 ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_edittree ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_editsetup3 ); + + init_ctx__seq_to_list(&ctx); + init_ctx__vec_to_list(&ctx); + init_ctx__list_to_vec(&ctx); + init_ctx__pack_reprtree(&ctx); + init_ctx__unpack_reprtree(&ctx); } diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs index 1afa517..1a76e84 100644 --- a/lib-nested-core/src/editors/list/editor.rs +++ b/lib-nested-core/src/editors/list/editor.rs @@ -2,13 +2,13 @@ use { crate::{ edit_tree::{diagnostics::Diagnostics, EditTree, TreeCursor, TreeNav}, editors::{list::{ListCmd, ListCursor, ListCursorMode}, ObjCommander}, - repr_tree::{ReprTreeBuilder, context::TYPEID_edittree, node::ReprTreeArc, Context, ReprTree, ReprTreeExt} + repr_tree::{context::{TYPEID_edittree, TYPEID_reprtree}, node::ReprTreeArc, Context, ReprTree, ReprTreeBuilder, ReprTreeExt} }, laddertypes::TypeTerm, r3vi::{ buffer::{singleton::*, vec::*}, projection::*, - view::{list::*, sequence::*, singleton::*, OuterViewPort} + view::{list::*, port::UpdateTask, sequence::*, singleton::*, OuterViewPort} }, std::sync::{Arc, RwLock} }; @@ -25,11 +25,10 @@ pub struct ListEditor { depth: OuterViewPort>, + pub item_type: TypeTerm, + pub master_repr: Arc>, pub item_builder: ReprTreeBuilder, pub ctx: Arc>, - - /// item type - pub typ: TypeTerm, } impl ListEditor { @@ -51,7 +50,7 @@ impl ListEditor { ) -> Self { let cursor = SingletonBuffer::new(ListCursor::default()); - ListEditor { + let mut le = ListEditor { mode_port: cursor .get_port() .map({ @@ -66,7 +65,6 @@ impl ListEditor { if idx >= 0 && idx < data.len() as isize { data.get(idx as usize) .edittree(&ctx).get() - .read().unwrap() .get_mode_view() } else { ip @@ -95,7 +93,6 @@ impl ListEditor { if idx >= 0 && idx < data.len() as isize { return data.get(idx as usize) .edittree(&ctx).get() - .read().unwrap() .get_addr_view(); } } @@ -119,49 +116,55 @@ impl ListEditor { TypeTerm::TypeID(TYPEID_edittree) ])) .clone(), - typ, + master_repr: Arc::new(RwLock::new(typ.clone())), + item_type: typ, ctx, - depth: SingletonBuffer::new(0).get_port() - } + depth: SingletonBuffer::new(0).get_port(), + }; + + le.update_item_reprtrees(&le.item_type.clone()); + le } - pub fn into_node(mut self, depth: OuterViewPort>) -> EditTree { - let ctx = self.ctx.clone(); - - self.depth = depth.clone(); + pub fn to_edittree(self, mut edittree: EditTree) -> EditTree { let editor = Arc::new(RwLock::new(self)); - let e = editor.read().unwrap(); - let mut node = EditTree::new(ctx, depth) + edittree.ctrl.spillbuf = e.spillbuf.clone(); + edittree .set_editor(editor.clone()) .set_nav(editor.clone()) .set_cmd(editor.clone()) .set_diag(e.get_edittree_seq() - .enumerate() - .map(|(idx, item_editor)| { - let idx = *idx; - item_editor.get_msg_port() - .map(move |msg| { - let mut msg = msg.clone(); - msg.addr.insert(0, idx); - msg - }) - }) - .flatten()); - - node.ctrl.spillbuf = e.spillbuf.clone(); - node + .enumerate() + .map(|(idx, item_editor)| { + let idx = *idx; + item_editor.get_msg_port() + .map(move |msg| { + let mut msg = msg.clone(); + msg.addr.insert(0, idx); + msg + }) + }) + .flatten()) } - pub fn update_item_reprtrees(&self, master_repr: &TypeTerm) { + pub fn into_node(mut self, depth: OuterViewPort>) -> EditTree { + let ctx = self.ctx.clone(); + self.depth = depth.clone(); + self.to_edittree(EditTree::new(ctx, depth)) + } + + pub fn update_item_reprtrees(&mut self, master_repr: &TypeTerm) { + self.data.get_port().0.update(); + *self.master_repr.write().unwrap() = master_repr.clone(); for i in 0..self.data.len() { self.item_builder.update( &self.data.get(i), master_repr.clone() ); } } pub fn get_item_type(&self) -> TypeTerm { - self.typ.clone() + self.item_type.clone() } pub fn get_list_type(&self) -> TypeTerm { @@ -179,11 +182,17 @@ impl ListEditor { self.data.get_port().to_list() } - pub fn get_edittree_list(&self) -> OuterViewPort> { + pub fn get_edittree_list(&self) -> OuterViewPort> { self.get_reprtree_list().map({ let ctx = self.ctx.clone(); - move|rt| { - rt.edittree(&ctx).get().read().unwrap().clone() + let item_builder = self.item_builder.clone(); + let master_repr = self.master_repr.clone(); + let item_type = self.item_type.clone(); + move |rt| { + if rt.descend(TypeTerm::TypeID(TYPEID_edittree)).is_none() { + item_builder.update( &rt, item_type.clone() ); + } + rt.edittree(&ctx).get() } }) } @@ -191,11 +200,12 @@ 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(); ReprTree::new_leaf( - self.get_seq_type(), + self.get_list_type(), data_view.into() ) } @@ -226,11 +236,11 @@ impl ListEditor { } } - pub fn get_edittree(&self, idx: usize) -> Arc> { + pub fn get_edittree(&self, idx: usize) -> EditTree { self.data.get(idx).edittree(&self.ctx).get() } - pub fn get_cur_edittree(&self) -> Option< Arc> > { + pub fn get_cur_edittree(&self) -> Option< EditTree > { if let Some(idx) = self.cursor.get().idx { let idx = crate::utils::modulo(idx as isize, self.data.len() as isize) as usize; if idx < self.data.len() { @@ -245,7 +255,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) + self.ctx.read().unwrap().is_list_type(&self.item_type) } /// delete all items @@ -282,8 +292,10 @@ impl ListEditor { /// insert a new element pub fn insert(&mut self, item: ReprTreeArc) { - let item_edit = item.edittree(&self.ctx).get(); - item_edit.read().unwrap().disp.depth.0.set_view( + self.item_builder.update( &item, self.master_repr.read().unwrap().clone() ); + + let mut item_edit = item.edittree(&self.ctx).get_mut(); + item_edit.disp.depth.0.set_view( self.depth.map(|d| d+1).get_view() ); @@ -295,7 +307,7 @@ impl ListEditor { if self.is_listlist() { cur.mode = ListCursorMode::Select; } else { - item_edit.write().unwrap().goto(TreeCursor::none()); + item_edit.goto(TreeCursor::none()); cur.idx = Some(idx + 1); } } @@ -352,8 +364,7 @@ impl ListEditor { let cur = self.get_cursor(); if let Some(mut item) = self.get_item().clone() { - let item_edit = item.edittree(&self.ctx).get(); - let mut ie = item_edit.write().unwrap(); + let mut ie = item.edittree(&self.ctx).get_mut(); ie.send_cmd_obj(ListCmd::Split.into_repr_tree(&self.ctx)); if cur.tree_addr.len() < 3 { @@ -364,12 +375,11 @@ impl ListEditor { let mut b = ie.ctrl.spillbuf.write().unwrap(); - let rt = ReprTree::new_arc(self.typ.clone()); + let rt = ReprTree::new_arc(self.item_type.clone()); let mut et = self.ctx.read().unwrap().setup_edittree(&rt); if let Some(edittree) = et.as_mut(){ - let mut tail_node = edittree.get(); - let mut tail_node = tail_node.write().unwrap(); + let mut tail_node = edittree.get_mut(); tail_node.goto(TreeCursor::home()); for n in b.iter() { @@ -399,10 +409,8 @@ impl ListEditor { pub fn listlist_join_pxev(&mut self, idx: isize) { { - let cur_editor = self.data.get(idx as usize).edittree(&self.ctx).get(); - let pxv_editor = self.data.get(idx as usize-1).edittree(&self.ctx).get(); - let mut cur_editor = cur_editor.write().unwrap(); - let mut pxv_editor = pxv_editor.write().unwrap(); + let mut cur_editor = self.data.get(idx as usize).edittree(&self.ctx).get_mut(); + let mut pxv_editor = self.data.get(idx as usize-1).edittree(&self.ctx).get_mut(); let oc0 = cur_editor.get_cursor(); @@ -451,10 +459,8 @@ impl ListEditor { pub fn listlist_join_nexd(&mut self, idx: usize) { { - let cur_editor = self.data.get(idx).edittree(&self.ctx).get(); - let nxd_editor = self.data.get(idx + 1).edittree(&self.ctx).get(); - let mut cur_editor = cur_editor.write().unwrap(); - let mut nxd_editor = nxd_editor.write().unwrap(); + let mut cur_editor = self.data.get(idx).edittree(&self.ctx).get_mut(); + let mut nxd_editor = self.data.get(idx + 1).edittree(&self.ctx).get_mut(); let oc0 = cur_editor.get_cursor(); diff --git a/lib-nested-core/src/editors/list/nav.rs b/lib-nested-core/src/editors/list/nav.rs index bbef859..573430c 100644 --- a/lib-nested-core/src/editors/list/nav.rs +++ b/lib-nested-core/src/editors/list/nav.rs @@ -38,7 +38,6 @@ impl TreeNav for ListEditor { _ => 0 }) .edittree(&self.ctx).get() - .read().unwrap() .get_height(op) } else { 1 @@ -49,7 +48,6 @@ impl TreeNav for ListEditor { .map(|i| self.data .get(i) .edittree(&self.ctx).get() - .read().unwrap() .get_height(&TreeHeightOp::Max) ) .max() @@ -75,7 +73,6 @@ impl TreeNav for ListEditor { if let Some(i) = cur.idx { if i < self.data.len() as isize { let mut sub_cur = self.get_edittree(i as usize) - .read().unwrap() .get_cursor_warp(); sub_cur.tree_addr.insert(0, i as isize - self.data.len() as isize); return sub_cur; @@ -108,7 +105,7 @@ impl TreeNav for ListEditor { ListCursorMode::Select => { if let Some(i) = cur.idx { if i < self.data.len() as isize { - let mut sub_cur = self.get_edittree(i as usize).read().unwrap().get_cursor(); + let mut sub_cur = self.get_edittree(i as usize).get_cursor(); if sub_cur.tree_addr.len() > 0 { sub_cur.tree_addr.insert(0, i as isize); return sub_cur; @@ -132,7 +129,7 @@ impl TreeNav for ListEditor { let old_cur = self.cursor.get(); if let Some(i) = old_cur.idx { if i < self.data.len() as isize { - self.get_edittree(i as usize).write().unwrap().goto(TreeCursor::none()); + self.get_edittree(i as usize).goto(TreeCursor::none()); } } @@ -154,7 +151,6 @@ impl TreeNav for ListEditor { if new_cur.leaf_mode == ListCursorMode::Select && self.data.len() > 0 { self.get_edittree(idx as usize) - .write().unwrap() .goto(TreeCursor { leaf_mode: ListCursorMode::Select, tree_addr: vec![] @@ -173,7 +169,6 @@ impl TreeNav for ListEditor { }); self.get_edittree(idx as usize) - .write().unwrap() .goto(TreeCursor { leaf_mode: new_cur.leaf_mode, tree_addr: new_cur.tree_addr[1..].iter().cloned().collect(), @@ -219,7 +214,6 @@ impl TreeNav for ListEditor { if cur.tree_addr[0] < self.data.len() as isize { if self.get_edittree(cur.tree_addr[0] as usize) - .write().unwrap() .goby(Vector2::new(direction.x, direction.y)) == TreeNavResult::Continue { self.cursor.set(ListCursor { @@ -253,11 +247,11 @@ impl TreeNav for ListEditor { match cur.leaf_mode { ListCursorMode::Select => { let cur_item = self.get_edittree(cur.tree_addr[0] as usize); - let cur_height = cur_item.read().unwrap().get_height(&TreeHeightOp::Max); + let cur_height = cur_item.get_height(&TreeHeightOp::Max); let new_item = self.get_edittree(idx as usize); - let height = new_item.read().unwrap().get_height( + let height = new_item.get_height( if direction.x < 0 { &TreeHeightOp::Q } else { @@ -282,7 +276,7 @@ impl TreeNav for ListEditor { { if (cur.tree_addr[0] as usize) < self.data.len() { let cur_item = self.get_edittree(cur.tree_addr[0] as usize); - let cur_height = cur_item.read().unwrap().get_height(&TreeHeightOp::P); + let cur_height = cur_item.get_height(&TreeHeightOp::P); if gravity && cur_height > 1 { new_addr.push( cur.tree_addr[0] ); @@ -294,7 +288,7 @@ impl TreeNav for ListEditor { } else { if (idx as usize) < self.data.len() { let pxv_item = self.get_edittree(idx as usize); - let pxv_height = pxv_item.read().unwrap().get_height(&TreeHeightOp::P); + let pxv_height = pxv_item.get_height(&TreeHeightOp::P); if gravity && pxv_height > 1 { new_addr.push( idx ); @@ -328,8 +322,8 @@ impl TreeNav for ListEditor { // nested if cur.tree_addr[0] < self.data.len() as isize { - let cur_item = self.get_edittree(cur.tree_addr[0] as usize); - let result = cur_item.write().unwrap().goby(direction); + let mut cur_item = self.get_edittree(cur.tree_addr[0] as usize); + let result = cur_item.goby(direction); match result { @@ -356,8 +350,8 @@ impl TreeNav for ListEditor { if direction.x < 0 { let pxv_item = self.get_edittree(cur.tree_addr[0] as usize - 1); - let pxv_height = pxv_item.read().unwrap().get_height(&TreeHeightOp::Q) as isize; - let cur_height = cur_item.read().unwrap().get_height(&TreeHeightOp::P) as isize; + let pxv_height = pxv_item.get_height(&TreeHeightOp::Q) as isize; + let cur_height = cur_item.get_height(&TreeHeightOp::P) as isize; let dist_from_ground = cur_height - (depth as isize - 1); let n_steps_down = if gravity { @@ -374,8 +368,8 @@ impl TreeNav for ListEditor { } else { let nxd_item = self.get_edittree(cur.tree_addr[0] as usize + 1); - let cur_height = cur_item.read().unwrap().get_height(&TreeHeightOp::Q) as isize; - let nxd_height = nxd_item.read().unwrap().get_height(&TreeHeightOp::P) as isize; + let cur_height = cur_item.get_height(&TreeHeightOp::Q) as isize; + let nxd_height = nxd_item.get_height(&TreeHeightOp::P) as isize; let dist_from_ground = cur_height - (depth as isize - 1); let n_steps_down = if gravity { diff --git a/lib-nested-core/src/editors/list/segment.rs b/lib-nested-core/src/editors/list/segment.rs index 4cd495b..6f25782 100644 --- a/lib-nested-core/src/editors/list/segment.rs +++ b/lib-nested-core/src/editors/list/segment.rs @@ -23,6 +23,8 @@ pub enum ListSegment { } } +/* todo: switch to ListView instead of SequenceView + */ pub struct ListSegmentSequence { data: Arc>, cursor: Arc>, diff --git a/lib-nested-core/src/repr_tree/builder.rs b/lib-nested-core/src/repr_tree/builder.rs index af76b31..bb8f763 100644 --- a/lib-nested-core/src/repr_tree/builder.rs +++ b/lib-nested-core/src/repr_tree/builder.rs @@ -11,7 +11,7 @@ use { }, buffer::{singleton::*, vec::*} }, - laddertypes::{TypeTerm, TypeID}, + laddertypes::{TypeTerm, TypeID, sugar::*}, std::{ collections::HashMap, sync::{Arc, RwLock}, @@ -45,7 +45,7 @@ impl ReprTreeBuilder { /// Add a type to the set of required representations. pub fn require(mut self, t: TypeTerm) -> Self { - self.required_leaves.push(t); + self.required_leaves.push(t.normalize()); self } @@ -63,6 +63,7 @@ impl ReprTreeBuilder { let morphism_base = &self.ctx.read().unwrap().morphisms; let mt = master_leaf_type.into(); + //eprintln!("REBUILD repr tree from {}", self.ctx.read().unwrap().type_term_to_str(&mt)); let mut leaves = self.required_leaves.clone(); leaves.retain(|t| t != &mt); diff --git a/lib-nested-core/src/repr_tree/context.rs b/lib-nested-core/src/repr_tree/context.rs index 4ff1f75..8254090 100644 --- a/lib-nested-core/src/repr_tree/context.rs +++ b/lib-nested-core/src/repr_tree/context.rs @@ -1,7 +1,7 @@ use { crate::{ edit_tree::EditTree, - repr_tree::{GenericReprTreeMorphism, ReprTree, ReprTreeExt, ReprTreeArc}, + repr_tree::{GenericReprTreeMorphism, ReprTree, ReprTreeArc, ReprTreeExt}, }, laddertypes::{ parser::ParseLadderType, sugar::SugaredTypeTerm, unparser::UnparseLadderType, @@ -12,18 +12,18 @@ use { view::{singleton::*, OuterViewPort}, }, std::{ - collections::HashMap, - sync::{Arc, RwLock}, - } + collections::HashMap, ops::DerefMut, sync::{Arc, RwLock} + }, }; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> -pub static TYPEID_edittree : TypeID = TypeID::Fun(0); -pub static TYPEID_char : TypeID = TypeID::Fun(1); -pub static TYPEID_u64 : TypeID = TypeID::Fun(2); -pub static TYPEID_list : TypeID = TypeID::Fun(3); -pub static TYPEID_vec : TypeID = TypeID::Fun(4); +pub static TYPEID_reprtree: TypeID = TypeID::Fun(0); +pub static TYPEID_edittree: TypeID = TypeID::Fun(1); +pub static TYPEID_char: TypeID = TypeID::Fun(2); +pub static TYPEID_u64: TypeID = TypeID::Fun(3); +pub static TYPEID_list: TypeID = TypeID::Fun(4); +pub static TYPEID_vec: TypeID = TypeID::Fun(5); //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> @@ -32,18 +32,18 @@ pub struct Context { /// assigns a name to every type pub type_dict: Arc>, - pub morphisms: laddertypes::morphism::MorphismBase< GenericReprTreeMorphism >, + pub morphisms: laddertypes::morphism::MorphismBase, /// named vertices of the graph - nodes: HashMap< String, Arc> >, + nodes: HashMap, /// todo: beautify /// types that can be edited as lists /// do we really need this? - pub list_types: Vec< TypeID >, - pub meta_chars: Vec< char >, + pub list_types: Vec, + pub meta_chars: Vec, - edittree_hook: Arc< dyn Fn(&mut EditTree, TypeTerm) + Send +Sync +'static >, + edittree_hook: Arc, /// recursion parent: Option>>, @@ -52,14 +52,13 @@ pub struct Context { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> impl Context { - pub fn with_parent( - parent: Option>> - ) -> Self { + pub fn with_parent(parent: Option>>) -> Self { Context { type_dict: match parent.as_ref() { Some(p) => p.read().unwrap().type_dict.clone(), None => { let mut dict = BimapTypeDict::new(); + assert_eq!(TYPEID_reprtree, dict.add_typename("ReprTree".into())); assert_eq!(TYPEID_edittree, dict.add_typename("EditTree".into())); assert_eq!(TYPEID_char, dict.add_typename("Char".into())); assert_eq!(TYPEID_u64, dict.add_typename("machine.UInt64".into())); @@ -69,18 +68,18 @@ impl Context { Arc::new(RwLock::new(dict)) } }, - morphisms: MorphismBase::new( TYPEID_list ), + morphisms: MorphismBase::new(TYPEID_list), nodes: HashMap::new(), list_types: match parent.as_ref() { Some(p) => p.read().unwrap().list_types.clone(), - None => Vec::new() + None => Vec::new(), }, meta_chars: match parent.as_ref() { Some(p) => p.read().unwrap().meta_chars.clone(), - None => Vec::new() + None => Vec::new(), }, parent, - edittree_hook: Arc::new(|_et, _t| {}) + edittree_hook: Arc::new(|_et, _t| {}), } } @@ -88,7 +87,10 @@ impl Context { Context::with_parent(None) } - pub fn set_edittree_hook(&mut self, hook: Arc< dyn Fn(&mut EditTree, TypeTerm) + Send +Sync +'static >) { + pub fn set_edittree_hook( + &mut self, + hook: Arc, + ) { self.edittree_hook = hook; } @@ -100,22 +102,20 @@ impl Context { } } - pub fn apply_morphism( &self, rt: &Arc>, ty: &MorphismType ) { - if let Some(path) - = self.morphisms.find_morphism_path( ty.clone().normalize() ) - { + pub fn apply_morphism(&self, rt: &Arc>, ty: &MorphismType) { + if let Some(path) = self.morphisms.find_morphism_path(ty.clone().normalize()) { let mut path = path.into_iter(); if let Some(mut src_type) = path.next() { for dst_type in path { - if let Some(( m, mut τ, σ )) = - self.morphisms.find_morphism_with_subtyping( - &laddertypes::MorphismType { - src_type: src_type.clone(), - dst_type: dst_type.clone() - } - ) { - let mut rt = rt.descend( τ ).expect("descend src repr"); - (m.setup_projection)( &mut rt, &σ ); + if let Some((m, mut τ, σ)) = + self.morphisms + .find_morphism_with_subtyping(&laddertypes::MorphismType { + src_type: src_type.clone(), + dst_type: dst_type.clone(), + }) + { + let mut rt = rt.descend(τ).expect("descend src repr"); + (m.setup_projection)(&mut rt, &σ); } src_type = dst_type; @@ -127,7 +127,10 @@ impl Context { } pub fn parse(ctx: &Arc>, s: &str) -> TypeTerm { - ctx.read().unwrap().type_term_from_str(s).expect("could not parse type term") + ctx.read() + .unwrap() + .type_term_from_str(s) + .expect("could not parse type term") } pub fn add_typename(&mut self, tn: &str) -> TypeID { @@ -139,21 +142,21 @@ impl Context { } pub fn add_synonym(&mut self, new: &str, old: &str) { - self.type_dict.write().unwrap().add_synonym(new.to_string(), old.to_string()); + self.type_dict + .write() + .unwrap() + .add_synonym(new.to_string(), old.to_string()); } pub fn add_list_typename(&mut self, tn: &str) { let tid = self.add_typename(tn); - self.list_types.push( tid ); + self.list_types.push(tid); } pub fn is_list_type(&self, t: &TypeTerm) -> bool { match t { - TypeTerm::TypeID(id) => { - self.list_types.contains(id) - } - TypeTerm::Ladder(args) | - TypeTerm::App(args) => { + TypeTerm::TypeID(id) => self.list_types.contains(id), + TypeTerm::Ladder(args) | TypeTerm::App(args) => { if args.len() > 0 { if self.is_list_type(&args[0]) { true @@ -164,7 +167,7 @@ impl Context { false } } - _ => false + _ => false, } } @@ -175,7 +178,7 @@ impl Context { pub fn get_fun_typeid(&self, tn: &str) -> Option { match self.get_typeid(tn) { Some(TypeID::Fun(x)) => Some(x), - _ => None + _ => None, } } @@ -186,11 +189,14 @@ impl Context { pub fn get_var_typeid(&self, tn: &str) -> Option { match self.get_typeid(tn) { Some(TypeID::Var(x)) => Some(x), - _ => None + _ => None, } } - pub fn type_term_from_str(&self, tn: &str) -> Result { + pub fn type_term_from_str( + &self, + tn: &str, + ) -> Result { self.type_dict.write().unwrap().parse(&tn) } @@ -200,17 +206,22 @@ impl Context { /// adds an object without any representations pub fn add_obj(ctx: Arc>, name: String, typename: &str) { - let type_tag = ctx.read().unwrap() - .type_dict.write().unwrap() - .parse(typename).unwrap(); -/* - if let Some(node) = Context::make_node(&ctx, type_tag, SingletonBuffer::new(0).get_port()) { - ctx.write().unwrap().nodes.insert(name, node); - } -*/ + let type_tag = ctx + .read() + .unwrap() + .type_dict + .write() + .unwrap() + .parse(typename) + .unwrap(); + /* + if let Some(node) = Context::make_node(&ctx, type_tag, SingletonBuffer::new(0).get_port()) { + ctx.write().unwrap().nodes.insert(name, node); + } + */ } - pub fn get_obj(&self, name: &String) -> Option< ReprTreeArc > { + pub fn get_obj(&self, name: &String) -> Option { if let Some(obj) = self.nodes.get(name) { Some(obj.clone()) } else if let Some(parent) = self.parent.as_ref() { @@ -222,22 +233,24 @@ impl Context { pub fn setup_edittree( &self, - rt: &Arc> - ) -> Option>>> { - if let Some(new_edittree) = - rt.descend(self.type_term_from_str("EditTree").unwrap()) - { - let typ = rt.read().unwrap().get_type().clone(); - let buf = new_edittree.singleton_buffer::>>(); - (*self.edittree_hook)( - &mut *buf.get().write().unwrap(), - typ - ); + rt: &Arc>, + ) -> Option> { + let edittree_rt = rt + .descend( + self.type_term_from_str("EditTree") + .expect("cant descend to editTree"), + ) + .clone(); + if let Some(new_edittree) = edittree_rt { + let typ = rt.get_type(); + let mut buf = new_edittree.singleton_buffer::(); + (*self.edittree_hook)(&mut *buf.get_mut(), typ); Some(buf) } else { - eprintln!("cant find edit tree repr {} ~Ψ~ {}", - self.type_term_to_str(rt.read().unwrap().get_halo_type()), - self.type_term_to_str(rt.read().unwrap().get_type()) + eprintln!( + "cant find edit tree repr {} ~Ψ~ {}", + self.type_term_to_str(&rt.get_halo_type()), + self.type_term_to_str(&rt.get_type()) ); None } diff --git a/lib-nested-core/src/repr_tree/mod.rs b/lib-nested-core/src/repr_tree/mod.rs index d4a3b52..193fbc2 100644 --- a/lib-nested-core/src/repr_tree/mod.rs +++ b/lib-nested-core/src/repr_tree/mod.rs @@ -38,6 +38,7 @@ use { pub trait ReprTreeExt { fn get_type(&self) -> TypeTerm; + fn get_halo_type(&self) -> TypeTerm; fn get_full_type(&self) -> TypeTerm; fn insert_leaf(&mut self, type_ladder: impl Into, leaf: ReprLeaf); @@ -61,8 +62,8 @@ pub trait ReprTreeExt { fn singleton_buffer(&self) -> SingletonBuffer; fn vec_buffer(&self) -> VecBuffer; - fn edittree(&self, ctx: &Arc>) -> SingletonBuffer< Arc> > { - self.descend_create(Context::parse(&ctx, "EditTree")) + fn edittree(&self, ctx: &Arc>) -> SingletonBuffer< crate::edit_tree::EditTree > { + self.descend(Context::parse(&ctx, "EditTree")) .expect("failed to get EditTree") .singleton_buffer() } @@ -73,6 +74,10 @@ impl ReprTreeExt for Arc> { self.read().unwrap().get_type().clone() } + fn get_halo_type(&self) -> TypeTerm { + self.read().unwrap().get_halo_type().clone() + } + fn get_full_type(&self) -> TypeTerm { self.read().unwrap().get_full_type().clone() } @@ -144,11 +149,11 @@ impl ReprTreeExt for Arc> { } fn singleton_buffer(&self) -> SingletonBuffer { - self.write().unwrap().singleton_buffer::().expect("") + self.write().unwrap().singleton_buffer::().expect("cant get singleton buffer") } fn vec_buffer(&self) -> VecBuffer { - self.write().unwrap().vec_buffer::().expect("") + self.write().unwrap().vec_buffer::().expect("cant get vec buffer") } } diff --git a/lib-nested-core/src/repr_tree/node.rs b/lib-nested-core/src/repr_tree/node.rs index c0b34c5..ad66008 100644 --- a/lib-nested-core/src/repr_tree/node.rs +++ b/lib-nested-core/src/repr_tree/node.rs @@ -21,7 +21,7 @@ use { Context, ReprLeaf, ReprTreeExt, - context::{TYPEID_list, TYPEID_vec, TYPEID_char, TYPEID_u64, TYPEID_edittree} + context::{TYPEID_reprtree, TYPEID_list, TYPEID_vec, TYPEID_char, TYPEID_u64, TYPEID_edittree} } }; @@ -228,13 +228,22 @@ impl ReprTree { leaf.attach_to(src_port); } else { if self.type_tag == TypeTerm::App(vec![ + TypeTerm::TypeID(TYPEID_vec), + TypeTerm::TypeID(TYPEID_reprtree) + ]) { + let mut leaf = ReprLeaf::from_vec_buffer( + VecBuffer::::new() + ); + + leaf.attach_to(src_port); + self.leaf = Some(leaf); + } + else if self.type_tag == TypeTerm::App(vec![ TypeTerm::TypeID(TYPEID_vec), TypeTerm::TypeID(TYPEID_edittree) ]) { let mut leaf = ReprLeaf::from_vec_buffer( - VecBuffer::< - Arc> - >::new() + VecBuffer::::new() ); leaf.attach_to(src_port); @@ -261,7 +270,8 @@ impl ReprTree { leaf.attach_to(src_port); self.leaf = Some(leaf); - } else { + } + else { self.leaf = Some(ReprLeaf::from_view(src_port)); } } @@ -272,9 +282,12 @@ impl ReprTree { if self.type_tag == TypeTerm::TypeID(TYPEID_edittree) { - leaf.detach::< dyn SingletonView< - Item = Arc> - > >(); + leaf.detach::< dyn SingletonView >(); + } + else if self.type_tag == + TypeTerm::TypeID(TYPEID_reprtree) + { + leaf.detach::< dyn SingletonView >(); } else if self.type_tag == TypeTerm::TypeID(TYPEID_char) @@ -285,13 +298,17 @@ impl ReprTree { { leaf.detach::< dyn SingletonView >(); } + else if self.type_tag == TypeTerm::App(vec![ + TypeTerm::TypeID(TYPEID_vec), + TypeTerm::TypeID(TYPEID_reprtree), + ]) { + leaf.detach_vec::< ReprTreeArc >(); + } else if self.type_tag == TypeTerm::App(vec![ TypeTerm::TypeID(TYPEID_vec), TypeTerm::TypeID(TYPEID_edittree), ]) { - leaf.detach_vec::< - Arc> - >(); + leaf.detach_vec::(); } else if self.type_tag == TypeTerm::App(vec![ TypeTerm::TypeID(TYPEID_vec), @@ -309,7 +326,13 @@ impl ReprTree { TypeTerm::TypeID(TYPEID_list), TypeTerm::TypeID(TYPEID_edittree), ]) { - leaf.detach::< dyn ListView>> >(); + leaf.detach::< dyn ListView >(); + } + else if self.type_tag == TypeTerm::App(vec![ + TypeTerm::TypeID(TYPEID_list), + TypeTerm::TypeID(TYPEID_reprtree), + ]) { + leaf.detach::< dyn ListView< ReprTreeArc > >(); } else if self.type_tag == TypeTerm::App(vec![ TypeTerm::TypeID(TYPEID_list), diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index cee4284..63b4ca0 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -98,6 +98,8 @@ impl PTYListStyle { let editor = node.get_edit::().unwrap(); let editor = editor.read().unwrap(); let pty_view = Self::new(style).pty_view(&editor); + + eprintln!("reset TerminalView Leaf"); node.disp.view .attach_leaf_to( Context::parse(&node.ctx, "TerminalView"), @@ -218,33 +220,14 @@ impl PTYListController { let mut e = self.editor.write().unwrap(); let cur = e.cursor.get(); let ctx = e.ctx.clone(); - let ctx = ctx.read().unwrap(); match cur.mode { ListCursorMode::Insert => { - let rt = ReprTree::new_arc(e.typ.clone()); + let mut rt = ReprTree::new_arc(e.item_type.clone()); + rt = e.item_builder.build_from(rt).expect("cant creat EditTree for item"); - let src_ladder = laddertypes::TypeTerm::Ladder(vec![ - rt.read().unwrap().get_type().clone() - ]); - let dst_ladder = laddertypes::TypeTerm::Ladder(vec![ - rt.read().unwrap().get_type().clone(), - ctx.type_term_from_str("EditTree").expect("") - ]); - ctx.apply_morphism( - &rt, - &laddertypes::MorphismType { - src_type: src_ladder, - dst_type: dst_ladder - } - ); - - let new_edittree = ctx.setup_edittree( &rt ); - - if let Some(new_edittree) = new_edittree { - let mut ne = new_edittree.get(); - let mut ne = ne.write().unwrap(); - match ne.send_cmd_obj(cmd_obj.clone()) { + let mut ne = rt.edittree(&ctx).get_mut(); + match ne.send_cmd_obj(cmd_obj.clone()) { TreeNavResult::Continue => { drop(ne); e.insert(rt); @@ -253,23 +236,19 @@ impl PTYListController { TreeNavResult::Exit => { TreeNavResult::Exit } - } - } else { - panic!("cant get edit tree"); - TreeNavResult::Continue } }, ListCursorMode::Select => { - if let Some(item) = e.get_cur_edittree() { - let res = item.write().unwrap().send_cmd_obj(cmd_obj.clone()); - let child_close_char = item.read().unwrap().ctrl.close_char.get(); + if let Some(mut item) = e.get_cur_edittree() { + let res = item.send_cmd_obj(cmd_obj.clone()); + let child_close_char = item.ctrl.close_char.get(); match res { TreeNavResult::Continue => TreeNavResult::Continue, TreeNavResult::Exit => { // child editor returned control, probably for meta-char handling.. - if cmd_obj.read().unwrap().get_type().clone() == ctx.type_term_from_str("Char").unwrap() { + if cmd_obj.read().unwrap().get_type().clone() == Context::parse(&ctx, "Char") { let co = cmd_obj.read().unwrap(); if let Some(cmd_view) = co.get_view::>() { drop(co); diff --git a/lib-nested-tty/src/lib.rs b/lib-nested-tty/src/lib.rs index 022e8f9..a9976f0 100644 --- a/lib-nested-tty/src/lib.rs +++ b/lib-nested-tty/src/lib.rs @@ -13,7 +13,7 @@ pub mod tty_application; pub mod editors; pub mod edit_tree; -//pub mod widgets; +pub mod widgets; // <<<<>>>><<>><><<>><<< * >>><<>><><<>><<<<>>>> \\ @@ -47,13 +47,13 @@ impl DisplaySegment for nested::edit_tree::EditTree { if let Some( tv_repr ) = self.disp.view .descend( Context::parse(&self.ctx, "TerminalView") ) { - if let Some(port) = + if let Some(port) = tv_repr .read().unwrap() .get_port::() { port } - + else { make_label("# could not get ViewPort #") } @@ -147,7 +147,7 @@ pub fn setup_edittree_hook(ctx: &Arc>) { else if let Ok(σ) = laddertypes::unify(&t, &posint_bin_type) { crate::editors::list::PTYListStyle::for_node( &mut *et, ("0b", "", "")); crate::editors::list::PTYListController::for_node( &mut *et, None, None ); - } + } else if let Ok(σ) = laddertypes::unify(&t, &posint_oct_type) { crate::editors::list::PTYListStyle::for_node( &mut *et, ("0o", "", "")); crate::editors::list::PTYListController::for_node( &mut *et, None, None ); @@ -178,4 +178,3 @@ pub fn setup_edittree_hook(ctx: &Arc>) { ); } - diff --git a/lib-nested-tty/src/widgets/ascii_box.rs b/lib-nested-tty/src/widgets/ascii_box.rs index c59ca6f..a57e17e 100644 --- a/lib-nested-tty/src/widgets/ascii_box.rs +++ b/lib-nested-tty/src/widgets/ascii_box.rs @@ -1,15 +1,7 @@ use { - cgmath::{Point2, Vector2}, - r3vi::{ - view::{ - InnerViewPort, Observer, ObserverBroadcast, OuterViewPort, View, - index::* - }, - }, - crate::{ - terminal::{TerminalAtom, TerminalView}, - }, - std::sync::{Arc, RwLock}, + crate::{TerminalAtom, TerminalView}, cgmath::{Point2, Vector2}, r3vi::view::{ + index::*, InnerViewPort, Observer, ObserverBroadcast, OuterViewPort, View + }, std::sync::{Arc, RwLock} }; pub struct AsciiBox {