diff --git a/lib-nested-core/src/editors/char/ctx.rs b/lib-nested-core/src/editors/char/ctx.rs new file mode 100644 index 0000000..8b04f4d --- /dev/null +++ b/lib-nested-core/src/editors/char/ctx.rs @@ -0,0 +1,88 @@ +use { + r3vi::{ + view::{ + OuterViewPort, + singleton::*, + port::UpdateTask + }, + buffer::singleton::* + }, + laddertypes::{TypeTerm}, + crate::{ + repr_tree::{Context, ReprTree, ReprLeaf, ReprTreeExt, GenericReprTreeMorphism}, + edit_tree::{EditTree, TreeNavResult}, + editors::{ + char::CharEditor, + ObjCommander + }, + }, + std::sync::Arc, + std::sync::RwLock +}; + +pub fn init_ctx( ctx: Arc> ) { + + let char_morph_to_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, "Char"), + Context::parse(&ctx, "Char~EditTree"), + { + let ctx = ctx.clone(); + move |rt, σ| { + { + 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::(); + let mut edittree = CharEditor::new_edit_tree( + ctx.clone(), + char_buf, + SingletonBuffer::::new(0).get_port() + ); + + rt.insert_leaf( + Context::parse(&ctx, "EditTree"), + ReprLeaf::from_singleton_buffer( + SingletonBuffer::new( + Arc::new(RwLock::new(edittree)) + ) + ) + ); + + ctx.read().unwrap().setup_edittree(rt); + } + } + ); + + let char_morph_from_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, "Char~EditTree"), + Context::parse(&ctx, "Char"), + { + let ctx = ctx.clone(); + move |rt, σ| + { + let mut b = rt + .descend(Context::parse(&ctx, "EditTree")).unwrap() + .view_singleton::>>(); + + rt.attach_leaf_to( + Context::parse(&ctx, "Char"), + b.map(|x| + x.read().unwrap() + .get_edit::().unwrap() + .read().unwrap() + .get()) + ); + } + }); + 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 f9f97c0..26e3eaf 100644 --- a/lib-nested-core/src/editors/char/mod.rs +++ b/lib-nested-core/src/editors/char/mod.rs @@ -17,72 +17,8 @@ use { std::sync::RwLock }; -pub fn init_ctx( ctx: Arc> ) { - - let char_morph_to_edittree = GenericReprTreeMorphism::new( - Context::parse(&ctx, "Char"), - Context::parse(&ctx, "Char~EditTree"), - { - let ctx = ctx.clone(); - move |rt, σ| { - { - 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::(); - let mut edittree = CharEditor::new_edit_tree( - ctx.clone(), - char_buf, - SingletonBuffer::::new(0).get_port() - ); - - rt.insert_leaf( - Context::parse(&ctx, "EditTree"), - ReprLeaf::from_singleton_buffer( - SingletonBuffer::new( - Arc::new(RwLock::new(edittree)) - ) - ) - ); - - ctx.read().unwrap().setup_edittree(rt); - } - } - ); - - let char_morph_from_edittree = GenericReprTreeMorphism::new( - Context::parse(&ctx, "Char~EditTree"), - Context::parse(&ctx, "Char"), - { - let ctx = ctx.clone(); - move |rt, σ| - { - let mut b = rt - .descend(Context::parse(&ctx, "EditTree")).unwrap() - .view_singleton::>>(); - - rt.attach_leaf_to( - Context::parse(&ctx, "Char"), - b.map(|x| - x.read().unwrap() - .get_edit::().unwrap() - .read().unwrap() - .get()) - ); - } - }); - ctx.write().unwrap().morphisms.add_morphism( char_morph_to_edittree ); - ctx.write().unwrap().morphisms.add_morphism( char_morph_from_edittree ); -} +pub mod ctx; +pub use crate::editors::char::ctx::init_ctx; pub struct CharEditor { ctx: Arc>, @@ -146,4 +82,3 @@ impl CharEditor { .set_editor( editor.clone() ) } } -