wip: TypeTermEditor

This commit is contained in:
Michael Sippel 2023-04-03 14:39:02 +02:00
parent bdc19c33de
commit b0f30e9198
Signed by: senvas
GPG key ID: 060F22F65102F95C
2 changed files with 242 additions and 95 deletions

View file

@ -1,120 +1,268 @@
use { use {
r3vi::{
buffer::singleton::*,
view::{singleton::*, sequence::*, OuterViewPort},
projection::flatten_grid::*,
projection::flatten_singleton::*
},
crate::{ crate::{
type_system::{Context}, type_system::{Context, TypeTerm, ReprTree, MorphismTypePattern},
terminal::{TerminalEvent}, terminal::{TerminalEvent},
editors::{sum::*}, editors::{sum::*, list::{ListCursorMode, ListEditor, PTYListStyle, PTYListController}},
tree::{TreeNav}, tree::{NestedNode, TreeNav, TreeNavResult, TreeCursor},
tree::NestedNode, commander::ObjCommander,
commander::Commander,
PtySegment PtySegment
}, },
termion::event::{Key}, termion::event::{Key},
std::{ std::{sync::{Arc, RwLock}},
sync::{Arc, RwLock} cgmath::{Vector2, Point2}
}
}; };
#[derive(Clone)] enum State {
enum TypeTermVar {
Any, Any,
Symbol, Char,
Num, Num,
List List,
Symbol,
Fun,
Var,
} }
pub struct TypeTermEditor { pub struct TypeTermEditor {
ctx: Arc<RwLock<Context>>, ctx: Arc<RwLock<Context>>,
ty: TypeTermVar,
sum_edit: Arc<RwLock<SumEditor>> state: State,
cur_node: SingletonBuffer<NestedNode>
} }
impl TypeTermEditor { impl TypeTermEditor {
pub fn new(ctx: Arc<RwLock<Context>>, depth: usize) -> Self { pub fn init_ctx(ctx: &mut Context) {
TypeTermEditor { ctx.add_list_typename("TypeTerm".into());
ctx: ctx.clone(), ctx.add_node_ctor(
ty: TypeTermVar::Any, "TypeTerm", Arc::new(
sum_edit: Arc::new(RwLock::new(SumEditor::new( |ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
vec![ Some(TypeTermEditor::new_node(ctx, depth))
Context::make_node( &ctx, (&ctx, "( List TypeTerm )").into(), depth + 1).unwrap(),
Context::make_node( &ctx, (&ctx, "( PosInt 10 )").into(), depth + 1 ).unwrap(),
Context::make_node( &ctx, (&ctx, "( Symbol )").into(), depth + 1 ).unwrap()
])))
} }
} )
);
pub fn into_node(self, depth: usize) -> NestedNode {
let ctx = self.ctx.clone();
let sum_edit = self.sum_edit.clone();
let view = sum_edit.read().unwrap().pty_view();
let editor = Arc::new(RwLock::new(self));
NestedNode::new(depth)
.set_ctx(ctx)
.set_nav(sum_edit)
.set_cmd(editor.clone())
.set_view(view)
}
}
impl Commander for TypeTermEditor {
type Cmd = TerminalEvent;
fn send_cmd(&mut self, event: &TerminalEvent) {
match event {
TerminalEvent::Input( termion::event::Event::Key(Key::Char(c)) ) => {
match self.ty {
TypeTermVar::Any => {
self.ty =
if *c == '(' {
let mut se = self.sum_edit.write().unwrap();
se.select(0);
se.dn();
TypeTermVar::List
} else if c.to_digit(10).is_some() {
let mut se = self.sum_edit.write().unwrap();
se.select(1);
se.dn();
se.send_cmd( event );
TypeTermVar::Num
} else {
let mut se = self.sum_edit.write().unwrap();
se.select(2);
se.dn();
se.send_cmd( event );
TypeTermVar::Symbol
};
},
_ => {
/* /*
if *c == '(' { ctx.add_list_typename("TypeLadder".into());
let child = TypeTermEditor { ctx.add_node_ctor(
ctx: self.ctx.clone(), "TypeLadder", Arc::new(
ty: self.ty.clone(), |ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
sum_edit: Arc::new(RwLock::new(SumEditor::new(
}
)
);
*/
let pattern = MorphismTypePattern {
src_tyid: ctx.get_typeid("List"),
dst_tyid: ctx.get_typeid("TypeTerm").unwrap()
};
ctx.add_morphism(pattern,
Arc::new(
|mut node, _dst_type:_| {
Some(TypeTermEditor::with_node( node.ctx.clone().unwrap(), node.depth, node, State::Any ))
}
)
);
}
fn set_state(&mut self, new_state: State) {
let mut node = match new_state {
State::Char => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( Char )").into(), 0 ).unwrap();
let mut grid = r3vi::buffer::index_hashmap::IndexBuffer::new();
grid.insert_iter(
vec![ vec![
self.sum_edit.read().unwrap().editors[0].clone(), (Point2::new(0,0), crate::terminal::make_label("'")),
self.sum_edit.read().unwrap().editors[1].clone(), (Point2::new(1,0), node.view.clone().unwrap()),
self.sum_edit.read().unwrap().editors[2].clone(), (Point2::new(2,0), crate::terminal::make_label("'")),
]))) ]
);
node.view = Some(
grid.get_port()
.flatten()
);
node
}
State::List => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List TypeTerm )").into(), 0 ).unwrap();
PTYListController::for_node( &mut node, Some(' '), Some(')') );
PTYListStyle::for_node( &mut node, ("("," ",")") );
node
}
State::Symbol => {
Context::make_node( &self.ctx, (&self.ctx, "( Symbol )").into(), 0 ).unwrap()
}
State::Num => {
Context::make_node( &self.ctx, (&self.ctx, "( PosInt 10 BigEndian )").into(), 0 ).unwrap()
}
_ => {
self.cur_node.get()
}
}; };
self.ty = TypeTermVar::List; node.goto(TreeCursor::home());
self.sum_edit.write().unwrap().select(0); self.cur_node.set(node);
self.state = new_state;
}
let l = self.sum_edit.read().unwrap().editors[0].clone(); pub fn new_node(ctx: Arc<RwLock<Context>>, depth: usize) -> NestedNode {
let l = l.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap(); Self::with_node(ctx.clone(), depth, Context::make_node( &ctx, (&ctx, "( Symbol )").into(), 0 ).unwrap(), State::Any)
l.write().unwrap().insert(TypeTermEditor::new(self.ctx.clone(), 1).into_node());
} else {
*/
self.sum_edit.write().unwrap().send_cmd( event );
//}
}
} }
fn with_node(ctx: Arc<RwLock<Context>>, depth: usize, node: NestedNode, state: State) -> NestedNode {
let editor = TypeTermEditor {
ctx: ctx.clone(),
state,
cur_node: SingletonBuffer::new(node)
};
let ed_view = editor.cur_node
.get_port()
.map(
|node|
match node.editor {
Some(e) => {
e
}, },
event => { None => {
self.sum_edit.write().unwrap().send_cmd( event ); r3vi::buffer::singleton::SingletonBuffer::new(None).get_port()
} }
} }
)
.flatten();
let view = editor.cur_node
.get_port()
.map(
|node| {
match node.view.clone() {
Some(v) => {
v
}
None => {
r3vi::view::ViewPort::new().into_outer()
}
}
}
)
.to_grid()
.flatten();
let editor = Arc::new(RwLock::new(editor));
let mut node = NestedNode::new(depth)
.set_ctx(ctx)
.set_view( view )
.set_nav(editor.clone())
.set_cmd(editor.clone());
node.editor = Some(ed_view);
//node.editor.unwrap().get_view().unwrap().get().unwrap()
node
}
}
impl TreeNav for TypeTermEditor {
fn get_cursor(&self) -> TreeCursor {
self.cur_node.get().get_cursor()
}
fn get_addr_view(&self) -> OuterViewPort<dyn SequenceView<Item = isize>> {
// fixme this is wrong
self.cur_node.get().get_addr_view()
}
fn get_mode_view(&self) -> OuterViewPort<dyn SingletonView<Item = ListCursorMode>> {
// this is wrong
self.cur_node.get().get_mode_view()
}
fn get_cursor_warp(&self) -> TreeCursor {
self.cur_node.get().get_cursor_warp()
}
fn get_max_depth(&self) -> usize {
self.cur_node.get().get_max_depth()
}
fn goby(&mut self, dir: Vector2<isize>) -> TreeNavResult {
self.cur_node.get_mut().goby(dir)
}
fn goto(&mut self, new_cur: TreeCursor) -> TreeNavResult {
self.cur_node.get_mut().goto(new_cur)
}
}
impl ObjCommander for TypeTermEditor {
fn send_cmd_obj(&mut self, co: Arc<RwLock<ReprTree>>) -> TreeNavResult {
let cmd_obj = co.clone();
let cmd_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone();
let char_type = (&self.ctx, "( Char )").into();
if cmd_type == char_type {
if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() {
let c = cmd_view.get();
match self.state {
State::Any => {
match c {
'(' => {
self.set_state( State::List );
TreeNavResult::Continue
}
'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' => {
self.set_state( State::Num );
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
'\'' => {
self.set_state( State::Char );
TreeNavResult::Continue
}
_ => {
self.set_state( State::Symbol );
self.cur_node.get_mut().goto(TreeCursor::home());
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
}
}
State::Char => {
match c {
'\'' => {
self.cur_node.get_mut().goto(TreeCursor::none());
TreeNavResult::Exit
}
_ => {
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
}
}
_ => {
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
}
} else {
TreeNavResult::Exit
}
} else {
self.cur_node.get_mut().send_cmd_obj( co )
}
} }
} }

View file

@ -5,7 +5,7 @@ pub mod term;
pub mod ladder; pub mod ladder;
pub mod repr_tree; pub mod repr_tree;
pub mod make_editor; pub mod make_editor;
//pub mod editor; pub mod editor;
pub use { pub use {
dict::*, dict::*,
@ -13,7 +13,6 @@ pub use {
repr_tree::*, repr_tree::*,
term::*, term::*,
context::{Context, MorphismMode, MorphismType, MorphismTypePattern}, context::{Context, MorphismMode, MorphismType, MorphismTypePattern},
// type_term_editor::TypeTermEditor,
make_editor::* make_editor::*
}; };