more ReprTree refactoring

- <Vec Char>  ~>  <Vec ReprTree>  packing/unpacking
- remove Arc<RwLock<>> around EditTree
- gentle ListEditor update
- reactivate PosInt example
This commit is contained in:
Michael Sippel 2025-01-02 12:35:13 +01:00
parent 26186b3375
commit 47dd4ce747
Signed by: senvas
GPG key ID: F96CF119C34B64A6
20 changed files with 564 additions and 508 deletions

View file

@ -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)));

View file

@ -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 <List Char>
/* create a Representation-Tree of type <List Char>
*/
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, "<Vec ReprTree>"),
ldata.clone()
let mut rt_data = ReprTree::from_str("Hello World!");
rt_data.write().unwrap().set_halo(
Context::parse(&ctx, "<List Char>")
);
ldata_rt.write().unwrap().set_halo(
Context::parse(&ctx, "<List Char>~<List ReprTree>")
);
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, "<List Char>~<Vec Char>"))
.require(Context::parse(&ctx, "<List Char>"))
.require(Context::parse(&ctx, "<List Char>~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 <Vec Char> 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, "<List Char>~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::<dyn ListView<char>>()
.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

View file

@ -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,
" ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char> ~ EditTree"))
.require(Context::parse(&ctx,
" ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10>~Char> ~ EditTree"))
/*
.require(Context::parse(&ctx,
" ~ <PosInt 16 LittleEndian> ~ <Seq~List <Digit 16>> ~ EditTree"))
.require(Context::parse(&ctx,
" ~ <PosInt 16 BigEndian> ~ EditTree"))
.require(Context::parse(&ctx,
" ~ <PosInt 8 BigEndian> ~ 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() {
~ <List Char>
")
);
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, "<Seq Char>"));
// 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<RwLock<Context>>, comp: &mut TerminalCompositor, rt: &Arc<RwLock<ReprTree>>, 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;
}
}

View file

@ -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 ~ <Seq~List Char>")).unwrap(), 1 );
show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ~ <PosInt 10 BigEndian>")).unwrap(), 3 );
show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ~ <PosInt 16 BigEndian>")).unwrap(), 5 );
//show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ~ <PosInt 10 BigEndian>")).unwrap(), 3 );
//show_edit_tree( &ctx, &mut comp, &symbol_rt.descend(Context::parse(&ctx, "Instruction ~ Opcode ~ ~ <PosInt 16 BigEndian>")).unwrap(), 5 );
}
/* write the changes in the view of `term_port` to the terminal

View file

@ -148,7 +148,7 @@ impl LineEditor {
ctx.clone(),
Context::parse(&ctx, "<List Char>")
);
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<RwLock<ReprTree>> {
let ctx = &self.ctx;
let mut rt_line = ReprTree::from_str(
Context::parse(&ctx, "<List Char>~<Vec Char>"),
line_value
);
let mut rt_line = ReprTree::from_str(line_value);
rt_line.write().unwrap().set_halo(Context::parse(&ctx, "<List Char>~<Vec Char>"));
// create Editor & transfer data to Editor
ctx.read().unwrap().apply_morphism(

View file

@ -33,6 +33,7 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
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<RwLock<Context>> ) {
);
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<RwLock<Context>> ) {
{
let mut b = rt
.descend(Context::parse(&ctx, "EditTree")).unwrap()
.view_singleton::<Arc<RwLock<EditTree>>>();
.singleton_buffer::<EditTree>();
if rt.write().unwrap().singleton_buffer::<char>().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::<CharEditor>().unwrap()
b.get_port().map(
|x| x.get_edit::<CharEditor>().unwrap()
.read().unwrap()
.get())
.get()
)
);
}
});

View file

@ -47,7 +47,7 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
.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<RwLock<Context>> ) {
.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<RwLock<Context>> ) {
let port =
edittree
.get()
.read().unwrap()
.get_edit::<DigitEditor>().unwrap()
.read().unwrap()
.get_char_port();
@ -201,5 +200,3 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_to_u64 );
ctx.write().unwrap().morphisms.add_morphism( digit_morph_u64_to_char );
}

View file

@ -224,23 +224,21 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
)
.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, "<PosInt Radix BigEndian> ~ 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, "<PosInt Radix BigEndian>")
@ -262,22 +260,19 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
.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, "<PosInt Radix BigEndian> ~ <Seq~List <Digit Radix>>~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(
@ -299,4 +294,3 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_from_u64 );
ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_to_u64 );
}

View file

@ -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() {

View file

@ -13,6 +13,185 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub fn init_ctx__seq_to_list( ctx: &Arc<RwLock<Context>> ) {
let seq_morph_to_list_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<Seq Char>"),
Context::parse(&ctx, "<Seq Char>~<List Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>"),
src_rt.view_seq::<char>().to_list()
);
}
}
);
let seq_morph_to_list_u64 = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<Seq machine.UInt64>"),
Context::parse(&ctx, "<Seq machine.UInt64>~<List machine.UInt64>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List machine.UInt64>"),
src_rt.view_seq::<u64>().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<RwLock<Context>> ) {
let list_morph_from_vec_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char>~<Vec Char>"),
Context::parse(&ctx, "<List Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
let src_port = src_rt.descend(
Context::parse(&ctx, "<List Char>~<Vec Char>")
)
.expect("descend")
.get_port::<RwLock<Vec<char>>>().unwrap();
src_rt.attach_leaf_to( Context::parse(&ctx, "<List Char>"), src_port.to_list() );
}
}
);
let list_morph_from_vec_rt = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List ReprTree>~<Vec ReprTree>"),
Context::parse(&ctx, "<List ReprTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
let src_port = src_rt.descend(
Context::parse(&ctx, "<List ReprTree>~<Vec ReprTree>")
)
.expect("descend")
.get_port::<RwLock<Vec<ReprTreeArc>>>().unwrap();
src_rt.attach_leaf_to( Context::parse(&ctx, "<List ReprTree>"), 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<RwLock<Context>> ) {
let list_morph_to_vec_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char>"),
Context::parse(&ctx, "<List Char>~<Vec Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<Vec Char>"),
src_rt.view_list::<char>()
);
}
}
);
let list_morph_to_vec_u64 = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List machine.UInt64>"),
Context::parse(&ctx, "<List machine.UInt64>~<Vec machine.UInt64>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<Vec machine.UInt64>"),
src_rt.view_list::<u64>()
);
}
}
);
let list_morph_to_vec_reprtree = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List ReprTree>"),
Context::parse(&ctx, "<List ReprTree>~<Vec ReprTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<Vec ReprTree>"),
src_rt.view_list::< ReprTreeArc >()
);
}
}
);
let list_morph_to_vec_edittree = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List EditTree>"),
Context::parse(&ctx, "<List EditTree>~<Vec EditTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
let list_port = src_rt.get_port::<dyn ListView< EditTree >>().unwrap();
src_rt.attach_leaf_to( Context::parse(&ctx, "<Vec EditTree>"), 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<RwLock<Context>> ) {
let list_morph_rt_to_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char> ~ <List ReprTree>"),
Context::parse(&ctx, "<List Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>"),
src_rt.descend(Context::parse(&ctx, "<List ReprTree>")).expect("cant descend")
.view_list::<ReprTreeArc>()
.map(|rt| {
rt.view_singleton::<char>().get_view().get()
})
);
}
}
);
ctx.write().unwrap().morphisms.add_morphism( list_morph_rt_to_char );
}
pub fn init_ctx__pack_reprtree( ctx: &Arc<RwLock<Context>> ) {
let list_morph_rt_from_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char>"),
Context::parse(&ctx, "<List Char> ~ <List ReprTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>~<List ReprTree>"),
src_rt.view_list::<char>()
.map(|c| {
ReprTree::from_singleton_buffer(
TypeTerm::TypeID(TYPEID_char),
SingletonBuffer::<char>::new( *c )
)
})
);
}
}
);
ctx.write().unwrap().morphisms.add_morphism( list_morph_rt_from_char );
}
pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
ctx.write().unwrap().add_varname("Item");
@ -24,28 +203,36 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
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, "<List ReprTree>~<Vec ReprTree>")
.apply_substitution(&|id| σ.get(id).cloned()).clone()
)
let mut item_rt_vec = src_rt
.descend(Context::parse(&ctx, "<List ReprTree>~<Vec ReprTree>"))
.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::<usize>::new(0).get_port()
);
src_rt.insert_leaf(
Context::parse(&ctx, "<List Item> ~ 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::<EditTree>()
{
let mut et = buf.get_mut();
let mut list_editor = et.get_edit::<ListEditor>().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::<usize>::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<RwLock<Context>>) {
}
}
);
/*
let list_morph_editsetup3 = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Item> ~ EditTree"),
Context::parse(&ctx, "<List Item> ~ <List EditTree>"),
Context::parse(&ctx, "<List Item> ~ <List ReprTree> ~ <Vec ReprTree>"),
{
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, "<List Item> ~ <List EditTree>")
.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, "<List Char> ~ <List ReprTree>"),
Context::parse(&ctx, "<List Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>"),
src_rt.descend(Context::parse(&ctx, "<List ReprTree>")).expect("cant descend")
.view_list::<ReprTreeArc>()
.map(|rt| {
rt.view_singleton::<char>().get_view().get()
})
Context::parse(&ctx, "<List EditTree> ~ <Vec ReprTree>"),
list_edit.read().unwrap().data.get_port().to_list()
);
}
}
);
let list_morph_rt_from_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char>"),
Context::parse(&ctx, "<List Char> ~ <List ReprTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>~<List ReprTree>"),
src_rt.view_list::<char>()
.map({|c| {
ReprTree::from_singleton_buffer(
TypeTerm::TypeID(TYPEID_char),
SingletonBuffer::<char>::new( *c )
)
}})
);
}
}
);
let seq_morph_to_list_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<Seq Char>"),
Context::parse(&ctx, "<Seq Char>~<List Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>"),
src_rt.view_seq::<char>().to_list()
);
}
}
);
let list_morph_to_vec_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char>"),
Context::parse(&ctx, "<List Char>~<Vec Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<Vec Char>"),
src_rt.view_list::<char>()
);
}
}
);
let seq_morph_to_list_u64 = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<Seq machine.UInt64>"),
Context::parse(&ctx, "<Seq machine.UInt64>~<List machine.UInt64>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List machine.UInt64>"),
src_rt.view_seq::<u64>().to_list()
);
}
}
);
let list_morph_to_vec_u64 = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List machine.UInt64>"),
Context::parse(&ctx, "<List machine.UInt64>~<Vec machine.UInt64>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
src_rt.attach_leaf_to(
Context::parse(&ctx, "<Vec machine.UInt64>"),
src_rt.view_list::<u64>()
);
}
}
);
let list_morph_from_vec_char = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List Char>~<Vec Char>"),
Context::parse(&ctx, "<List Char>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
let src_port = src_rt.descend(Context::parse(&ctx, "<List Char>~<Vec Char>")).expect("descend")
.get_port::<RwLock<Vec<char>>>().unwrap();
src_rt.attach_leaf_to( Context::parse(&ctx, "<List Char>"), src_port.to_list() );
}
}
);
let list_morph_from_vec_rt = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List ReprTree>~<Vec ReprTree>"),
Context::parse(&ctx, "<List ReprTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
let src_port = src_rt.descend(Context::parse(&ctx, "<List ReprTree>~<Vec ReprTree>")).expect("descend")
.get_port::<RwLock<Vec<ReprTreeArc>>>().unwrap();
src_rt.attach_leaf_to( Context::parse(&ctx, "<List ReprTree>"), src_port.to_list() );
}
}
);
let list_morph_to_vec_edittree = GenericReprTreeMorphism::new(
Context::parse(&ctx, "<List EditTree>"),
Context::parse(&ctx, "<List EditTree> ~ <Vec EditTree>"),
{
let ctx = ctx.clone();
move |src_rt, σ| {
let list_port = src_rt.get_port::<dyn ListView< Arc<RwLock<EditTree>> >>().unwrap();
src_rt.attach_leaf_to( Context::parse(&ctx, "<Vec EditTree>"), 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);
}

View file

@ -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<dyn SingletonView<Item = usize>>,
pub item_type: TypeTerm,
pub master_repr: Arc<RwLock<TypeTerm>>,
pub item_builder: ReprTreeBuilder,
pub ctx: Arc<RwLock<Context>>,
/// 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<dyn SingletonView<Item = usize>>) -> 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<dyn SingletonView<Item = usize>>) -> 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<dyn ListView<EditTree>> {
pub fn get_edittree_list(&self) -> OuterViewPort<dyn ListView< EditTree >> {
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<dyn SequenceView<Item = EditTree>> {
self.get_edittree_list().to_sequence()
}
/*
pub fn get_data(&self) -> Arc<RwLock<ReprTree>> {
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<RwLock<EditTree>> {
pub fn get_edittree(&self, idx: usize) -> EditTree {
self.data.get(idx).edittree(&self.ctx).get()
}
pub fn get_cur_edittree(&self) -> Option< Arc<RwLock<EditTree>> > {
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();

View file

@ -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 {

View file

@ -23,6 +23,8 @@ pub enum ListSegment {
}
}
/* todo: switch to ListView instead of SequenceView
*/
pub struct ListSegmentSequence {
data: Arc<dyn SequenceView<Item = EditTree>>,
cursor: Arc<dyn SingletonView<Item = ListCursor>>,

View file

@ -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);

