Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 237 additions & 9 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"

[dependencies]
anyhow = "1.0.102"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
color-eyre = { version = "0.6.5", optional = true }
crossterm = "0.29.0"
nom = "8.0.0"
Expand Down
1 change: 1 addition & 0 deletions src/cmd/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub enum ImmediateOp {
DeleteEol,
RepeatLast,
Replace(char),
Undo,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
2 changes: 2 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod marks;
mod registers;
mod repeat;
mod session;
mod undo;
mod yank;

use std::collections::HashMap;
Expand All @@ -22,6 +23,7 @@ pub use marks::{Change, Marks};
pub use registers::{Register, RegisterData, Registers};
pub use repeat::RepeatBuffer;
pub use session::{BufferName, BufferView, Coords, ExSession, ExState, Mode, Session, Viewport};
pub use undo::{UndoScope, UndoSummary};
pub use yank::YankData;

pub type SessionId = usize;
Expand Down
102 changes: 95 additions & 7 deletions src/components/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::ops::Range;

use ropey::Rope;

use crate::components::{Change, Marks};
use super::undo::UndoStack;
use crate::components::{Change, Marks, UndoSummary, undo::Action};

// Mutable view of a buffer
pub trait MutBuffer {
Expand All @@ -12,10 +13,12 @@ pub trait MutBuffer {
fn insert_rope(&mut self, char_idx: usize, rope: &Rope);
fn insert_iter(&mut self, char_idx: usize, iter: impl IntoIterator<Item = char>);
fn remove(&mut self, char_range: Range<usize>);
fn undo(&mut self) -> Option<UndoSummary>;
}

pub struct Buffer {
marks: Marks,
undo_stack: UndoStack,
dirty: bool,
rope: Rope,
}
Expand All @@ -24,6 +27,7 @@ impl Buffer {
pub fn new(rope: Rope) -> Self {
Self {
marks: Marks::new(),
undo_stack: UndoStack::new(),
dirty: false,
rope,
}
Expand All @@ -32,6 +36,7 @@ impl Buffer {
pub fn empty() -> Self {
Self {
marks: Marks::new(),
undo_stack: UndoStack::new(),
dirty: false,
rope: Rope::new(),
}
Expand Down Expand Up @@ -60,6 +65,10 @@ impl Buffer {
pub fn edit(&mut self) -> impl MutBuffer {
SessionMutableBuffer { buffer: self }
}

pub fn commit_undo_txn(&mut self) {
self.undo_stack.commit();
}
}

// Define how to mutate a Buffer
Expand All @@ -74,6 +83,9 @@ impl<'a> MutBuffer for SessionMutableBuffer<'a> {

fn insert_char(&mut self, char_idx: usize, ch: char) {
self.buffer.rope.insert_char(char_idx, ch);
self.buffer
.undo_stack
.insert_char(self.buffer.dirty, char_idx, ch);
self.buffer.marks.adjust(Change::insert(char_idx, 1));
self.buffer.dirty = true;
}
Expand All @@ -84,6 +96,10 @@ impl<'a> MutBuffer for SessionMutableBuffer<'a> {
}

self.buffer.rope.insert(char_idx, text);
self.buffer
.undo_stack
.insert_str(self.buffer.dirty, char_idx, text);

self.buffer
.marks
.adjust(Change::insert(char_idx, text.chars().count()));
Expand All @@ -92,18 +108,15 @@ impl<'a> MutBuffer for SessionMutableBuffer<'a> {

fn insert_iter(&mut self, char_idx: usize, iter: impl IntoIterator<Item = char>) {
let mut len = 0;

for (i, c) in iter.into_iter().enumerate() {
self.buffer.rope.insert_char(char_idx + i, c);
self.buffer
.undo_stack
.insert_char(self.buffer.dirty, char_idx + i, c);
len += 1;
}

self.buffer.marks.adjust(Change::insert(char_idx, len));
}

fn remove(&mut self, char_range: Range<usize>) {
self.buffer.rope.remove(char_range.clone());
self.buffer.marks.adjust(Change::delete(char_range));
self.buffer.dirty = true;
}

Expand All @@ -115,9 +128,84 @@ impl<'a> MutBuffer for SessionMutableBuffer<'a> {
let tail = self.buffer.rope.split_off(char_idx);
self.buffer.rope.append(rope.clone());
self.buffer.rope.append(tail);

self.buffer
.undo_stack
.insert_rope(self.buffer.dirty, char_idx, rope);

self.buffer
.marks
.adjust(Change::insert(char_idx, rope.len_chars()));
self.buffer.dirty = true;
}

fn remove(&mut self, char_range: Range<usize>) {
if char_range.len() == 1 {
let c = self.buffer.rope.char(char_range.start);
self.buffer
.undo_stack
.remove_char(self.buffer.dirty, char_range.clone(), c);
} else {
let text = self.buffer.rope.slice(char_range.clone()).to_string();
self.buffer
.undo_stack
.remove(self.buffer.dirty, char_range.clone(), &text);
}

self.buffer.rope.remove(char_range.clone());
self.buffer.marks.adjust(Change::delete(char_range));
self.buffer.dirty = true;
}

fn undo(&mut self) -> Option<UndoSummary> {
let buffer = &mut self.buffer;

if let Some(txn) = buffer.undo_stack.pop() {
buffer.dirty = txn.dirty;

let mut summary = UndoSummary::default();
for chunk in txn.data.into_iter().rev() {
let s = mk_summary(
&buffer.rope,
txn.timestamp_ms,
txn.change_id,
chunk.char_idx,
chunk.num_lines,
);
summary.merge(s);

match chunk.action {
Action::Insert => {
let end_idx = chunk.char_idx + chunk.len_chars;
buffer.rope.remove(chunk.char_idx..end_idx);
buffer.marks.adjust(Change::delete(chunk.char_idx..end_idx));
}
Action::Delete => {
buffer.rope.insert(chunk.char_idx, &chunk.text);
buffer
.marks
.adjust(Change::insert(chunk.char_idx, chunk.len_chars));
}
}
}
return Some(summary);
}

None
}
}

fn mk_summary(
rope: &Rope,
timestamp_ms: u64,
change_id: u64,
char_idx: usize,
num_lines: usize,
) -> UndoSummary {
let line_idx = rope.char_to_line(char_idx);
if num_lines > 1 {
UndoSummary::destroy_from(timestamp_ms, change_id, char_idx, line_idx)
} else {
UndoSummary::single_line(timestamp_ms, change_id, char_idx, line_idx)
}
}
14 changes: 9 additions & 5 deletions src/components/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::layout::{Position, Rect};
use ropey::Rope;

use crate::{
components::{BufferId, DisplayBuffer, InsertLog, MutBuffer},
components::{BufferId, DisplayBuffer, InsertLog, MutBuffer, undo::UndoSummary},
misc,
};

Expand Down Expand Up @@ -203,10 +203,6 @@ impl MutBuffer for ExMutableBuffer<'_> {
}
}

fn remove(&mut self, char_range: Range<usize>) {
self.rope.remove(char_range);
}

fn insert_rope(&mut self, char_idx: usize, rope: &Rope) {
if rope.len_chars() == 0 {
return;
Expand All @@ -216,4 +212,12 @@ impl MutBuffer for ExMutableBuffer<'_> {
self.rope.append(rope.clone());
self.rope.append(tail);
}

fn remove(&mut self, char_range: Range<usize>) {
self.rope.remove(char_range);
}

fn undo(&mut self) -> Option<UndoSummary> {
None
}
}
Loading
Loading