Skip to content

Commit

Permalink
Tried backpatching the cursor to avoid jumping around
Browse files Browse the repository at this point in the history
  • Loading branch information
curlpipe committed Oct 23, 2024
1 parent d3597d1 commit c41259a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kaolinite/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,17 +1297,21 @@ impl Document {
self.file.slice(self.selection_range()).to_string()
}

/// Commit a change to the undo management system
pub fn commit(&mut self) {
let s = self.take_snapshot();
self.undo_mgmt.backpatch_cursor(&self.cursor);
self.undo_mgmt.commit(s);
}

/// Completely reload the file
pub fn reload_lines(&mut self) {
let to = std::mem::take(&mut self.info.loaded_to);
self.lines.clear();
self.load_to(to);
}

/// Delete the currently selected text
pub fn remove_selection(&mut self) {
self.file.remove(self.selection_range());
self.reload_lines();
Expand Down
7 changes: 7 additions & 0 deletions kaolinite/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,11 @@ impl UndoMgmt {
pub fn at_file(&self) -> bool {
self.undo.len() == self.on_disk
}

/// Change the cursor position of the previous snapshot
pub fn backpatch_cursor(&mut self, cursor: &Cursor) {
if let Some(snapshot) = self.undo.last_mut() {
snapshot.cursor = *cursor;
}
}
}

0 comments on commit c41259a

Please sign in to comment.