Skip to content

Commit

Permalink
Change cursor type! Cursor is at a node; lists have an extra loc at end.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed May 19, 2024
1 parent 786d07e commit 119bce5
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 307 deletions.
10 changes: 6 additions & 4 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ tree_keymap.bind_key("k", "Prev", || s::tree_nav_prev());
tree_keymap.bind_key("K", "First", || s::tree_nav_first());
tree_keymap.bind_key("j", "Next", || s::tree_nav_next());
tree_keymap.bind_key("J", "Last", || s::tree_nav_last());
tree_keymap.bind_key("h", "ChildLeft", || s::tree_nav_child_left());
tree_keymap.bind_key("H", "ParentLeft", || s::tree_nav_before_parent());
tree_keymap.bind_key("l", "ChildRight", || s::tree_nav_child_right());
tree_keymap.bind_key("L", "ParentRight", || s::tree_nav_after_parent());
tree_keymap.bind_key("l", "FirstChild", || s::tree_nav_first_child());
tree_keymap.bind_key("L", "LastChild", || s::tree_nav_last_child());
tree_keymap.bind_key("h", "Parent", || s::tree_nav_parent());
tree_keymap.bind_key("tab", "NextLeaf", || s::tree_nav_next_leaf());
tree_keymap.bind_key("S-tab", "PrevLeaf", || s::tree_nav_prev_leaf());

