From 0ac4a3474317962bcd544e5e92b1327bd79045e9 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Wed, 17 Jan 2024 03:42:47 +0100 Subject: [PATCH] example wip: create display for edit tree --- examples/tty-02-node/Cargo.toml | 1 + examples/tty-02-node/src/main.rs | 94 +++++++++--------------- lib-nested-core/src/editors/list/ctx.rs | 31 +------- lib-nested-core/src/repr_tree/context.rs | 42 ++++++++++- lib-nested-tty/src/editors/list.rs | 20 ++--- lib-nested-tty/src/editors/mod.rs | 1 - 6 files changed, 87 insertions(+), 102 deletions(-) diff --git a/examples/tty-02-node/Cargo.toml b/examples/tty-02-node/Cargo.toml index 589ee96..ccfc655 100644 --- a/examples/tty-02-node/Cargo.toml +++ b/examples/tty-02-node/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +laddertypes = { path = "../../../lib-laddertypes" } r3vi = { path = "../../../lib-r3vi" } nested = { path = "../../lib-nested-core" } nested-tty = { path = "../../lib-nested-tty" } diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index 7731127..8607683 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -32,6 +32,32 @@ async fn main() { nested::editors::integer::editor::init_ctx( ctx.clone() ); nested::editors::list::init_ctx( ctx.clone() ); + + let char_type = Context::parse(&ctx, "Char"); + let digit_type = Context::parse(&ctx, ""); + let list_type = Context::parse(&ctx, ""); + + ctx.write().unwrap().set_edittree_hook( + Arc::new( + move |et: Arc>, t: laddertypes::TypeTerm| { + if let Ok(σ) = laddertypes::unify(&t, &char_type) { + let mut et = et.write().unwrap(); + *et = nested_tty::editors::edittree_make_char_view(et.clone()); + } + else if let Ok(σ) = laddertypes::unify(&t, &digit_type) { + let mut et = et.write().unwrap(); + *et = nested_tty::editors::edittree_make_digit_view(et.clone()); + } + else if let Ok(σ) = laddertypes::unify(&t, &list_type) { + let mut et = et.write().unwrap(); + nested_tty::editors::list::PTYListStyle::for_node( &mut *et, ("(", ",", ")")); + nested_tty::editors::list::PTYListController::for_node( &mut *et, None, None ); + *et = nested_tty::editors::edittree_make_list_edit(et.clone()); + } + } + ) + ); + /* structure of Repr-Tree * * === Repr-Tree === @@ -60,67 +86,13 @@ async fn main() { SingletonBuffer::new('x').get_port().into() ); - let port_char = rt_digit.read().unwrap() - .descend(Context::parse(&ctx, "Char")).unwrap().read().unwrap() - .get_port::>().unwrap().0; - - ctx.read().unwrap() - .morphisms - .morph( - rt_digit.clone(), - &Context::parse(&ctx, "~EditTree") - ); - - let port_edit = rt_digit.read().unwrap() - .descend(Context::parse(&ctx, "EditTree")).unwrap() - .read().unwrap() - .get_port::> >>().unwrap(); - /* setup TTY-Display for DigitEditor */ - { - let et = port_edit.get_view().unwrap().get().clone(); - let mut et = et.write().unwrap(); - *et = nested_tty::editors::edittree_make_digit_view(et.clone()); - } + let edittree_digit = ctx.read().unwrap().setup_edittree(rt_digit.clone(), r3vi::buffer::singleton::SingletonBuffer::new(0).get_port()); //--- - let rt_string = ReprTree::new_arc( Context::parse(&ctx, ">") ); - ctx.read().unwrap() - .morphisms - .morph( - rt_string.clone(), - &Context::parse(&ctx, ">~EditTree") - ); - - let editport_string = rt_string.read().unwrap() - .descend(Context::parse(&ctx, "EditTree")).unwrap() - .read().unwrap() - .get_port::>> >().unwrap(); - - /* setup TTY-Display for ListEditor - */ - { - let et = editport_string.get_view().unwrap().get().clone(); - let mut et = et.write().unwrap(); - *et = nested_tty::editors::edittree_make_list_edit(et.clone()); - } -/* - let vec_string = Arc::new(RwLock::new(Vec::::new())); - - rt_string.write().unwrap() - .insert_leaf( - vec![ Context::parse(&ctx, "") ].into_iter(), - r3vi::view::ViewPort::with_view(vec_string).into_outer().into() - ); - - - rt_string.write().unwrap() - .insert_leaf( - vec![ Context::parse(&ctx, "EditTree") ].into_iter(), - r3vi::view::ViewPort::with_view() - ); -*/ + let rt_string = ReprTree::new_arc( Context::parse(&ctx, ">>") ); + let edittree = ctx.read().unwrap().setup_edittree(rt_string.clone(), r3vi::buffer::singleton::SingletonBuffer::new(0).get_port()); /* setup terminal */ @@ -128,9 +100,9 @@ async fn main() { /* event handler */ let ctx = ctx.clone(); - let et1 = editport_string.clone(); + let et1 = edittree.clone(); move |ev| { - et1.get_view().unwrap().get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx)); + et1.write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx)); } }); @@ -146,7 +118,7 @@ async fn main() { }) .offset(Vector2::new(5, 0)), ); - compositor.write().unwrap().push( port_edit.get_view().unwrap().get().read().unwrap().display_view().offset(Vector2::new(0,2)) ); + compositor.write().unwrap().push( edittree_digit.read().unwrap().display_view().offset(Vector2::new(0,2)) ); let label = ctx.read().unwrap().type_term_to_str(&rt_digit.read().unwrap().get_type()); compositor @@ -154,7 +126,7 @@ async fn main() { .unwrap() .push(nested_tty::make_label(&label).offset(Vector2::new(0, 1))); - compositor.write().unwrap().push( editport_string.get_view().unwrap().get().read().unwrap().display_view().offset(Vector2::new(0,4)) ); + compositor.write().unwrap().push( edittree.read().unwrap().display_view().offset(Vector2::new(0,4)) ); let label = ctx.read().unwrap().type_term_to_str(&rt_string.read().unwrap().get_type()); compositor diff --git a/lib-nested-core/src/editors/list/ctx.rs b/lib-nested-core/src/editors/list/ctx.rs index 90ef8c5..0ef2180 100644 --- a/lib-nested-core/src/editors/list/ctx.rs +++ b/lib-nested-core/src/editors/list/ctx.rs @@ -11,7 +11,7 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub fn init_ctx(ctx: Arc>) { - + ctx.write().unwrap().add_list_typename("List".into()); ctx.write().unwrap().add_varname("Item"); let mt = crate::repr_tree::MorphismType { src_type: Context::parse(&ctx, ""), @@ -24,7 +24,6 @@ pub fn init_ctx(ctx: Arc>) { move |rt, σ| { let item_id = laddertypes::TypeID::Var( ctx.read().unwrap().get_var_typeid("Item").unwrap() ); if let Some( item_type ) = σ.get( &item_id ) { - eprintln!("create list of {:?}", item_type); let mut edittree_list = ListEditor::new( ctx.clone(), item_type.clone() @@ -43,33 +42,5 @@ pub fn init_ctx(ctx: Arc>) { } } ); -/* - - ctx.add_typename("ListCmd".into()); - ctx.add_list_typename("List".into()); - ctx.add_node_ctor( - "List", Arc::new( - |ctx: Arc>, ty: TypeTerm, depth: OuterViewPort>| { - match ty { - TypeTerm::App(args) => { - if args.len() > 1 { - let typ = args[1].clone(); - - let mut node = ListEditor::new(ctx.clone(), typ).into_node(depth); - -// PTYListController::for_node( &mut node, Some(','), Some('}') ); -// PTYListStyle::for_node( &mut node, ("{",", ","}") ); - - Some(node) - } else { - None - } - } - _ => None - } - } - ) - ); - */ } diff --git a/lib-nested-core/src/repr_tree/context.rs b/lib-nested-core/src/repr_tree/context.rs index e280a9d..ef07efe 100644 --- a/lib-nested-core/src/repr_tree/context.rs +++ b/lib-nested-core/src/repr_tree/context.rs @@ -29,12 +29,16 @@ pub struct Context { pub list_types: Vec< TypeID >, pub meta_chars: Vec< char >, + edittree_hook: Arc< dyn Fn(Arc>, TypeTerm) + Send +Sync +'static >, + /// recursion parent: Option>>, } 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(), @@ -51,6 +55,8 @@ impl Context { None => Vec::new() }, parent, + + edittree_hook: Arc::new(|_et, _t| {}) } } @@ -58,6 +64,10 @@ impl Context { Context::with_parent(None) } + pub fn set_edittree_hook(&mut self, hook: Arc< dyn Fn(Arc>, TypeTerm) + Send +Sync +'static >) { + self.edittree_hook = hook; + } + pub fn depth(&self) -> usize { if let Some(parent) = self.parent.as_ref() { parent.read().unwrap().depth() + 1 @@ -165,6 +175,36 @@ impl Context { None } } + + pub fn setup_edittree( + &self, + rt: Arc>, + depth: OuterViewPort> + ) -> Arc> { + let ladder = TypeTerm::Ladder(vec![ + rt.read().unwrap().get_type().clone(), + self.type_term_from_str("EditTree").expect("") + ]); + + self.morphisms.morph( + rt.clone(), + &ladder + ); + + let new_edittree = rt + .read().unwrap() + .descend( + self.type_term_from_str("EditTree").expect("") + ).unwrap() + .read().unwrap() + .get_view::> >>() + .unwrap() + .get(); + + (*self.edittree_hook)( new_edittree.clone(), rt.read().unwrap().get_type().clone() ); + + new_edittree + } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index 0075089..64cf6a1 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -139,6 +139,7 @@ impl PTYListController { split_char: Option, close_char: Option ) { + /* { let ctx = node.ctx.as_ref(); let mut ctx = ctx.write().unwrap(); @@ -150,7 +151,7 @@ impl PTYListController { ctx.meta_chars.push(*c); } } - + */ let editor = node.get_edit::().unwrap(); let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.disp.depth.clone() ))); @@ -219,21 +220,22 @@ impl PTYListController { match cur.mode { ListCursorMode::Insert => { - /* TODO - let mut new_edit = Context::make_node(&e.ctx, e.typ.clone(), self.depth.map(|d| d+1)).unwrap(); - new_edit.goto(TreeCursor::home()); - - match new_edit.send_cmd_obj(cmd_obj.clone()) { + let rt = ReprTree::new_arc(e.typ.clone()); + let new_edittree = ctx.setup_edittree( + rt, + self.depth.map(|d| d+1) + ); + let mut ne = new_edittree.write().unwrap(); + match ne.send_cmd_obj(cmd_obj.clone()) { TreeNavResult::Continue => { - e.insert(Arc::new(RwLock::new(new_edit.clone()))); + drop(ne); + e.insert(new_edittree.clone()); TreeNavResult::Continue } TreeNavResult::Exit => { TreeNavResult::Exit } } - */ - TreeNavResult::Continue }, ListCursorMode::Select => { if let Some(item) = e.get_item_mut() { diff --git a/lib-nested-tty/src/editors/mod.rs b/lib-nested-tty/src/editors/mod.rs index 4e2e30a..eee1b74 100644 --- a/lib-nested-tty/src/editors/mod.rs +++ b/lib-nested-tty/src/editors/mod.rs @@ -90,7 +90,6 @@ pub fn edittree_make_list_edit( ) -> EditTree { list::PTYListStyle::for_node( &mut node, ("(", "", ")") ); list::PTYListController::for_node( &mut node, None, None ); - node } /*