From 6c80865229997bbd190baf156487278b960d5f51 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 8 Sep 2023 13:40:06 +0200 Subject: [PATCH] implement color by depth through viewport to account for dynamic change of tree structure --- nested/src/editors/char/mod.rs | 8 +-- nested/src/editors/integer/ctx.rs | 13 ++--- nested/src/editors/integer/editor.rs | 6 +-- nested/src/editors/list/ctx.rs | 3 +- nested/src/editors/list/editor.rs | 19 +++++-- nested/src/editors/list/pty_editor.rs | 30 ++++------- nested/src/editors/list/segment.rs | 14 ++--- nested/src/editors/product/editor.rs | 2 +- nested/src/editors/product/nav.rs | 4 +- nested/src/editors/sum/editor.rs | 2 +- nested/src/editors/typeterm/cmd.rs | 11 ++-- nested/src/editors/typeterm/ctx.rs | 21 ++++---- nested/src/editors/typeterm/mod.rs | 75 +++++++++++++++------------ nested/src/tree/node.rs | 11 ++-- nested/src/type_system/context.rs | 10 ++-- nested/src/utils/color.rs | 30 ++++++----- 16 files changed, 134 insertions(+), 125 deletions(-) diff --git a/nested/src/editors/char/mod.rs b/nested/src/editors/char/mod.rs index 6e5eb49..63b0c00 100644 --- a/nested/src/editors/char/mod.rs +++ b/nested/src/editors/char/mod.rs @@ -19,8 +19,8 @@ use { pub fn init_ctx( ctx: &mut Context ) { ctx.add_node_ctor( "Char", - Arc::new(|ctx: Arc>, _ty: TypeTerm, _depth: usize| { - Some(CharEditor::new_node(ctx)) + Arc::new(|ctx: Arc>, _ty: TypeTerm, depth: OuterViewPort>| { + Some(CharEditor::new_node(ctx, depth)) })); } @@ -69,7 +69,7 @@ impl CharEditor { self.get_port().get_view().unwrap().get() } - pub fn new_node(ctx0: Arc>) -> NestedNode { + pub fn new_node(ctx0: Arc>, depth: OuterViewPort>) -> NestedNode { let data = SingletonBuffer::new('\0'); let ctx = ctx0.clone(); let editor = Arc::new(RwLock::new(CharEditor{ ctx, data: data.clone() })); @@ -80,7 +80,7 @@ impl CharEditor { ctx0.read().unwrap().type_term_from_str("( Char )").unwrap(), data.get_port().into() ), - 0 // fixme + depth ) .set_view(data .get_port() diff --git a/nested/src/editors/integer/ctx.rs b/nested/src/editors/integer/ctx.rs index b683f3e..e9189c9 100644 --- a/nested/src/editors/integer/ctx.rs +++ b/nested/src/editors/integer/ctx.rs @@ -1,4 +1,8 @@ + use { + r3vi::{ + view::{OuterViewPort, singleton::*} + }, crate::{ type_system::{Context, TypeTerm}, editors::{ @@ -19,7 +23,7 @@ pub fn init_ctx(ctx: &mut Context) { ctx.add_node_ctor( "Digit", Arc::new( - |ctx: Arc>, ty: TypeTerm, depth: usize| { + |ctx: Arc>, ty: TypeTerm, depth: OuterViewPort>| { match ty { TypeTerm::App(args) => { if args.len() > 1 { @@ -50,9 +54,6 @@ pub fn init_ctx(ctx: &mut Context) { ctx.add_morphism(pattern, Arc::new( |mut node, dst_type| { - let _depth = node.depth.get(); - let _editor = node.editor.get().unwrap().downcast::>().unwrap(); - // todo: check src_type parameter to be ( Digit radix ) match dst_type { @@ -87,7 +88,7 @@ pub fn init_ctx(ctx: &mut Context) { ctx.add_node_ctor( "PosInt", Arc::new( - |ctx0: Arc>, dst_typ: TypeTerm, depth: usize| { + |ctx0: Arc>, dst_typ: TypeTerm, depth: OuterViewPort>| { match dst_typ.clone() { TypeTerm::App(args) => { if args.len() > 1 { @@ -105,7 +106,7 @@ pub fn init_ctx(ctx: &mut Context) { .clone() .into() ]), - depth+1 + depth.map(|d| d+1) ).unwrap(); node = node.morph(dst_typ); diff --git a/nested/src/editors/integer/editor.rs b/nested/src/editors/integer/editor.rs index b2fe84a..8c6c393 100644 --- a/nested/src/editors/integer/editor.rs +++ b/nested/src/editors/integer/editor.rs @@ -84,7 +84,7 @@ impl DigitEditor { } } - pub fn into_node(self, depth: usize) -> NestedNode { + pub fn into_node(self, depth: OuterViewPort>) -> NestedNode { let data = self.get_data(); let editor = Arc::new(RwLock::new(self)); let ed = editor.write().unwrap(); @@ -99,7 +99,7 @@ impl DigitEditor { TerminalAtom::new( c.unwrap_or('?'), if c.unwrap_or('?').to_digit(r).is_some() { - TerminalStyle::fg_color((100, 140, 100)) + TerminalStyle::fg_color((90, 160, 90)) } else { //TerminalStyle::bg_color((90, 10, 10)) TerminalStyle::fg_color((200, 40, 40)) @@ -146,7 +146,7 @@ impl PosIntEditor { let mut node = Context::make_node( &ctx, (&ctx, format!("( List ( Digit {} ) )", radix).as_str()).into(), - 0 + r3vi::buffer::singleton::SingletonBuffer::new(0).get_port() ).unwrap(); // Set Type diff --git a/nested/src/editors/list/ctx.rs b/nested/src/editors/list/ctx.rs index d42f9c2..f6eb5df 100644 --- a/nested/src/editors/list/ctx.rs +++ b/nested/src/editors/list/ctx.rs @@ -1,4 +1,5 @@ use { + r3vi::{view::{OuterViewPort, singleton::*}}, crate::{ type_system::{Context, TypeTerm}, editors::list::{ListEditor, PTYListController, PTYListStyle} @@ -14,7 +15,7 @@ pub fn init_ctx(ctx: &mut Context) { ctx.add_node_ctor( "List", Arc::new( - |ctx: Arc>, ty: TypeTerm, depth: usize| { + |ctx: Arc>, ty: TypeTerm, depth: OuterViewPort>| { match ty { TypeTerm::App(args) => { if args.len() > 1 { diff --git a/nested/src/editors/list/editor.rs b/nested/src/editors/list/editor.rs index d234cb3..9b81408 100644 --- a/nested/src/editors/list/editor.rs +++ b/nested/src/editors/list/editor.rs @@ -1,6 +1,6 @@ use { r3vi::{ - view::{OuterViewPort, singleton::*, sequence::*}, + view::{ViewPort, OuterViewPort, singleton::*, sequence::*}, buffer::{singleton::*, vec::*} }, crate::{ @@ -22,10 +22,12 @@ pub struct ListEditor { pub data: VecBuffer< Arc> >, pub spillbuf: Arc>>>>, - + pub(super) addr_port: OuterViewPort>, pub(super) mode_port: OuterViewPort>, + depth: OuterViewPort>, + pub(crate) ctx: Arc>, /// item type @@ -95,13 +97,16 @@ impl ListEditor { data, spillbuf: Arc::new(RwLock::new(Vec::new())), ctx, - typ + typ, + depth: SingletonBuffer::new(0).get_port() } } - pub fn into_node(self, depth: usize) -> NestedNode { + pub fn into_node(mut self, depth: OuterViewPort>) -> NestedNode { let data = self.get_data(); let ctx = self.ctx.clone(); + + self.depth = depth.clone(); let editor = Arc::new(RwLock::new(self)); let e = editor.read().unwrap(); @@ -231,6 +236,10 @@ impl ListEditor { if let Some(idx) = cur.idx { match cur.mode { ListCursorMode::Insert => { + item.read().unwrap().depth.0.set_view( + self.depth.map(|d| d+1).get_view() + ); + self.data.insert(idx as usize, item.clone()); if self.is_listlist() { cur.mode = ListCursorMode::Select; @@ -280,7 +289,7 @@ impl ListEditor { self.nexd(); let mut b = item.spillbuf.write().unwrap(); - let mut tail_node = Context::make_node(&self.ctx, self.typ.clone(), item.depth.get()).unwrap(); + let mut tail_node = Context::make_node(&self.ctx, self.typ.clone(), self.depth.map(|d| d+1)).unwrap(); tail_node.goto(TreeCursor::home()); for node in b.iter() { diff --git a/nested/src/editors/list/pty_editor.rs b/nested/src/editors/list/pty_editor.rs index bc5dc8a..e14a642 100644 --- a/nested/src/editors/list/pty_editor.rs +++ b/nested/src/editors/list/pty_editor.rs @@ -1,6 +1,6 @@ use { r3vi::{ - view::{OuterViewPort, sequence::*}, + view::{ViewPort, OuterViewPort, sequence::*}, projection::decorate_sequence::*, }, crate::{ @@ -18,23 +18,20 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub struct PTYListStyle { - style: (String, String, String), - depth: usize + style: (String, String, String) } impl PTYListStyle { - pub fn new(style: (&str, &str, &str), depth: usize) -> PTYListStyle { + pub fn new(style: (&str, &str, &str)) -> PTYListStyle { PTYListStyle { - style: (style.0.into(), style.1.into(), style.2.into()), - depth + style: (style.0.into(), style.1.into(), style.2.into()) } } pub fn get_seg_seq_view(&self, editor: &ListEditor) -> OuterViewPort>> { let seg_seq = ListSegmentSequence::new( editor.get_cursor_port(), - editor.get_data_port(), - self.depth + editor.get_data_port() ); let se = seg_seq.read().unwrap(); se.get_view().map(move |segment| segment.pty_view()) @@ -43,8 +40,7 @@ impl PTYListStyle { pub fn pty_view(&self, editor: &ListEditor) -> OuterViewPort { let seg_seq = ListSegmentSequence::new( editor.get_cursor_port(), - editor.get_data_port(), - self.depth + editor.get_data_port() ); let seg_seq = seg_seq.read().unwrap(); @@ -59,7 +55,7 @@ impl PTYListStyle { pub fn for_node(node: &mut NestedNode, style: (&str, &str, &str)) { node.view = Some( - Self::new(style, node.depth.get()) + Self::new(style) .pty_view( &node.get_edit::().unwrap().read().unwrap() ) @@ -75,7 +71,7 @@ pub struct PTYListController { split_char: Option, close_char: Option, - depth: usize + depth: OuterViewPort> } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> @@ -85,7 +81,7 @@ impl PTYListController { editor: Arc>, split_char: Option, close_char: Option, - depth: usize + depth: OuterViewPort> ) -> Self { PTYListController { editor, @@ -113,7 +109,7 @@ impl PTYListController { } let editor = node.get_edit::().unwrap(); - let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.depth.get() ))); + let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.depth.clone() ))); node.cmd.set(Some(controller.clone())); node.close_char.set(close_char); @@ -131,10 +127,6 @@ impl PTYListController { self.editor.read().unwrap().get_item() } - pub fn set_depth(&mut self, depth: usize) { - self.depth = depth; - } - pub fn handle_term_event(&mut self, event: &TerminalEvent, _cmd_obj: Arc>) -> TreeNavResult { let mut e = self.editor.write().unwrap(); match event { @@ -176,7 +168,7 @@ impl PTYListController { match cur.mode { ListCursorMode::Insert => { - let mut new_edit = Context::make_node(&e.ctx, e.typ.clone(), self.depth+1).unwrap(); + 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()) { diff --git a/nested/src/editors/list/segment.rs b/nested/src/editors/list/segment.rs index aba00c0..4ae0368 100644 --- a/nested/src/editors/list/segment.rs +++ b/nested/src/editors/list/segment.rs @@ -22,7 +22,6 @@ pub enum ListSegment { InsertCursor, Item { editor: NestedNode, - depth: usize, cur_dist: isize, } } @@ -37,9 +36,8 @@ impl PtySegment for ListSegment { .add_style_front(TerminalStyle::bold(true)) }) } - ListSegment::Item{ editor, depth, cur_dist } => { + ListSegment::Item{ editor, cur_dist } => { let e = editor.clone(); - let d = *depth; let cur_dist = *cur_dist; editor.get_view().map_item(move |_pt, atom| { let c = e.get_cursor(); @@ -50,10 +48,11 @@ impl PtySegment for ListSegment { } else { usize::MAX }; + atom .add_style_back(bg_style_from_depth(select)) .add_style_back(TerminalStyle::bold(select==1)) - .add_style_back(fg_style_from_depth(d)) + .add_style_back(fg_style_from_depth(e.depth.get_view().get())) }) } } @@ -64,7 +63,6 @@ pub struct ListSegmentSequence { data: Arc>, cursor: Arc>, - depth: usize, cur_cursor: ListCursor, port: ViewPort>, @@ -95,7 +93,6 @@ impl SequenceView for ListSegmentSequence { ListCursorMode::Select => { ListSegment::Item { editor: self.data.get(idx)?, - depth: self.depth, cur_dist: cur - *idx as isize } } @@ -103,7 +100,6 @@ impl SequenceView for ListSegmentSequence { if *idx < cur as usize { ListSegment::Item { editor: self.data.get(idx)?, - depth: self.depth, cur_dist: cur - *idx as isize } } else if *idx == cur as usize { @@ -111,7 +107,6 @@ impl SequenceView for ListSegmentSequence { } else { ListSegment::Item { editor: self.data.get(&(*idx - 1))?, - depth: self.depth, cur_dist: cur - *idx as isize } } @@ -120,7 +115,6 @@ impl SequenceView for ListSegmentSequence { } else { ListSegment::Item { editor: self.data.get(&idx)?, - depth: self.depth, cur_dist: *idx as isize + 1 } }) @@ -131,14 +125,12 @@ impl ListSegmentSequence { pub fn new( cursor_port: OuterViewPort>, data_port: OuterViewPort>, - depth: usize ) -> Arc> { let out_port = ViewPort::new(); let mut proj_helper = ProjectionHelper::new(out_port.update_hooks.clone()); let proj = Arc::new(RwLock::new(ListSegmentSequence { cur_cursor: cursor_port.get_view().get(), port: out_port.clone(), - depth, cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| { let _old_cursor = s.cur_cursor; diff --git a/nested/src/editors/product/editor.rs b/nested/src/editors/product/editor.rs index 790a38d..97967d1 100644 --- a/nested/src/editors/product/editor.rs +++ b/nested/src/editors/product/editor.rs @@ -238,7 +238,7 @@ impl ObjCommander for ProductEditor { } } } else { - let mut e = Context::make_node(&self.ctx, t.clone(), *ed_depth+1).unwrap(); + let mut e = Context::make_node(&self.ctx, t.clone(), r3vi::buffer::singleton::SingletonBuffer::new(*ed_depth).get_port()).unwrap(); *editor = Some(e.clone()); update_segment = true; diff --git a/nested/src/editors/product/nav.rs b/nested/src/editors/product/nav.rs index c339c97..e681111 100644 --- a/nested/src/editors/product/nav.rs +++ b/nested/src/editors/product/nav.rs @@ -71,7 +71,7 @@ impl TreeNav for ProductEditor { e.goto(c.clone()); } else if c.tree_addr.len() > 0 { // create editor - let mut e = Context::make_node(&self.ctx, t.clone(), *ed_depth+1).unwrap(); + let mut e = Context::make_node(&self.ctx, t.clone(), r3vi::buffer::singleton::SingletonBuffer::new(*ed_depth+1).get_port()).unwrap(); *editor = Some(e.clone()); e.goto(c.clone()); } @@ -128,7 +128,7 @@ impl TreeNav for ProductEditor { } else { // create editor - let mut e = Context::make_node(&self.ctx, t.clone(), *ed_depth+1).unwrap(); + let mut e = Context::make_node(&self.ctx, t.clone(), r3vi::buffer::singleton::SingletonBuffer::new(*ed_depth+1).get_port()).unwrap(); *editor = Some(e.clone()); e.goby(direction); } diff --git a/nested/src/editors/sum/editor.rs b/nested/src/editors/sum/editor.rs index 24c587e..dc0c81e 100644 --- a/nested/src/editors/sum/editor.rs +++ b/nested/src/editors/sum/editor.rs @@ -60,7 +60,7 @@ impl SumEditor { NestedNode::new( ctx.clone(), ReprTree::new_arc(TypeTerm::TypeID(ctx.read().unwrap().get_typeid("Sum").unwrap())), - 0 + r3vi::buffer::singleton::SingletonBuffer::new(0).get_port() ) .set_view(view) .set_editor(editor.clone()) diff --git a/nested/src/editors/typeterm/cmd.rs b/nested/src/editors/typeterm/cmd.rs index 23271eb..56f93d8 100644 --- a/nested/src/editors/typeterm/cmd.rs +++ b/nested/src/editors/typeterm/cmd.rs @@ -44,7 +44,7 @@ impl ObjCommander for TypeTermEditor { TreeNavResult::Exit } _ => { - self.set_state( State::AnySymbol ); + self.set_state( State::FunSymbol ); self.cur_node.get_mut().goto(TreeCursor::home()); self.send_child_cmd( co ) } @@ -67,9 +67,9 @@ impl ObjCommander for TypeTermEditor { if c == '~' { let i0 = self.cur_node.get().get_edit::().unwrap(); - let cur_it = i0.clone().read().unwrap().get_item().clone(); - - if let Some(i) = cur_it { + if self.get_cursor().tree_addr.len() > 1 { + let cur_it = i0.clone().read().unwrap().get_item().clone(); + if let Some(i) = cur_it { let tte = i.get_edit::().unwrap(); if tte.read().unwrap().state != State::App { @@ -81,6 +81,9 @@ impl ObjCommander for TypeTermEditor { ); } } + } else { + return TreeNavResult::Continue; + } } self.send_child_cmd( co.clone() ) diff --git a/nested/src/editors/typeterm/ctx.rs b/nested/src/editors/typeterm/ctx.rs index 8036e72..6ee0dbd 100644 --- a/nested/src/editors/typeterm/ctx.rs +++ b/nested/src/editors/typeterm/ctx.rs @@ -1,8 +1,15 @@ use { + r3vi::{ + view::{OuterViewPort, singleton::*} + }, crate::{ type_system::{Context, TypeTerm, MorphismTypePattern}, terminal::{TerminalStyle, TerminalProjections}, - editors::{list::{PTYListStyle, PTYListController}, typeterm::{State, TypeTermEditor}} + editors::{ + list::{PTYListStyle, PTYListController, ListEditor, ListSegmentSequence}, + typeterm::{State, TypeTermEditor} + }, + PtySegment }, std::{sync::{Arc, RwLock}}, cgmath::{Point2} @@ -25,7 +32,7 @@ pub fn init_ctx(ctx: &mut Context) { let ctx : Arc> = Arc::new(RwLock::new(Context::with_parent(Some(node.ctx.clone())))); ctx.write().unwrap().meta_chars.push('~'); - let new_node = TypeTermEditor::with_node( ctx, node.depth.get(), node.clone(), State::Any ); + let new_node = TypeTermEditor::with_node( ctx, node.clone(), State::Any ); Some(new_node) })); @@ -38,7 +45,7 @@ pub fn init_ctx(ctx: &mut Context) { if vertical_view { let editor = node.get_edit::().unwrap(); let mut e = editor.write().unwrap(); - let seg_view = PTYListStyle::new( ("","~",""), node.depth.get() ).get_seg_seq_view( &mut e ); + let seg_view = PTYListStyle::new( ("","~","") ).get_seg_seq_view( &mut e ); node = node.set_view( seg_view.to_grid_vertical().flatten() @@ -71,6 +78,7 @@ pub fn init_ctx(ctx: &mut Context) { Arc::new(|mut node, _dst_type:_| { PTYListController::for_node( &mut node, Some(' '), None ); PTYListStyle::for_node( &mut node, ("","","") ); + Some(node) })); @@ -80,11 +88,6 @@ pub fn init_ctx(ctx: &mut Context) { PTYListController::for_node( &mut node, Some(' '), None ); PTYListStyle::for_node( &mut node, ("","","") ); - // display variables blue color - if let Some(v) = node.view { - node.view = Some( - v.map_item(|_i,p| p.add_style_front(TerminalStyle::fg_color((5, 120, 240))))); - } Some(node) })); @@ -117,7 +120,7 @@ pub fn init_ctx(ctx: &mut Context) { })); ctx.add_node_ctor("Type", Arc::new( - |ctx: Arc>, _ty: TypeTerm, depth: usize| { + |ctx: Arc>, _ty: TypeTerm, depth: OuterViewPort>| { Some(TypeTermEditor::new_node(ctx, depth)) })); } diff --git a/nested/src/editors/typeterm/mod.rs b/nested/src/editors/typeterm/mod.rs index 259e20d..15f7e99 100644 --- a/nested/src/editors/typeterm/mod.rs +++ b/nested/src/editors/typeterm/mod.rs @@ -6,6 +6,7 @@ pub use ctx::init_ctx; use { r3vi::{ + view::{OuterViewPort, singleton::*}, buffer::{singleton::*} }, crate::{ @@ -36,9 +37,10 @@ pub struct TypeTermEditor { data: Arc>, close_char: SingletonBuffer>, spillbuf: Arc>>>>, + depth: OuterViewPort>, - depth: usize, - + buf: SingletonBuffer< TypeTerm >, + // editing/parsing state state: State, @@ -47,8 +49,8 @@ pub struct TypeTermEditor { } impl TypeTermEditor { - pub fn from_type_term(ctx: Arc>, depth: usize, term: &TypeTerm) -> NestedNode { - let mut node = TypeTermEditor::new_node(ctx.clone(), depth); + pub fn from_type_term(ctx: Arc>, depth: OuterViewPort>, term: &TypeTerm) -> NestedNode { + let mut node = TypeTermEditor::new_node(ctx.clone(), depth.clone()); node.goto(TreeCursor::home()); match term { @@ -74,7 +76,7 @@ impl TypeTermEditor { let parent_ctx = editor.read().unwrap().cur_node.get().ctx.clone(); for x in args.iter() { - let arg_node = TypeTermEditor::from_type_term( parent_ctx.clone(), depth+1, x ); + let arg_node = TypeTermEditor::from_type_term( parent_ctx.clone(), depth.map(|d| d+1), x ); node.send_cmd_obj( ReprTree::new_leaf( @@ -92,7 +94,7 @@ impl TypeTermEditor { let parent_ctx = editor.read().unwrap().cur_node.get().ctx.clone(); for x in args.iter() { - let arg_node = TypeTermEditor::from_type_term( parent_ctx.clone(), depth+1, x ); + let arg_node = TypeTermEditor::from_type_term( parent_ctx.clone(), depth.map(|d| d+1), x ); node.send_cmd_obj( ReprTree::new_leaf( @@ -130,28 +132,27 @@ impl TypeTermEditor { fn set_state(&mut self, new_state: State) { let mut node = match new_state { State::Any => { - Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::Sym )").into() ) - } State::App => { - Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::App )").into() ) } State::Ladder => { - Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::Ladder )").into() ) } State::AnySymbol => { - Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::Sym )").into() ) }, State::FunSymbol => { - Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::Sym::Fun )").into() ) }, State::VarSymbol => { - Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::Sym::Var )").into() ) } State::Num => { @@ -160,7 +161,7 @@ impl TypeTermEditor { .morph( (&self.ctx, "( Type::Lit::Num )").into() ) } State::Char => { - Context::make_node( &self.ctx, (&self.ctx, "( Char )").into(), self.depth ).unwrap() + Context::make_node( &self.ctx, (&self.ctx, "( Char )").into(), self.depth.map(|x| x) ).unwrap() .morph( (&self.ctx, "( Type::Lit::Char )").into() ) } }; @@ -173,7 +174,7 @@ impl TypeTermEditor { self.state = new_state; } - pub fn new_node(ctx: Arc>, depth: usize) -> NestedNode { + pub fn new_node(ctx: Arc>, depth: OuterViewPort>) -> NestedNode { let ctx : Arc> = Arc::new(RwLock::new(Context::with_parent(Some(ctx)))); ctx.write().unwrap().meta_chars.push('~'); @@ -182,14 +183,13 @@ impl TypeTermEditor { Self::with_node( ctx.clone(), - depth, symb_node, State::Any ) } - fn with_node(ctx: Arc>, depth: usize, node: NestedNode, state: State) -> NestedNode { - let _buffer = SingletonBuffer::>::new( None ); + fn with_node(ctx: Arc>, cur_node: NestedNode, state: State) -> NestedNode { + let buf = SingletonBuffer::::new( TypeTerm::unit() ); let data = Arc::new(RwLock::new(ReprTree::new( (&ctx, "( Type )") @@ -199,10 +199,11 @@ impl TypeTermEditor { ctx: ctx.clone(), data: data.clone(), state, - cur_node: SingletonBuffer::new(node), + buf, + cur_node: SingletonBuffer::new(cur_node.clone()), close_char: SingletonBuffer::new(None), spillbuf: Arc::new(RwLock::new(Vec::new())), - depth + depth: cur_node.depth.clone() }; let view = editor.cur_node @@ -215,16 +216,16 @@ impl TypeTermEditor { let _cc = editor.cur_node.get().close_char; let editor = Arc::new(RwLock::new(editor)); - let mut node = NestedNode::new(ctx, data, depth) + let mut super_node = NestedNode::new(ctx, data, cur_node.depth) .set_view(view) .set_nav(editor.clone()) .set_cmd(editor.clone()) .set_editor(editor.clone()); - editor.write().unwrap().close_char = node.close_char.clone(); - node.spillbuf = editor.read().unwrap().spillbuf.clone(); - - node + editor.write().unwrap().close_char = super_node.close_char.clone(); + super_node.spillbuf = editor.read().unwrap().spillbuf.clone(); + + super_node } fn forward_spill(&mut self) { @@ -288,16 +289,17 @@ impl TypeTermEditor { let subladder_list_edit = subladder_list_edit.read().unwrap(); if subladder_list_edit.data.len() == 0 { - self.set_state( State::Any ); } } - /* unwrap a ladder if it only contains one element */ pub fn normalize_singleton(&mut self) { eprintln!("normalize singleton"); + + if self.state == State::Ladder{ + let subladder_list_node = self.cur_node.get().clone(); let subladder_list_edit = subladder_list_node.get_edit::().unwrap(); @@ -312,11 +314,15 @@ impl TypeTermEditor { other_tt.normalize_singleton(); + other_tt.depth.0.set_view( self.depth.map(|x| x).get_view() ); + self.close_char.set(other_tt.close_char.get()); self.cur_node.set(other_tt.cur_node.get()); self.state = other_tt.state; } } + + } } /* in insert mode, morph the previous element into a ladder and continue there @@ -352,14 +358,13 @@ impl TypeTermEditor { _ => { item_typterm.goto(TreeCursor::none()); drop(item_typterm); - + // else create new ladder - let mut list_node = Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth ).unwrap(); + let mut list_node = Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth.map(|d| d+1) ).unwrap(); list_node = list_node.morph( (&self.ctx, "( Type::Ladder )").into() ); let mut new_node = TypeTermEditor::with_node( self.ctx.clone(), - self.depth, list_node, State::Ladder ); @@ -382,18 +387,20 @@ impl TypeTermEditor { } } + // TODO: morph_to_app() / morph_to_list(state) + /* replace with new ladder node with self as first element */ pub fn morph_to_ladder(&mut self) { eprintln!("morph into ladder"); + let old_node = self.cur_node.get().clone(); - *old_node.depth.get_mut() += 1; - /* create a new NestedNode with TerminaltypeEditor, * that has same state & child-node as current node. */ - let old_edit_node = TypeTermEditor::new_node( self.ctx.clone(), self.depth ); + let old_edit_node = TypeTermEditor::new_node( self.ctx.clone(), self.depth.map(|d| d + 1)); + old_node.depth.0.set_view( self.depth.map(|d| d + 1).get_view() ); let old_edit_clone = old_edit_node.get_edit::().unwrap(); old_edit_clone.write().unwrap().set_state( self.state ); old_edit_clone.write().unwrap().close_char.set( old_node.close_char.get() ); @@ -401,7 +408,7 @@ impl TypeTermEditor { /* create new list-edit node for the ladder */ - let mut new_node = Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth ).unwrap(); + let mut new_node = Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), self.depth.map(|x| x) ).unwrap(); new_node = new_node.morph( (&self.ctx, "( Type::Ladder )").into() ); /* reconfigure current node to display new_node list-editor diff --git a/nested/src/tree/node.rs b/nested/src/tree/node.rs index 2e81ae3..c73da7c 100644 --- a/nested/src/tree/node.rs +++ b/nested/src/tree/node.rs @@ -82,16 +82,13 @@ pub struct NestedNode { pub diag: Option< OuterViewPort> >, /// depth - pub depth: SingletonBuffer< usize >, + pub depth: OuterViewPort< dyn SingletonView >, /// abstract editor pub editor: SingletonBuffer< Option< Arc > >, - /* TODO: - * - spill buffer (contains overflowing elements as well as 'split-off' parts) - */ pub spillbuf: Arc>>>>, /// commander & navigation @@ -107,13 +104,13 @@ pub struct NestedNode { } impl NestedNode { - pub fn new(ctx: Arc>, data: Arc>, depth: usize) -> Self { + pub fn new(ctx: Arc>, data: Arc>, depth: OuterViewPort>) -> Self { NestedNode { ctx, data, view: None, diag: None, - depth: SingletonBuffer::new(depth), + depth, editor: SingletonBuffer::new(None), spillbuf: Arc::new(RwLock::new(Vec::new())), cmd: SingletonBuffer::new(None), @@ -133,7 +130,7 @@ impl NestedNode { (&ctx, "( Char )"), buf.get_port().into() ), - 0 + SingletonBuffer::new(0).get_port() ) .set_view(buf.get_port() .map(|c| TerminalAtom::from(c)) diff --git a/nested/src/type_system/context.rs b/nested/src/type_system/context.rs index 7e57eb9..7904994 100644 --- a/nested/src/type_system/context.rs +++ b/nested/src/type_system/context.rs @@ -1,4 +1,5 @@ use { + r3vi::{view::{OuterViewPort, singleton::*}, buffer::{singleton::*}}, crate::{ type_system::{TypeDict, TypeTerm, TypeID, ReprTree}, tree::NestedNode @@ -229,7 +230,7 @@ impl Context { self.type_dict.read().unwrap().type_term_to_str(&t) } - pub fn add_node_ctor(&mut self, tn: &str, mk_editor: Arc>, TypeTerm, usize) -> Option + Send + Sync>) { + pub fn add_node_ctor(&mut self, tn: &str, mk_editor: Arc>, TypeTerm, OuterViewPort>) -> Option + Send + Sync>) { let dict = self.type_dict.clone(); let mut dict = dict.write().unwrap(); @@ -248,7 +249,7 @@ impl Context { drop(dict); self.add_morphism(morphism_pattern, Arc::new(move |node, dst_type| { - mk_editor(node.ctx.clone(), dst_type, node.depth.get()) + mk_editor(node.ctx.clone(), dst_type, node.depth) })); } @@ -275,7 +276,7 @@ impl Context { } } - pub fn make_node(ctx: &Arc>, type_term: TypeTerm, depth: usize) -> Option { + pub fn make_node(ctx: &Arc>, type_term: TypeTerm, depth: OuterViewPort>) -> Option { let mk_node = ctx.read().unwrap().get_morphism(MorphismType { src_type: None, dst_type: type_term.clone() @@ -284,7 +285,6 @@ impl Context { /* create new context per node ?? too heavy.. whats the reason? TODO */ let new_ctx = Arc::new(RwLock::new(Context::with_parent(Some(ctx.clone())))); - let _new_depth = depth; mk_node( NestedNode::new(new_ctx, ReprTree::new_arc(type_term.clone()), depth), @@ -323,7 +323,7 @@ impl Context { .type_dict.read().unwrap() .type_term_from_str(typename).unwrap(); - if let Some(node) = Context::make_node(&ctx, type_tag, 0) { + if let Some(node) = Context::make_node(&ctx, type_tag, SingletonBuffer::new(0).get_port()) { ctx.write().unwrap().nodes.insert(name, node); } } diff --git a/nested/src/utils/color.rs b/nested/src/utils/color.rs index 25772ee..532a7fe 100644 --- a/nested/src/utils/color.rs +++ b/nested/src/utils/color.rs @@ -5,23 +5,27 @@ use { pub fn bg_style_from_depth(depth: usize) -> TerminalStyle { match depth { 0 => TerminalStyle::bg_color((150,80,230)), - 1 => TerminalStyle::bg_color((75,75,75)), - 2 => TerminalStyle::bg_color((40,40,40)), - 3 => TerminalStyle::bg_color((30,30,30)), - 4 => TerminalStyle::bg_color((25,25,25)), - 5 => TerminalStyle::bg_color((20,20,20)), + 1 => TerminalStyle::bg_color((66,66,66)), + 2 => TerminalStyle::bg_color((44,44,44)), + 3 => TerminalStyle::bg_color((33,33,33)), + 4 => TerminalStyle::bg_color((28,28,28)), + 5 => TerminalStyle::bg_color((21,21,21)), _ => TerminalStyle::default(), } } pub fn fg_style_from_depth(depth: usize) -> TerminalStyle { - match depth % 6 { - 0 => TerminalStyle::fg_color((120, 120, 0)), - 1 => TerminalStyle::fg_color((250, 165, 40)), - 2 => TerminalStyle::fg_color((80, 180, 180)), - 3 => TerminalStyle::fg_color((180, 240, 85)), - 4 => TerminalStyle::fg_color((200, 190, 70)), - _ => TerminalStyle::default() - } + if depth == 0 { + TerminalStyle::fg_color((200, 200, 200)) + } else { + match depth % 5 { + 0 => TerminalStyle::fg_color((128, 106, 97)), + 1 => TerminalStyle::fg_color((100, 120, 232)), + 2 => TerminalStyle::fg_color((180, 100, 96)), + 3 => TerminalStyle::fg_color((188, 155, 18)), + 4 => TerminalStyle::fg_color((135, 182, 134)), + _ => TerminalStyle::default() + } + } }