Change Buffer constructors to construct the History internally

This commit is contained in:
Max Brunsfeld 2022-07-05 14:51:28 -07:00
parent 1add2d270e
commit 116fa92e84
3 changed files with 30 additions and 33 deletions

View File

@ -314,30 +314,30 @@ impl CharKind {
}
impl Buffer {
pub fn new<T: Into<Arc<str>>>(
pub fn new<T: Into<String>>(
replica_id: ReplicaId,
base_text: T,
cx: &mut ModelContext<Self>,
) -> Self {
let history = History::new(base_text.into());
let line_ending = LineEnding::detect(&history.base_text);
let base_text = base_text.into();
let line_ending = LineEnding::detect(&base_text);
Self::build(
TextBuffer::new(replica_id, cx.model_id() as u64, history),
TextBuffer::new(replica_id, cx.model_id() as u64, base_text),
None,
line_ending,
)
}
pub fn from_file<T: Into<Arc<str>>>(
pub fn from_file<T: Into<String>>(
replica_id: ReplicaId,
base_text: T,
file: Arc<dyn File>,
cx: &mut ModelContext<Self>,
) -> Self {
let history = History::new(base_text.into());
let line_ending = LineEnding::detect(&history.base_text);
let base_text = base_text.into();
let line_ending = LineEnding::detect(&base_text);
Self::build(
TextBuffer::new(replica_id, cx.model_id() as u64, history),
TextBuffer::new(replica_id, cx.model_id() as u64, base_text),
Some(file),
line_ending,
)
@ -349,11 +349,7 @@ impl Buffer {
file: Option<Arc<dyn File>>,
cx: &mut ModelContext<Self>,
) -> Result<Self> {
let buffer = TextBuffer::new(
replica_id,
message.id,
History::new(Arc::from(message.base_text)),
);
let buffer = TextBuffer::new(replica_id, message.id, message.base_text);
let line_ending = proto::LineEnding::from_i32(message.line_ending)
.ok_or_else(|| anyhow!("missing line_ending"))?;
let mut this = Self::build(buffer, file, LineEnding::from_proto(line_ending));

View File

@ -18,7 +18,7 @@ fn init_logger() {
#[test]
fn test_edit() {
let mut buffer = Buffer::new(0, 0, History::new("abc".into()));
let mut buffer = Buffer::new(0, 0, "abc".into());
assert_eq!(buffer.text(), "abc");
buffer.edit([(3..3, "def")]);
assert_eq!(buffer.text(), "abcdef");
@ -42,7 +42,7 @@ fn test_random_edits(mut rng: StdRng) {
let mut reference_string = RandomCharIter::new(&mut rng)
.take(reference_string_len)
.collect::<String>();
let mut buffer = Buffer::new(0, 0, History::new(reference_string.clone().into()));
let mut buffer = Buffer::new(0, 0, reference_string.clone().into());
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
let mut buffer_versions = Vec::new();
log::info!(
@ -150,7 +150,7 @@ fn test_random_edits(mut rng: StdRng) {
#[test]
fn test_line_len() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
let mut buffer = Buffer::new(0, 0, "".into());
buffer.edit([(0..0, "abcd\nefg\nhij")]);
buffer.edit([(12..12, "kl\nmno")]);
buffer.edit([(18..18, "\npqrs\n")]);
@ -167,7 +167,7 @@ fn test_line_len() {
#[test]
fn test_common_prefix_at_positionn() {
let text = "a = str; b = δα";
let buffer = Buffer::new(0, 0, History::new(text.into()));
let buffer = Buffer::new(0, 0, text.into());
let offset1 = offset_after(text, "str");
let offset2 = offset_after(text, "δα");
@ -215,7 +215,7 @@ fn test_common_prefix_at_positionn() {
#[test]
fn test_text_summary_for_range() {
let buffer = Buffer::new(0, 0, History::new("ab\nefg\nhklm\nnopqrs\ntuvwxyz".into()));
let buffer = Buffer::new(0, 0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz".into());
assert_eq!(
buffer.text_summary_for_range::<TextSummary, _>(1..3),
TextSummary {
@ -280,7 +280,7 @@ fn test_text_summary_for_range() {
#[test]
fn test_chars_at() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
let mut buffer = Buffer::new(0, 0, "".into());
buffer.edit([(0..0, "abcd\nefgh\nij")]);
buffer.edit([(12..12, "kl\nmno")]);
buffer.edit([(18..18, "\npqrs")]);
@ -302,7 +302,7 @@ fn test_chars_at() {
assert_eq!(chars.collect::<String>(), "PQrs");
// Regression test:
let mut buffer = Buffer::new(0, 0, History::new("".into()));
let mut buffer = Buffer::new(0, 0, "".into());
buffer.edit([(0..0, "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n")]);
buffer.edit([(60..60, "\n")]);
@ -312,7 +312,7 @@ fn test_chars_at() {
#[test]
fn test_anchors() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
let mut buffer = Buffer::new(0, 0, "".into());
buffer.edit([(0..0, "abc")]);
let left_anchor = buffer.anchor_before(2);
let right_anchor = buffer.anchor_after(2);
@ -430,7 +430,7 @@ fn test_anchors() {
#[test]
fn test_anchors_at_start_and_end() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
let mut buffer = Buffer::new(0, 0, "".into());
let before_start_anchor = buffer.anchor_before(0);
let after_end_anchor = buffer.anchor_after(0);
@ -453,7 +453,7 @@ fn test_anchors_at_start_and_end() {
#[test]
fn test_undo_redo() {
let mut buffer = Buffer::new(0, 0, History::new("1234".into()));
let mut buffer = Buffer::new(0, 0, "1234".into());
// Set group interval to zero so as to not group edits in the undo stack.
buffer.history.group_interval = Duration::from_secs(0);
@ -490,7 +490,7 @@ fn test_undo_redo() {
#[test]
fn test_history() {
let mut now = Instant::now();
let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
let mut buffer = Buffer::new(0, 0, "123456".into());
buffer.start_transaction_at(now);
buffer.edit([(2..4, "cd")]);
@ -544,7 +544,7 @@ fn test_history() {
#[test]
fn test_finalize_last_transaction() {
let now = Instant::now();
let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
let mut buffer = Buffer::new(0, 0, "123456".into());
buffer.start_transaction_at(now);
buffer.edit([(2..4, "cd")]);
@ -579,7 +579,7 @@ fn test_finalize_last_transaction() {
#[test]
fn test_edited_ranges_for_transaction() {
let now = Instant::now();
let mut buffer = Buffer::new(0, 0, History::new("1234567".into()));
let mut buffer = Buffer::new(0, 0, "1234567".into());
buffer.start_transaction_at(now);
buffer.edit([(2..4, "cd")]);
@ -618,9 +618,9 @@ fn test_edited_ranges_for_transaction() {
fn test_concurrent_edits() {
let text = "abcdef";
let mut buffer1 = Buffer::new(1, 0, History::new(text.into()));
let mut buffer2 = Buffer::new(2, 0, History::new(text.into()));
let mut buffer3 = Buffer::new(3, 0, History::new(text.into()));
let mut buffer1 = Buffer::new(1, 0, text.into());
let mut buffer2 = Buffer::new(2, 0, text.into());
let mut buffer3 = Buffer::new(3, 0, text.into());
let buf1_op = buffer1.edit([(1..2, "12")]);
assert_eq!(buffer1.text(), "a12cdef");
@ -659,7 +659,7 @@ fn test_random_concurrent_edits(mut rng: StdRng) {
let mut network = Network::new(rng.clone());
for i in 0..peers {
let mut buffer = Buffer::new(i as ReplicaId, 0, History::new(base_text.clone().into()));
let mut buffer = Buffer::new(i as ReplicaId, 0, base_text.clone().into());
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
buffers.push(buffer);
replica_ids.push(i as u16);

View File

@ -148,9 +148,9 @@ impl HistoryEntry {
}
#[derive(Clone)]
pub struct History {
struct History {
// TODO: Turn this into a String or Rope, maybe.
pub base_text: Arc<str>,
base_text: Arc<str>,
operations: HashMap<clock::Local, Operation>,
undo_stack: Vec<HistoryEntry>,
redo_stack: Vec<HistoryEntry>,
@ -539,7 +539,8 @@ pub struct UndoOperation {
}
impl Buffer {
pub fn new(replica_id: u16, remote_id: u64, history: History) -> Buffer {
pub fn new(replica_id: u16, remote_id: u64, base_text: String) -> Buffer {
let history = History::new(base_text.into());
let mut fragments = SumTree::new();
let mut insertions = SumTree::new();