View file

@ -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<RwLock<BimapTypeDict>>,
pub morphisms: laddertypes::morphism::MorphismBase< GenericReprTreeMorphism >,
pub morphisms: laddertypes::morphism::MorphismBase<GenericReprTreeMorphism>,
/// named vertices of the graph
nodes: HashMap< String, Arc<RwLock<ReprTree>> >,
nodes: HashMap<String, ReprTreeArc>,
/// 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<TypeID>,
pub meta_chars: Vec<char>,
edittree_hook: Arc< dyn Fn(&mut EditTree, TypeTerm) + Send +Sync +'static >,
edittree_hook: Arc<dyn Fn(&mut EditTree, TypeTerm) + Send + Sync + 'static>,
/// recursion
parent: Option<Arc<RwLock<Context>>>,
@ -52,14 +52,13 @@ pub struct Context {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl Context {
pub fn with_parent(
parent: Option<Arc<RwLock<Context>>>
) -> Self {
pub fn with_parent(parent: Option<Arc<RwLock<Context>>>) -> 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<dyn Fn(&mut EditTree, TypeTerm) + Send + Sync + 'static>,
) {
self.edittree_hook = hook;
}
@ -100,22 +102,20 @@ impl Context {
}
}
pub fn apply_morphism( &self, rt: &Arc<RwLock<ReprTree>>, ty: &MorphismType ) {
if let Some(path)
= self.morphisms.find_morphism_path( ty.clone().normalize() )
{
pub fn apply_morphism(&self, rt: &Arc<RwLock<ReprTree>>, 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<RwLock<Self>>, 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<u64> {
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<u64> {
match self.get_typeid(tn) {
Some(TypeID::Var(x)) => Some(x),
_ => None
_ => None,
}
}
pub fn type_term_from_str(&self, tn: &str) -> Result<TypeTerm, laddertypes::parser::ParseError> {
pub fn type_term_from_str(
&self,
tn: &str,
) -> Result<TypeTerm, laddertypes::parser::ParseError> {
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<RwLock<Context>>, 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<ReprTreeArc> {
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<RwLock<ReprTree>>
) -> Option<SingletonBuffer<Arc<RwLock<EditTree>>>> {
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::<Arc<RwLock<EditTree>>>();
(*self.edittree_hook)(
&mut *buf.get().write().unwrap(),
typ
);
rt: &Arc<RwLock<ReprTree>>,
) -> Option<SingletonBuffer<EditTree>> {
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::<EditTree>();
(*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
}

View file

@ -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<TypeTerm>, leaf: ReprLeaf);
@ -61,8 +62,8 @@ 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_create(Context::parse(&ctx, "EditTree"))
fn edittree(&self, ctx: &Arc<RwLock<Context>>) -> 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<RwLock<ReprTree>> {
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<RwLock<ReprTree>> {
}
fn singleton_buffer<T: Clone + Send + Sync + 'static>(&self) -> SingletonBuffer<T> {
self.write().unwrap().singleton_buffer::<T>().expect("")
self.write().unwrap().singleton_buffer::<T>().expect("cant get singleton buffer")
}
fn vec_buffer<T: Clone + Send + Sync + 'static>(&self) -> VecBuffer<T> {
self.write().unwrap().vec_buffer::<T>().expect("")
self.write().unwrap().vec_buffer::<T>().expect("cant get vec buffer")
}
}

View file

@ -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::<ReprTreeArc>::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<RwLock<crate::edit_tree::EditTree>>
>::new()
VecBuffer::<crate::edit_tree::EditTree>::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<RwLock< crate::edit_tree::EditTree >>
> >();
leaf.detach::< dyn SingletonView<Item = crate::edit_tree::EditTree> >();
}
else if self.type_tag ==
TypeTerm::TypeID(TYPEID_reprtree)
{
leaf.detach::< dyn SingletonView<Item = ReprTreeArc> >();
}
else if self.type_tag ==
TypeTerm::TypeID(TYPEID_char)
@ -285,13 +298,17 @@ impl ReprTree {
{
leaf.detach::< dyn SingletonView<Item = u64> >();
}
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<RwLock< crate::edit_tree::EditTree >>
>();
leaf.detach_vec::<crate::edit_tree::EditTree>();
}
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<Arc<RwLock<crate::edit_tree::EditTree>>> >();
leaf.detach::< dyn ListView<crate::edit_tree::EditTree> >();
}
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),

View file

@ -98,6 +98,8 @@ impl PTYListStyle {
let editor = node.get_edit::<ListEditor>().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::<dyn SingletonView<Item = char>>() {
drop(co);

View file

@ -13,7 +13,7 @@ pub mod tty_application;
pub mod editors;
pub mod edit_tree;
//pub mod widgets;
pub mod widgets;
// <<<<>>>><<>><><<>><<< * >>><<>><><<>><<<<>>>> \\
@ -178,4 +178,3 @@ pub fn setup_edittree_hook(ctx: &Arc<RwLock<Context>>) {
);
}

View file

@ -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 {