From 6c659a17dc490d98758adaf0bfc6025ce7d6b770 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Fri, 9 Jan 2015 22:40:41 -0800 Subject: [PATCH] Switched undo stack to use a linked list instead of a vector. This makes sure there aren't pathological cases where the user has been typing for a very long time, and the undo stack needs to change capacity. --- src/buffer/mod.rs | 11 ++++++----- todo.md | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 7062796..ec63d7d 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] use std::mem; +use std::collections::DList; use self::node::{BufferNode, BufferNodeGraphemeIter, BufferNodeLineIter}; use self::line::{Line, LineEnding}; @@ -18,7 +19,7 @@ mod node; pub struct Buffer { text: BufferNode, pub line_ending_type: LineEnding, - undo_stack: Vec, + undo_stack: DList, } @@ -27,7 +28,7 @@ impl Buffer { Buffer { text: BufferNode::new(), line_ending_type: LineEnding::LF, - undo_stack: Vec::new(), + undo_stack: DList::new(), } } @@ -142,7 +143,7 @@ impl Buffer { pub fn insert_text(&mut self, text: &str, pos: usize) { self._insert_text(text, pos); - self.undo_stack.push(Operation::InsertText(String::from_str(text), pos)); + self.undo_stack.push_back(Operation::InsertText(String::from_str(text), pos)); } fn _insert_text(&mut self, text: &str, pos: usize) { @@ -157,7 +158,7 @@ impl Buffer { self._remove_text(pos_a, pos_b); // Push operation to the undo stack - self.undo_stack.push(Operation::RemoveText(removed_text, pos_a)); + self.undo_stack.push_back(Operation::RemoveText(removed_text, pos_a)); } fn _remove_text(&mut self, pos_a: usize, pos_b: usize) { @@ -191,7 +192,7 @@ impl Buffer { /// Undoes operations that were pushed to the undo stack, and returns a /// cursor position that the cursor should jump to, if any. pub fn undo(&mut self) -> Option { - if let Some(op) = self.undo_stack.pop() { + if let Some(op) = self.undo_stack.pop_back() { match op { Operation::InsertText(ref s, p) => { let size = grapheme_count(s.as_slice()); diff --git a/todo.md b/todo.md index 85be154..8e19c02 100644 --- a/todo.md +++ b/todo.md @@ -3,4 +3,4 @@ - Line number display - File opening by entering path - UI that wraps editors, for split view. -- Undo functionality \ No newline at end of file +- Redo functionality \ No newline at end of file