lib-nested/nested/src/make_editor.rs

239 lines
8.2 KiB
Rust
Raw Normal View History

2022-05-08 23:30:49 +02:00
use {
crate::{
2022-11-18 00:21:29 +01:00
core::{TypeTerm, TypeLadder, Context, OuterViewPort},
terminal::{TerminalView, TerminalEditor, TerminalEvent, TerminalEditorResult, make_label},
tree::{TreeNav},
2022-05-08 23:30:49 +02:00
integer::PosIntEditor,
2022-06-19 23:13:21 +02:00
list::{ListEditor, PTYListEditor},
sequence::{decorator::{SeqDecorStyle}},
product::editor::ProductEditor,
2022-11-18 00:21:29 +01:00
sum::SumEditor,
char_editor::CharEditor,
diagnostics::Diagnostics,
Nested
2022-05-08 23:30:49 +02:00
},
2022-06-26 00:49:35 +02:00
cgmath::{Vector2, Point2},
2022-06-19 23:13:21 +02:00
std::sync::{Arc, RwLock},
2022-05-08 23:30:49 +02:00
};
enum RhsNode {
Sum (
Arc<RwLock< PTYListEditor< RhsNode > >>
),
Product (
Arc<RwLock< PTYListEditor< RhsNode > >>
),
String(
Arc<RwLock< PTYListEditor< CharEditor > >>
)
}
impl TreeNav for RhsNode {}
impl TerminalEditor for RhsNode {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
make_label("todo")
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
TerminalEditorResult::Continue
}
}
impl Diagnostics for RhsNode {}
impl Nested for RhsNode {}
struct GrammarRuleEditor {
lhs: Arc<RwLock<PTYListEditor<CharEditor>>>,
rhs: Arc<RwLock<PTYListEditor<RhsNode>>>
}
pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let mut ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
2022-11-18 00:21:29 +01:00
ctx.write().unwrap().add_editor_ctor(
"Char", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, _depth: usize| {
2022-11-18 00:21:29 +01:00
Some(
Arc::new(RwLock::new(CharEditor::new()))
2022-11-18 00:21:29 +01:00
as Arc<RwLock<dyn Nested + Send + Sync>>)
}
)
);
2022-11-18 00:21:29 +01:00
ctx.write().unwrap().add_editor_ctor(
"List", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
2022-11-18 00:21:29 +01:00
match ty {
TypeTerm::Type {
id, args
} => {
if args.len() > 0 {
// todod factor style out of type arGS
let style = if args.len() > 1 {
match args[1] {
TypeTerm::Num(0) => SeqDecorStyle::Plain,
TypeTerm::Num(1) => SeqDecorStyle::HorizontalSexpr,
TypeTerm::Num(2) => SeqDecorStyle::VerticalSexpr,
TypeTerm::Num(3) => SeqDecorStyle::DoubleQuote,
TypeTerm::Num(4) => SeqDecorStyle::Tuple,
TypeTerm::Num(5) => SeqDecorStyle::EnumSet,
TypeTerm::Num(6) => SeqDecorStyle::Path,
_ => SeqDecorStyle::HorizontalSexpr
}
}else {
SeqDecorStyle::HorizontalSexpr
};
let delim = if args.len() > 1 {
match args[1] {
TypeTerm::Num(0) => ' ',
TypeTerm::Num(1) => ' ',
TypeTerm::Num(2) => '\n',
TypeTerm::Num(3) => '"',
TypeTerm::Num(4) => ',',
TypeTerm::Num(5) => ',',
TypeTerm::Num(6) => '/',
_ => '\0'
}
}else {
'\0'
};
Some(
Arc::new(RwLock::new(PTYListEditor::new(
Box::new({
move || {
Context::make_editor(ctx.clone(), args[0].clone(), depth + 1).unwrap()
2022-11-18 00:21:29 +01:00
}
}),
style,
delim,
depth
)))
as Arc<RwLock<dyn Nested + Send + Sync>>
)
} else {
None
}
}
2022-11-18 00:21:29 +01:00
_ => None
}
}
)
);
2022-11-18 00:21:29 +01:00
ctx.write().unwrap().add_editor_ctor(
"Symbol", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
Context::make_editor(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( List Char 0 )").unwrap(),
2022-11-18 00:21:29 +01:00
depth
)
}
)
);
2022-11-18 00:21:29 +01:00
ctx.write().unwrap().add_editor_ctor(
"String", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
Context::make_editor(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( List Char 3 )").unwrap(),
depth
2022-11-18 00:21:29 +01:00
)
}
)
);
2022-11-18 00:21:29 +01:00
ctx.write().unwrap().add_editor_ctor(
"TypeTerm", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
2022-11-18 00:21:29 +01:00
let mut s = SumEditor::new(
vec![
Context::make_editor(ctx.clone(), ctx.read().unwrap().type_term_from_str("( Symbol )").unwrap(), depth+1).unwrap(),
Context::make_editor(ctx.clone(), ctx.read().unwrap().type_term_from_str("( PosInt 10 )").unwrap(), depth+1).unwrap(),
Context::make_editor(ctx.clone(), ctx.read().unwrap().type_term_from_str("( List TypeTerm )").unwrap(), depth+1).unwrap(),
2022-11-18 00:21:29 +01:00
]
);
s.select(0);
Some(
Arc::new(RwLock::new(
s
))
)
}
)
);
ctx
}
pub fn init_math_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let mut ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
ctx.write().unwrap().add_typename("BigEndian".into());
ctx.write().unwrap().add_editor_ctor(
"PosInt", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, _depth: usize| {
match ty {
TypeTerm::Type {
id, args
} => {
if args.len() > 0 {
match args[0] {
TypeTerm::Num(radix) => {
Some(
Arc::new(RwLock::new(PosIntEditor::new(radix as u32)))
as Arc<RwLock<dyn Nested + Send + Sync>>
)
},
_ => None
}
} else {
None
}
}
_ => None
}
}
)
);
2022-11-18 00:21:29 +01:00
ctx
2022-05-08 23:30:49 +02:00
}
pub fn init_os_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let mut ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
ctx.write().unwrap().add_editor_ctor(
"PathSegment", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
Context::make_editor(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( List Char 0 )").unwrap(),
depth
)
}
)
);
ctx.write().unwrap().add_editor_ctor(
"Path", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
Context::make_editor(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( List PathSegment 6 )").unwrap(),
depth+1
)
}
)
);
ctx
}
2022-05-08 23:30:49 +02:00
2022-11-18 00:21:29 +01:00