From 9af00d72942e470f6d1f1a97b50131f1f84a8a96 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 18 Aug 2023 03:06:25 +0200 Subject: [PATCH] move more event handling from PTYListController into being handled by ListEditor with ListCmd --- nested/src/editors/list/cmd.rs | 137 ++++++++++++++++++++++++++ nested/src/editors/list/commander.rs | 88 ----------------- nested/src/editors/list/editor.rs | 2 +- nested/src/editors/list/mod.rs | 4 +- nested/src/editors/list/pty_editor.rs | 53 +--------- 5 files changed, 145 insertions(+), 139 deletions(-) create mode 100644 nested/src/editors/list/cmd.rs delete mode 100644 nested/src/editors/list/commander.rs diff --git a/nested/src/editors/list/cmd.rs b/nested/src/editors/list/cmd.rs new file mode 100644 index 0000000..6ac44d5 --- /dev/null +++ b/nested/src/editors/list/cmd.rs @@ -0,0 +1,137 @@ +use { + r3vi::{ + view::{singleton::*}, + buffer::{singleton::*} + }, + crate::{ + editors::list::{ListEditor, ListCursorMode}, + type_system::{Context, ReprTree}, + tree::{NestedNode, TreeNav, TreeNavResult, TreeCursor}, + commander::{ObjCommander} + }, + std::sync::{Arc, RwLock} +}; + +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum ListCmd { + DeletePxev, + DeleteNexd, + JoinNexd, + JoinPxev, + Split, + Clear, + Close, +} + +impl ListCmd { + pub fn into_repr_tree(self, ctx: &Arc>) -> Arc> { + let buf = r3vi::buffer::singleton::SingletonBuffer::new(self); + ReprTree::new_leaf( + (ctx, "( ListCmd )"), + buf.get_port().into() + ) + } +} + +impl ObjCommander for ListEditor { + fn send_cmd_obj(&mut self, cmd_obj: Arc>) -> TreeNavResult { + let cmd_repr = cmd_obj.read().unwrap(); + + if let Some(cmd) = cmd_repr.get_view::>() { + let cur = self.cursor.get(); + drop(cmd_repr); + + if let Some(idx) = cur.idx { + match cur.mode { + ListCursorMode::Select => { + if let Some(mut item) = self.get_item().clone() { + if self.is_listlist() { + let item_cur = item.get_cursor(); + + match cmd.get() { + ListCmd::DeletePxev => { + if idx > 0 + && item_cur.tree_addr.iter().fold( + true, + |is_zero, x| is_zero && (*x == 0) + ) + { + self.listlist_join_pxev(idx, &item); + TreeNavResult::Continue + } else { + item.send_cmd_obj(cmd_obj) + } + } + + ListCmd::DeleteNexd => { + let item_cur = item.get_cursor_warp(); + let next_idx = idx as usize + 1; + + if next_idx < self.data.len() + && item_cur.tree_addr.iter().fold( + true, + |is_end, x| is_end && (*x == -1) + ) + { + self.listlist_join_nexd(next_idx, &item); + TreeNavResult::Continue + } else { + item.send_cmd_obj(cmd_obj) + } + } + + _ => { + TreeNavResult::Continue + } + } + } else { + TreeNavResult::Exit + } + } else { + TreeNavResult::Exit + } + }, + + ListCursorMode::Insert => { + match cmd.get() { + ListCmd::DeletePxev => { + self.delete_pxev(); + TreeNavResult::Continue + } + ListCmd::DeleteNexd => { + self.delete_nexd(); + TreeNavResult::Continue + } + ListCmd::Split => { + self.listlist_split(); + TreeNavResult::Continue + } + ListCmd::Clear => { + self.clear(); + TreeNavResult::Continue + } + ListCmd::Close => { + self.goto(TreeCursor::none()); + TreeNavResult::Exit + } + _ =>{ + TreeNavResult::Continue + } + } + } + } + } else { + TreeNavResult::Exit + } + + } else { + if let Some(cur_item) = self.get_item_mut() { + drop(cmd_repr); + cur_item.write().unwrap().send_cmd_obj(cmd_obj) + } else { + TreeNavResult::Continue + } + } + } +} + diff --git a/nested/src/editors/list/commander.rs b/nested/src/editors/list/commander.rs deleted file mode 100644 index 874a566..0000000 --- a/nested/src/editors/list/commander.rs +++ /dev/null @@ -1,88 +0,0 @@ -use { - r3vi::{ - view::{singleton::*}, - buffer::{singleton::*} - }, - crate::{ - editors::list::ListEditor, - type_system::{Context, ReprTree}, - tree::{NestedNode, TreeNav, TreeNavResult, TreeCursor}, - commander::{ObjCommander} - }, - std::sync::{Arc, RwLock} -}; - -#[derive(Copy, Clone, PartialEq, Eq)] -pub enum ListCmd { - DeletePxev, - DeleteNexd, - JoinNexd, - JoinPxev, - Split, - Clear, - Close, -} - -impl ListCmd { - fn into_repr_tree(self, ctx: &Arc>) -> Arc> { - let buf = r3vi::buffer::singleton::SingletonBuffer::new(self); - ReprTree::new_leaf( - (ctx, "( ListCmd )"), - buf.get_port().into() - ) - } -} -/* -impl Into< Arc> > for (&Arc>, ListCmd) { - fn into(self) -> Arc> { - self.1.into_repr_tree(self.0) - } -} -*/ -impl ObjCommander for ListEditor { - fn send_cmd_obj(&mut self, cmd_obj: Arc>) -> TreeNavResult { - let cmd_repr = cmd_obj.read().unwrap(); - if let Some(cmd) = cmd_repr.get_view::>() { - match cmd.get() { - ListCmd::DeletePxev => { - self.delete_pxev(); - TreeNavResult::Continue - } - ListCmd::DeleteNexd => { - self.delete_nexd(); - TreeNavResult::Continue - } - ListCmd::JoinPxev => { - // TODO - //self.listlist_join_pxev(); - TreeNavResult::Continue - } - ListCmd::JoinNexd => { - // TODO - //self.listlist_join_nexd(); - TreeNavResult::Continue - } - ListCmd::Split => { - self.listlist_split(); - TreeNavResult::Continue - } - ListCmd::Clear => { - self.clear(); - TreeNavResult::Continue - } - ListCmd::Close => { - self.goto(TreeCursor::none()); - TreeNavResult::Exit - } - } - } else { - if let Some(cur_item) = self.get_item_mut() { - drop(cmd_repr); - cur_item.write().unwrap().send_cmd_obj(cmd_obj) - } else { - TreeNavResult::Continue - } - } - } -} - diff --git a/nested/src/editors/list/editor.rs b/nested/src/editors/list/editor.rs index 802c99f..70cda6c 100644 --- a/nested/src/editors/list/editor.rs +++ b/nested/src/editors/list/editor.rs @@ -5,7 +5,7 @@ use { }, crate::{ type_system::{Context, TypeTerm, ReprTree}, - editors::list::{ListCursor, ListCursorMode, commander::ListCmd, PTYListController, PTYListStyle}, + editors::list::{ListCursor, ListCursorMode, ListCmd, PTYListController, PTYListStyle}, tree::{NestedNode, TreeNav, TreeCursor}, diagnostics::Diagnostics }, diff --git a/nested/src/editors/list/mod.rs b/nested/src/editors/list/mod.rs index 8494856..f67e533 100644 --- a/nested/src/editors/list/mod.rs +++ b/nested/src/editors/list/mod.rs @@ -5,13 +5,13 @@ pub mod editor; pub mod nav; pub mod segment; pub mod pty_editor; -pub mod commander; +pub mod cmd; pub use { cursor::{ListCursor, ListCursorMode}, editor::ListEditor, segment::{ListSegment, ListSegmentSequence}, pty_editor::{PTYListStyle, PTYListController}, - commander::ListCmd + cmd::ListCmd }; diff --git a/nested/src/editors/list/pty_editor.rs b/nested/src/editors/list/pty_editor.rs index 5f5b164..9ed9719 100644 --- a/nested/src/editors/list/pty_editor.rs +++ b/nested/src/editors/list/pty_editor.rs @@ -161,6 +161,7 @@ impl ObjCommander for PTYListController { let cur = e.cursor.get(); let cur_depth = e.get_cursor().tree_addr.len(); + let ctx0 = e.ctx.clone(); let ctx = e.ctx.clone(); let ctx = ctx.read().unwrap(); @@ -218,12 +219,10 @@ impl ObjCommander for PTYListController { ListCursorMode::Insert => { match event { TerminalEvent::Input(Event::Key(Key::Backspace)) => { - e.delete_pxev(); - TreeNavResult::Continue + e.send_cmd_obj( ListCmd::DeletePxev.into_repr_tree(&ctx0) ) } TerminalEvent::Input(Event::Key(Key::Delete)) => { - e.delete_nexd(); - TreeNavResult::Continue + e.send_cmd_obj( ListCmd::DeletePxev.into_repr_tree(&ctx0) ) } _ => { let mut node = Context::make_node(&e.ctx, e.typ.clone(), self.depth).unwrap(); @@ -251,52 +250,10 @@ impl ObjCommander for PTYListController { if e.is_listlist() { match event { TerminalEvent::Input(Event::Key(Key::Backspace)) => { - let item_cur = item.get_cursor(); - - if idx > 0 - && item_cur.tree_addr.iter().fold( - true, - |is_zero, x| is_zero && (*x == 0) - ) - { - e.listlist_join_pxev(idx, &item); - - /* Optional: recursive joining - - if item_cur.tree_addr.len() > 1 { - let mut item = e.get_item_mut().unwrap(); - item.handle_terminal_event(event); - } - */ - TreeNavResult::Continue - } else { - item.send_cmd_obj(cmd_obj) - } + e.send_cmd_obj( ListCmd::DeletePxev.into_repr_tree(&ctx0) ) } TerminalEvent::Input(Event::Key(Key::Delete)) => { - let item_cur = item.get_cursor_warp(); - let next_idx = idx as usize + 1; - - if next_idx < e.data.len() - && item_cur.tree_addr.iter().fold( - true, - |is_end, x| is_end && (*x == -1) - ) - { - e.listlist_join_nexd(next_idx, &item); - - /* Optional: recursive joining - - if item_cur.tree_addr.len() > 1 { - let mut item = e.get_item_mut().unwrap(); - item.handle_terminal_event(event); - } - */ - - TreeNavResult::Continue - } else { - item.send_cmd_obj(cmd_obj) - } + e.send_cmd_obj( ListCmd::DeleteNexd.into_repr_tree(&ctx0) ) } TerminalEvent::Input(Event::Key(Key::Char(c))) => {