From 78a5f857af342e144d329a1a5ee8f7b5022ce1d2 Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 19 May 2024 11:41:59 -0400 Subject: [PATCH] feat: next/prev text in text mode --- scripts/init.rhai | 10 ++++++++++ src/engine/command.rs | 4 ++++ src/engine/doc.rs | 2 ++ src/runtime.rs | 2 ++ src/tree/location.rs | 20 ++++++++++++++++++++ 5 files changed, 38 insertions(+) diff --git a/scripts/init.rhai b/scripts/init.rhai index f0c3625..54f6b02 100644 --- a/scripts/init.rhai +++ b/scripts/init.rhai @@ -115,6 +115,16 @@ text_keymap.bind_key("left", "Left", || s::text_nav_left()); text_keymap.bind_key("right", "Right", || s::text_nav_right()); text_keymap.bind_key("bksp", "Backspace", || s::text_ed_backspace()); text_keymap.bind_key("del", "Delete", || s::text_ed_delete()); +text_keymap.bind_key("tab", "NextLeaf", || { + s::text_nav_exit(); + s::tree_nav_next_text(); + s::tree_nav_enter_text(); +}); +text_keymap.bind_key("S-tab", "PrevLeaf", || { + s::text_nav_exit(); + s::tree_nav_prev_text(); + s::tree_nav_enter_text(); +}); // ~~~ File Selection Keymap ~~~ diff --git a/src/engine/command.rs b/src/engine/command.rs index 21e3d98..3e9cb0e 100644 --- a/src/engine/command.rs +++ b/src/engine/command.rs @@ -85,6 +85,10 @@ pub enum TreeNavCommand { NextLeaf, /// Move the cursor to the previous leaf node (node with no children). PrevLeaf, + /// Move the cursor to the next texty node. + NextText, + /// Move the cursor to the previous texty node. + PrevText, /// If the node at the cursor is texty, enter text mode, placing the cursor at the /// end of the text. EnterText, diff --git a/src/engine/doc.rs b/src/engine/doc.rs index 78a1d18..21b21e9 100644 --- a/src/engine/doc.rs +++ b/src/engine/doc.rs @@ -381,6 +381,8 @@ fn execute_tree_nav( Last => cursor.last_sibling(s), PrevLeaf => cursor.prev_leaf(s), NextLeaf => cursor.next_leaf(s), + PrevText => cursor.prev_text(s), + NextText => cursor.next_text(s), Parent => cursor.parent(s), FirstChild => cursor .node(s) diff --git a/src/runtime.rs b/src/runtime.rs index d000bd0..d1df4e6 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -571,6 +571,8 @@ impl + 'static> Runtime { ); register!(module, rt, TreeNavCommand::PrevLeaf as tree_nav_prev_leaf); register!(module, rt, TreeNavCommand::NextLeaf as tree_nav_next_leaf); + register!(module, rt, TreeNavCommand::PrevText as tree_nav_prev_text); + register!(module, rt, TreeNavCommand::NextText as tree_nav_next_text); register!(module, rt, TreeNavCommand::LastChild as tree_nav_last_child); register!(module, rt, TreeNavCommand::Parent as tree_nav_parent); register!(module, rt, TreeNavCommand::EnterText as tree_nav_enter_text); diff --git a/src/tree/location.rs b/src/tree/location.rs index 31631d8..6641490 100644 --- a/src/tree/location.rs +++ b/src/tree/location.rs @@ -233,6 +233,26 @@ impl Location { ))) } + /// Get the location at the next texty node. + pub fn next_text(mut self, s: &Storage) -> Option { + loop { + self = self.next_leaf(s)?; + if self.node(s).bug().is_texty(s) { + return Some(self); + } + } + } + + /// Get the location at the previous texty node. + pub fn prev_text(mut self, s: &Storage) -> Option { + loop { + self = self.prev_leaf(s)?; + if self.node(s).bug().is_texty(s) { + return Some(self); + } + } + } + /// Get the location at this node's parent. pub fn parent(self, s: &Storage) -> Option { Some(Location(AtNode(self.parent_node(s)?)))