From 3a33fab091cb752959728a8f60bbec849775f8b8 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 12 Apr 2021 17:47:19 +0200 Subject: [PATCH] Generate operation when updating selection set after undo/redo --- zed/src/editor/buffer/mod.rs | 55 +++++++++-------------------------- zed/src/editor/buffer_view.rs | 23 ++++++++++++++- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index c2a6eacad6..b143a4bf33 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -22,7 +22,7 @@ use gpui::{AppContext, Entity, ModelContext}; use lazy_static::lazy_static; use rand::prelude::*; use std::{ - cmp::{self, Ordering}, + cmp, hash::BuildHasher, iter::{self, Iterator}, ops::{AddAssign, Range}, @@ -767,10 +767,10 @@ impl Buffer { pub fn add_selection_set( &mut self, - selections: Vec, + selections: impl Into>, ctx: Option<&mut ModelContext>, ) -> (SelectionSetId, Operation) { - let selections = Arc::from(selections); + let selections = selections.into(); let lamport_timestamp = self.lamport_clock.tick(); self.selections .insert(lamport_timestamp, Arc::clone(&selections)); @@ -793,12 +793,11 @@ impl Buffer { pub fn update_selection_set( &mut self, set_id: SelectionSetId, - mut selections: Vec, + selections: impl Into>, ctx: Option<&mut ModelContext>, ) -> Result { - self.merge_selections(&mut selections); - let selections = Arc::from(selections); - self.selections.insert(set_id, Arc::clone(&selections)); + let selections = selections.into(); + self.selections.insert(set_id, selections.clone()); let lamport_timestamp = self.lamport_clock.tick(); self.selections_last_update += 1; @@ -843,28 +842,6 @@ impl Buffer { .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id)) } - fn merge_selections(&mut self, selections: &mut Vec) { - let mut i = 1; - while i < selections.len() { - if selections[i - 1] - .end - .cmp(&selections[i].start, self) - .unwrap() - >= Ordering::Equal - { - let removed = selections.remove(i); - if removed.start.cmp(&selections[i - 1].start, self).unwrap() < Ordering::Equal { - selections[i - 1].start = removed.start; - } - if removed.end.cmp(&selections[i - 1].end, self).unwrap() > Ordering::Equal { - selections[i - 1].end = removed.end; - } - } else { - i += 1; - } - } - } - pub fn apply_ops>( &mut self, ops: I, @@ -1063,21 +1040,19 @@ impl Buffer { Ok(()) } - pub fn undo(&mut self, ctx: Option<&mut ModelContext>) -> Vec { + pub fn undo(&mut self, mut ctx: Option<&mut ModelContext>) -> Vec { let was_dirty = self.is_dirty(); let old_version = self.version.clone(); let mut ops = Vec::new(); if let Some(transaction) = self.history.pop_undo() { - let transaction_selections = transaction.selections_before.clone(); + let selections = transaction.selections_before.clone(); for edit_id in transaction.edits.clone() { ops.push(self.undo_or_redo(edit_id).unwrap()); } - if let Some((set_id, transaction_selections)) = transaction_selections { - if let Some(selections) = self.selections.get_mut(&set_id) { - *selections = transaction_selections; - } + if let Some((set_id, selections)) = selections { + let _ = self.update_selection_set(set_id, selections, ctx.as_deref_mut()); } } @@ -1092,21 +1067,19 @@ impl Buffer { ops } - pub fn redo(&mut self, ctx: Option<&mut ModelContext>) -> Vec { + pub fn redo(&mut self, mut ctx: Option<&mut ModelContext>) -> Vec { let was_dirty = self.is_dirty(); let old_version = self.version.clone(); let mut ops = Vec::new(); if let Some(transaction) = self.history.pop_redo() { - let transaction_selections = transaction.selections_after.clone(); + let selections = transaction.selections_after.clone(); for edit_id in transaction.edits.clone() { ops.push(self.undo_or_redo(edit_id).unwrap()); } - if let Some((set_id, transaction_selections)) = transaction_selections { - if let Some(selections) = self.selections.get_mut(&set_id) { - *selections = transaction_selections; - } + if let Some((set_id, selections)) = selections { + let _ = self.update_selection_set(set_id, selections, ctx.as_deref_mut()); } } diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index a4d6f15b2c..22cd3f0c9f 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -721,7 +721,28 @@ impl BufferView { .unwrap() } - fn update_selections(&self, selections: Vec, ctx: &mut ViewContext) { + fn update_selections(&self, mut selections: Vec, ctx: &mut ViewContext) { + let buffer = self.buffer.as_ref(ctx); + let mut i = 1; + while i < selections.len() { + if selections[i - 1] + .end + .cmp(&selections[i].start, buffer) + .unwrap() + >= Ordering::Equal + { + let removed = selections.remove(i); + if removed.start.cmp(&selections[i - 1].start, buffer).unwrap() < Ordering::Equal { + selections[i - 1].start = removed.start; + } + if removed.end.cmp(&selections[i - 1].end, buffer).unwrap() > Ordering::Equal { + selections[i - 1].end = removed.end; + } + } else { + i += 1; + } + } + let op = self.buffer.update(ctx, |buffer, ctx| { buffer .update_selection_set(self.selection_set_id, selections, Some(ctx))