Generate operation when updating selection set after undo/redo
This commit is contained in:
parent
59a9f0102f
commit
3a33fab091
2 changed files with 36 additions and 42 deletions
|
@ -22,7 +22,7 @@ use gpui::{AppContext, Entity, ModelContext};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use std::{
|
use std::{
|
||||||
cmp::{self, Ordering},
|
cmp,
|
||||||
hash::BuildHasher,
|
hash::BuildHasher,
|
||||||
iter::{self, Iterator},
|
iter::{self, Iterator},
|
||||||
ops::{AddAssign, Range},
|
ops::{AddAssign, Range},
|
||||||
|
@ -767,10 +767,10 @@ impl Buffer {
|
||||||
|
|
||||||
pub fn add_selection_set(
|
pub fn add_selection_set(
|
||||||
&mut self,
|
&mut self,
|
||||||
selections: Vec<Selection>,
|
selections: impl Into<Arc<[Selection]>>,
|
||||||
ctx: Option<&mut ModelContext<Self>>,
|
ctx: Option<&mut ModelContext<Self>>,
|
||||||
) -> (SelectionSetId, Operation) {
|
) -> (SelectionSetId, Operation) {
|
||||||
let selections = Arc::from(selections);
|
let selections = selections.into();
|
||||||
let lamport_timestamp = self.lamport_clock.tick();
|
let lamport_timestamp = self.lamport_clock.tick();
|
||||||
self.selections
|
self.selections
|
||||||
.insert(lamport_timestamp, Arc::clone(&selections));
|
.insert(lamport_timestamp, Arc::clone(&selections));
|
||||||
|
@ -793,12 +793,11 @@ impl Buffer {
|
||||||
pub fn update_selection_set(
|
pub fn update_selection_set(
|
||||||
&mut self,
|
&mut self,
|
||||||
set_id: SelectionSetId,
|
set_id: SelectionSetId,
|
||||||
mut selections: Vec<Selection>,
|
selections: impl Into<Arc<[Selection]>>,
|
||||||
ctx: Option<&mut ModelContext<Self>>,
|
ctx: Option<&mut ModelContext<Self>>,
|
||||||
) -> Result<Operation> {
|
) -> Result<Operation> {
|
||||||
self.merge_selections(&mut selections);
|
let selections = selections.into();
|
||||||
let selections = Arc::from(selections);
|
self.selections.insert(set_id, selections.clone());
|
||||||
self.selections.insert(set_id, Arc::clone(&selections));
|
|
||||||
|
|
||||||
let lamport_timestamp = self.lamport_clock.tick();
|
let lamport_timestamp = self.lamport_clock.tick();
|
||||||
self.selections_last_update += 1;
|
self.selections_last_update += 1;
|
||||||
|
@ -843,28 +842,6 @@ impl Buffer {
|
||||||
.ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))
|
.ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge_selections(&mut self, selections: &mut Vec<Selection>) {
|
|
||||||
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<I: IntoIterator<Item = Operation>>(
|
pub fn apply_ops<I: IntoIterator<Item = Operation>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
ops: I,
|
ops: I,
|
||||||
|
@ -1063,21 +1040,19 @@ impl Buffer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn undo(&mut self, ctx: Option<&mut ModelContext<Self>>) -> Vec<Operation> {
|
pub fn undo(&mut self, mut ctx: Option<&mut ModelContext<Self>>) -> Vec<Operation> {
|
||||||
let was_dirty = self.is_dirty();
|
let was_dirty = self.is_dirty();
|
||||||
let old_version = self.version.clone();
|
let old_version = self.version.clone();
|
||||||
|
|
||||||
let mut ops = Vec::new();
|
let mut ops = Vec::new();
|
||||||
if let Some(transaction) = self.history.pop_undo() {
|
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() {
|
for edit_id in transaction.edits.clone() {
|
||||||
ops.push(self.undo_or_redo(edit_id).unwrap());
|
ops.push(self.undo_or_redo(edit_id).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((set_id, transaction_selections)) = transaction_selections {
|
if let Some((set_id, selections)) = selections {
|
||||||
if let Some(selections) = self.selections.get_mut(&set_id) {
|
let _ = self.update_selection_set(set_id, selections, ctx.as_deref_mut());
|
||||||
*selections = transaction_selections;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1092,21 +1067,19 @@ impl Buffer {
|
||||||
ops
|
ops
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redo(&mut self, ctx: Option<&mut ModelContext<Self>>) -> Vec<Operation> {
|
pub fn redo(&mut self, mut ctx: Option<&mut ModelContext<Self>>) -> Vec<Operation> {
|
||||||
let was_dirty = self.is_dirty();
|
let was_dirty = self.is_dirty();
|
||||||
let old_version = self.version.clone();
|
let old_version = self.version.clone();
|
||||||
|
|
||||||
let mut ops = Vec::new();
|
let mut ops = Vec::new();
|
||||||
if let Some(transaction) = self.history.pop_redo() {
|
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() {
|
for edit_id in transaction.edits.clone() {
|
||||||
ops.push(self.undo_or_redo(edit_id).unwrap());
|
ops.push(self.undo_or_redo(edit_id).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((set_id, transaction_selections)) = transaction_selections {
|
if let Some((set_id, selections)) = selections {
|
||||||
if let Some(selections) = self.selections.get_mut(&set_id) {
|
let _ = self.update_selection_set(set_id, selections, ctx.as_deref_mut());
|
||||||
*selections = transaction_selections;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -721,7 +721,28 @@ impl BufferView {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_selections(&self, selections: Vec<Selection>, ctx: &mut ViewContext<Self>) {
|
fn update_selections(&self, mut selections: Vec<Selection>, ctx: &mut ViewContext<Self>) {
|
||||||
|
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| {
|
let op = self.buffer.update(ctx, |buffer, ctx| {
|
||||||
buffer
|
buffer
|
||||||
.update_selection_set(self.selection_set_id, selections, Some(ctx))
|
.update_selection_set(self.selection_set_id, selections, Some(ctx))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue