implement color by depth through viewport to account for dynamic change of tree structure

This commit is contained in:
Michael Sippel 2023-09-08 13:40:06 +02:00
parent 62cc40c39c
commit 6c80865229
Signed by: senvas
GPG key ID: F96CF119C34B64A6
16 changed files with 134 additions and 125 deletions

View file

@ -19,8 +19,8 @@ use {
pub fn init_ctx( ctx: &mut Context ) {
ctx.add_node_ctor(
"Char",
Arc::new(|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, _depth: usize| {
Some(CharEditor::new_node(ctx))
Arc::new(|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
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<RwLock<Context>>) -> NestedNode {
pub fn new_node(ctx0: Arc<RwLock<Context>>, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> 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()

View file

@ -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<RwLock<Context>>, ty: TypeTerm, depth: usize| {
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
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::<RwLock<ListEditor>>().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<RwLock<Context>>, dst_typ: TypeTerm, depth: usize| {
|ctx0: Arc<RwLock<Context>>, dst_typ: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
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);

View file

@ -84,7 +84,7 @@ impl DigitEditor {
}
}
pub fn into_node(self, depth: usize) -> NestedNode {
pub fn into_node(self, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> 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

View file

@ -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<RwLock<Context>>, ty: TypeTerm, depth: usize| {
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
match ty {
TypeTerm::App(args) => {
if args.len() > 1 {

View file

@ -1,6 +1,6 @@
use {
r3vi::{
view::{OuterViewPort, singleton::*, sequence::*},
view::{ViewPort, OuterViewPort, singleton::*, sequence::*},
buffer::{singleton::*, vec::*}
},
crate::{
@ -26,6 +26,8 @@ pub struct ListEditor {
pub(super) addr_port: OuterViewPort<dyn SequenceView<Item = isize>>,
pub(super) mode_port: OuterViewPort<dyn SingletonView<Item = ListCursorMode>>,
depth: OuterViewPort<dyn SingletonView<Item = usize>>,
pub(crate) ctx: Arc<RwLock<Context>>,
/// 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<dyn SingletonView<Item = usize>>) -> 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() {

View file

@ -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<dyn SequenceView<Item = OuterViewPort<dyn TerminalView>>> {
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<dyn TerminalView> {
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::<ListEditor>().unwrap().read().unwrap()
)
@ -75,7 +71,7 @@ pub struct PTYListController {
split_char: Option<char>,
close_char: Option<char>,
depth: usize
depth: OuterViewPort<dyn SingletonView<Item = usize>>
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@ -85,7 +81,7 @@ impl PTYListController {
editor: Arc<RwLock<ListEditor>>,
split_char: Option<char>,
close_char: Option<char>,
depth: usize
depth: OuterViewPort<dyn SingletonView<Item = usize>>
) -> Self {
PTYListController {
editor,
@ -113,7 +109,7 @@ impl PTYListController {
}
let editor = node.get_edit::<ListEditor>().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<RwLock<ReprTree>>) -> 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()) {

View file

@ -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<dyn SequenceView<Item = NestedNode>>,
cursor: Arc<dyn SingletonView<Item = ListCursor>>,
depth: usize,
cur_cursor: ListCursor,
port: ViewPort<dyn SequenceView<Item = ListSegment>>,
@ -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<dyn SingletonView<Item = ListCursor>>,
data_port: OuterViewPort<dyn SequenceView<Item = NestedNode>>,
depth: usize
) -> Arc<RwLock<Self>> {
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;

View file

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

View file

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

View file

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

View file

@ -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::<ListEditor>().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::<TypeTermEditor>().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() )

View file

@ -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<RwLock<Context>> = 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::<crate::editors::list::ListEditor>().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<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
Some(TypeTermEditor::new_node(ctx, depth))
}));
}

View file

@ -6,6 +6,7 @@ pub use ctx::init_ctx;
use {
r3vi::{
view::{OuterViewPort, singleton::*},
buffer::{singleton::*}
},
crate::{
@ -36,8 +37,9 @@ pub struct TypeTermEditor {
data: Arc<RwLock<ReprTree>>,
close_char: SingletonBuffer<Option<char>>,
spillbuf: Arc<RwLock<Vec<Arc<RwLock<NestedNode>>>>>,
depth: OuterViewPort<dyn SingletonView<Item = usize>>,
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<RwLock<Context>>, depth: usize, term: &TypeTerm) -> NestedNode {
let mut node = TypeTermEditor::new_node(ctx.clone(), depth);
pub fn from_type_term(ctx: Arc<RwLock<Context>>, depth: OuterViewPort<dyn SingletonView<Item = usize>>, 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<RwLock<Context>>, depth: usize) -> NestedNode {
pub fn new_node(ctx: Arc<RwLock<Context>>, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> NestedNode {
let ctx : Arc<RwLock<Context>> = 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<RwLock<Context>>, depth: usize, node: NestedNode, state: State) -> NestedNode {
let _buffer = SingletonBuffer::<Option<TypeTerm>>::new( None );
fn with_node(ctx: Arc<RwLock<Context>>, cur_node: NestedNode, state: State) -> NestedNode {
let buf = SingletonBuffer::<TypeTerm>::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();
editor.write().unwrap().close_char = super_node.close_char.clone();
super_node.spillbuf = editor.read().unwrap().spillbuf.clone();
node
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::<ListEditor>().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
@ -354,12 +360,11 @@ impl TypeTermEditor {
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;
let old_node = self.cur_node.get().clone();
/* 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::<TypeTermEditor>().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

View file

@ -82,16 +82,13 @@ pub struct NestedNode {
pub diag: Option< OuterViewPort<dyn SequenceView<Item = Message>> >,
/// depth
pub depth: SingletonBuffer< usize >,
pub depth: OuterViewPort< dyn SingletonView<Item = usize> >,
/// abstract editor
pub editor: SingletonBuffer<
Option< Arc<dyn Any + Send + Sync> >
>,
/* TODO:
* - spill buffer (contains overflowing elements as well as 'split-off' parts)
*/
pub spillbuf: Arc<RwLock<Vec<Arc<RwLock<NestedNode>>>>>,
/// commander & navigation
@ -107,13 +104,13 @@ pub struct NestedNode {
}
impl NestedNode {
pub fn new(ctx: Arc<RwLock<Context>>, data: Arc<RwLock<ReprTree>>, depth: usize) -> Self {
pub fn new(ctx: Arc<RwLock<Context>>, data: Arc<RwLock<ReprTree>>, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> 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))

View file

@ -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<dyn Fn(Arc<RwLock<Self>>, TypeTerm, usize) -> Option<NestedNode> + Send + Sync>) {
pub fn add_node_ctor(&mut self, tn: &str, mk_editor: Arc<dyn Fn(Arc<RwLock<Self>>, TypeTerm, OuterViewPort<dyn SingletonView<Item = usize>>) -> Option<NestedNode> + 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<RwLock<Self>>, type_term: TypeTerm, depth: usize) -> Option<NestedNode> {
pub fn make_node(ctx: &Arc<RwLock<Self>>, type_term: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> Option<NestedNode> {
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);
}
}

View file

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