tree_keymap.bind_key(";", "Parent", || s::tree_nav_parent());
tree_keymap.bind_key("^", "First", || s::tree_nav_first());
Expand Down Expand Up @@ -109,6 +110,7 @@ tree_keymap.bind_key("i", "QuickInsert", || {

let text_keymap = new_keymap();
text_keymap.bind_key("esc", "ExitText", || s::text_nav_exit());
text_keymap.bind_key("enter", "ExitText", || s::text_nav_exit());
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());
Expand Down
50 changes: 24 additions & 26 deletions src/engine/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ pub enum NavCommand {

#[derive(Debug)]
pub enum TreeEdCommand {
/// In a listy sequence, insert the given node at the cursor position. In a fixed sequence,
/// replace the node after the cursor with the given node. Either way, move the cursor after
/// the new node.
/// In a listy sequence, insert the given node before the cursor. In a fixed sequence, replace
/// the node at the cursor with the given node. Either way, move the cursor to the new node.
Insert(Node),
/// Replace the node to the left of the cursor with the given node.
/// Replace the node at the cursor with the given node.
Replace(Node),
/// In a listy sequence, delete the node before the cursor. In a fixed sequence,
/// replace the node before the cursor with a hole, and move the cursor before it.
/// In a listy sequence, delete the node at the cursor and move the cursor to the left. In a
/// fixed sequence, replace the node at the cursor with a hole.
Backspace,
/// In a listy sequence, delete the node after the cursor. In a fixed sequence,
/// replace the node after the cursor with a hole, and move the cursor after it.
/// In a listy sequence, delete the node at the cursor and move the cursor to the right. In a
/// fixed sequence, replace the node at the cursor with a hole.
Delete,
}

Expand All @@ -51,12 +50,12 @@ pub enum TextEdCommand {
// TODO: cut=copy,backspace paste-copy=dup,paste
#[derive(Debug)]
pub enum ClipboardCommand {
/// Copy the node to the left of the cursor and push it onto the clipboard stack.
/// Copy the node at the cursor and push it onto the clipboard stack.
Copy,
/// Pop the top node from the clipboard stack, and insert it at the cursor (in the same manner
/// as [`TreeEdCommand::Insert`]).
Paste,
/// Swap the top node in the clipboard stack with the node to the left of the cursor.
/// Swap the top node in the clipboard stack with the node at the cursor.
PasteSwap,
/// Duplicate the top node in the clipboard stack.
Dup,
Expand All @@ -69,28 +68,27 @@ pub enum ClipboardCommand {
pub enum TreeNavCommand {
/// Move the cursor back one node.
Prev,
/// Move the cursor to before the first sibling.
/// Move the cursor to the first sibling.
First,
/// Move the cursor forward one node.
Next,
/// Move the cursor to after the last sibling.
/// Move the cursor to the last sibling, or after it in a listy sequence.
Last,
/// Move the cursor to the next location in-order.
InorderNext,
/// Move the cursor to the previous location in-order.
InorderPrev,
/// Move the cursor after its parent.
AfterParent,
/// Move the cursor before its parent.
BeforeParent,
/// Move the cursor to before the first child of the node after the cursor.
ChildRight,
/// Move the cursor to after the last child of the node before the cursor.
ChildLeft,
/// If the node before the cursor is texty, enter text mode, placing the cursor at the
/// Move the cursor to its parent node.
Parent,
/// Move the cursor to the first child of the node at the cursor.
FirstChild,
/// Move the cursor to the last child of the node at the cursor, or after it in a listy
/// sequence.
LastChild,
/// Move the cursor to the next leaf node (node with no children).
NextLeaf,
/// Move the cursor to the previous leaf node (node with no children).
PrevLeaf,
/// If the node at the cursor is texty, enter text mode, placing the cursor at the
/// end of the text.
EnterText,
/// Use this when the node before the cursor has just been `Insert`ed, to move the cursor to a
/// Use this when the node at the cursor has just been `Insert`ed, to move the cursor to a
/// convenient editing location.
FirstInsertLoc,
}
Expand Down
53 changes: 23 additions & 30 deletions src/engine/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ impl Doc {
return None;
}
Some(Doc {
cursor: Location::before_children(s, root_node).bug(),
cursor: Location::before_children(s, root_node)
.bug_msg("Root constructs must be able to have at least 1 child"),
recent: None,
undo_stack: Vec::new(),
redo_stack: Vec::new(),
Expand Down Expand Up @@ -256,32 +257,25 @@ fn execute_tree_ed(

match cmd {
Insert(node) => match cursor.insert(s, node) {
Ok(None) => Ok(vec![(*cursor, Backspace.into())]),
Ok(Some(detached_node)) => Ok(vec![(
cursor.prev_sibling(s).bug(),
Insert(detached_node).into(),
)]),
Ok(None) => Ok(vec![(*cursor, Delete.into())]),
Ok(Some(detached_node)) => Ok(vec![(*cursor, Insert(detached_node).into())]),
Err(()) => Err(EditError::CannotPlaceNode),
},
Replace(new_node) => {
let old_node = cursor.left_node(s).ok_or(EditError::NoNodeHere)?;
let old_node = cursor.node(s).ok_or(EditError::NoNodeHere)?;
if old_node.swap(s, new_node) {
*cursor = Location::after(s, new_node);
*cursor = Location::at(s, new_node);
Ok(vec![(*cursor, Replace(old_node).into())])
} else {
Err(EditError::CannotPlaceNode)
}
}
Backspace => {
let (old_node, undo_location) = cursor
.delete_neighbor(s, true)
.ok_or(EditError::NoNodeHere)?;
let (old_node, undo_location) = cursor.delete(s, true).ok_or(EditError::NoNodeHere)?;
Ok(vec![(undo_location, Insert(old_node).into())])
}
Delete => {
let (old_node, undo_location) = cursor
.delete_neighbor(s, false)
.ok_or(EditError::NoNodeHere)?;
let (old_node, undo_location) = cursor.delete(s, false).ok_or(EditError::NoNodeHere)?;
Ok(vec![(undo_location, Insert(old_node).into())])
}
}
Expand Down Expand Up @@ -332,7 +326,7 @@ fn execute_clipboard(

match cmd {
Copy => {
let node = cursor.left_node(s).ok_or(EditError::NoNodeHere)?;
let node = cursor.node(s).ok_or(EditError::NoNodeHere)?;
clipboard.push(node.deep_copy(s));
Ok(Vec::new())
}
Expand All @@ -346,9 +340,9 @@ fn execute_clipboard(
}
PasteSwap => {
let clip_node = clipboard.pop().ok_or(EditError::EmptyClipboard)?;
let doc_node = cursor.right_node(s).ok_or(EditError::NoNodeHere)?;
let doc_node = cursor.node(s).ok_or(EditError::NoNodeHere)?;
if doc_node.swap(s, clip_node) {
*cursor = Location::after(s, clip_node);
*cursor = Location::at(s, clip_node);
clipboard.push(doc_node.deep_copy(s));
Ok(vec![(*cursor, TreeEdCommand::Replace(doc_node).into())])
} else {
Expand Down Expand Up @@ -383,23 +377,22 @@ fn execute_tree_nav(
let new_loc = match cmd {
Prev => cursor.prev_cousin(s),
Next => cursor.next_cousin(s),
First => cursor.first(s),
Last => cursor.last(s),
BeforeParent => cursor.before_parent(s),
AfterParent => cursor.after_parent(s),
ChildLeft => cursor
.left_node(s)
.and_then(|node| Location::after_children(s, node)),
ChildRight => cursor
.right_node(s)
First => cursor.first_sibling(s),
Last => cursor.last_sibling(s),
PrevLeaf => cursor.prev_leaf(s),
NextLeaf => cursor.next_leaf(s),
Parent => cursor.parent(s),
FirstChild => cursor
.node(s)
.and_then(|node| Location::before_children(s, node)),
InorderNext => cursor.inorder_next(s),
InorderPrev => cursor.inorder_prev(s),
LastChild => cursor
.node(s)
.and_then(|node| Location::after_children(s, node)),
EnterText => cursor
.left_node(s)
.node(s)
.and_then(|node| Location::end_of_text(s, node)),
FirstInsertLoc => cursor
.left_node(s)
.node(s)
.map(|node| Location::first_insert_loc(s, node)),
};

Expand Down
2 changes: 1 addition & 1 deletion src/engine/doc_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl DocSet {
focus_target,
focus_height: settings.focus_height,
width_strategy: pane::WidthStrategy::NoMoreThan(settings.max_display_width),
set_focus: true,
set_focus: doc.cursor().node(s).is_none(),
};
(doc, options)
}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Default for Settings {
Settings {
max_source_width: 100,
max_display_width: 120,
focus_height: 0.5,
focus_height: 0.25,
}
}
}
24 changes: 7 additions & 17 deletions src/pretty_doc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::language::Storage;
use crate::style::{
Condition, Style, StyleLabel, ValidNotation, HOLE_STYLE, LEFT_CURSOR_STYLE, RIGHT_CURSOR_STYLE,
Condition, Style, StyleLabel, ValidNotation, CLOSE_STYLE, CURSOR_STYLE, HOLE_STYLE,
};
use crate::tree::{Location, Node, NodeId};
use crate::util::{error, SynlessBug, SynlessError};
Expand Down Expand Up @@ -90,20 +90,12 @@ impl<'d> ppp::PrettyDoc<'d> for DocRef<'d> {
fn lookup_style(self, style_label: StyleLabel) -> Result<Style, Self::Error> {
Ok(match style_label {
StyleLabel::Hole => HOLE_STYLE,
StyleLabel::Open => {
let parent = self.cursor_loc.parent_node(self.storage);
let left = self.cursor_loc.left_node(self.storage);
if parent == Some(self.node) && left.is_none() {
LEFT_CURSOR_STYLE
} else {
Style::default()
}
}
StyleLabel::Open => Style::default(),
StyleLabel::Close => {
let parent = self.cursor_loc.parent_node(self.storage);
let right = self.cursor_loc.right_node(self.storage);
if parent == Some(self.node) && right.is_none() {
RIGHT_CURSOR_STYLE
let node_at_cursor = self.cursor_loc.node(self.storage);
if parent == Some(self.node) && node_at_cursor.is_none() {
CLOSE_STYLE
} else {
Style::default()
}
Expand All @@ -126,10 +118,8 @@ impl<'d> ppp::PrettyDoc<'d> for DocRef<'d> {
}

fn node_style(self) -> Result<Style, Self::Error> {
let style = if self.cursor_loc.left_node(self.storage) == Some(self.node) {
LEFT_CURSOR_STYLE
} else if self.cursor_loc.right_node(self.storage) == Some(self.node) {
RIGHT_CURSOR_STYLE
let style = if self.cursor_loc.node(self.storage) == Some(self.node) {
CURSOR_STYLE
} else {
Style::default()
};
Expand Down
29 changes: 6 additions & 23 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {

pub fn cut(&mut self) -> Result<(), SynlessError> {
self.engine.execute(ClipboardCommand::Copy)?;
self.engine.execute(TreeEdCommand::Backspace)
self.engine.execute(TreeEdCommand::Delete)
}

/***********
Expand Down Expand Up @@ -567,29 +567,12 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
register!(
module,
rt,
TreeNavCommand::InorderNext as tree_nav_inorder_next
);
register!(
module,
rt,
TreeNavCommand::InorderPrev as tree_nav_inorder_prev
);
register!(
module,
rt,
TreeNavCommand::ChildRight as tree_nav_child_right
);
register!(module, rt, TreeNavCommand::ChildLeft as tree_nav_child_left);
register!(
module,
rt,
TreeNavCommand::BeforeParent as tree_nav_before_parent
);
register!(
module,
rt,
TreeNavCommand::AfterParent as tree_nav_after_parent
TreeNavCommand::FirstChild as tree_nav_first_child
);
register!(module, rt, TreeNavCommand::PrevLeaf as tree_nav_prev_leaf);
register!(module, rt, TreeNavCommand::NextLeaf as tree_nav_next_leaf);
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);

// Editing: Tree Ed
Expand Down
23 changes: 12 additions & 11 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ pub const HOLE_STYLE: Style = Style {
..Style::const_default()
};

pub const LEFT_CURSOR_STYLE: Style = Style {
pub const CLOSE_STYLE: Style = Style {
cursor: Some(CursorHalf::Left),
bg_color: Some((Base16Color::Base02, Priority::High)),
fg_color: Some((Base16Color::Base00, Priority::High)),
bg_color: Some((Base16Color::Base04, Priority::High)),
..Style::const_default()
};

pub const RIGHT_CURSOR_STYLE: Style = Style {
cursor: Some(CursorHalf::Right),
bg_color: Some((Base16Color::Base00, Priority::High)),
pub const CURSOR_STYLE: Style = Style {
cursor: Some(CursorHalf::Left),
bg_color: Some((Base16Color::Base02, Priority::High)),
..Style::const_default()
};

pub const FG_COLOR: Base16Color = Base16Color::Base05;
// NOTE: we might want to use Base00 as the default background, to follow the base16 conventions.
pub const BG_COLOR: Base16Color = Base16Color::Base01;
pub const BG_COLOR: Base16Color = Base16Color::Base00;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConcreteStyle {
Expand Down Expand Up @@ -233,12 +233,13 @@ impl Style {
}

impl ColorTheme {
/// The "default dark" Base16 colorscheme, by Chris Kempson (http://chriskempson.com)
/// The "default dark" Base16 colorscheme, by Chris Kempson
/// [link](https://github.com/chriskempson/base16-default-schemes)
pub fn default_dark() -> ColorTheme {
ColorTheme {
base00: Rgb::from_hex("#103030").bug(),
base01: Rgb::from_hex("#111111").bug(),
base02: Rgb::from_hex("#312121").bug(),
base00: Rgb::from_hex("#181818").bug(),
base01: Rgb::from_hex("#282828").bug(),
base02: Rgb::from_hex("#383838").bug(),
base03: Rgb::from_hex("#585858").bug(),
base04: Rgb::from_hex("#b8b8b8").bug(),
base05: Rgb::from_hex("#d8d8d8").bug(),
Expand Down
Loading

0 comments on commit 119bce5

Please sign in to comment.