Skip to content

Commit

Permalink
feat: next/prev text in text mode
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed May 19, 2024
1 parent 119bce5 commit 78a5f85
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~~~

Expand Down
4 changes: 4 additions & 0 deletions src/engine/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/engine/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
);
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);
Expand Down
20 changes: 20 additions & 0 deletions src/tree/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ impl Location {
)))
}

/// Get the location at the next texty node.
pub fn next_text(mut self, s: &Storage) -> Option<Location> {
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<Location> {
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<Location> {
Some(Location(AtNode(self.parent_node(s)?)))
Expand Down

0 comments on commit 78a5f85

Please sign in to comment.