Merge pull request #213 from zed-industries/language-buffer

Extract Buffer's language-aware behavior into a new `language` crate
This commit is contained in:
Max Brunsfeld 2021-10-21 13:22:04 +02:00 committed by GitHub
commit 559774d6ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 2790 additions and 2672 deletions

37
Cargo.lock generated
View file

@ -750,21 +750,12 @@ dependencies = [
"arrayvec 0.7.1",
"clock",
"gpui",
"lazy_static",
"log",
"parking_lot",
"rand 0.8.3",
"rpc",
"seahash",
"serde 1.0.125",
"similar",
"smallvec",
"smol",
"sum_tree",
"theme",
"tree-sitter",
"tree-sitter-rust",
"unindent",
]
[[package]]
@ -1624,6 +1615,7 @@ dependencies = [
"buffer",
"clock",
"gpui",
"language",
"lazy_static",
"log",
"parking_lot",
@ -2816,6 +2808,30 @@ dependencies = [
"log",
]
[[package]]
name = "language"
version = "0.1.0"
dependencies = [
"anyhow",
"buffer",
"clock",
"futures",
"gpui",
"lazy_static",
"log",
"parking_lot",
"rand 0.8.3",
"rpc",
"serde 1.0.125",
"similar",
"smol",
"theme",
"tree-sitter",
"tree-sitter-rust",
"unindent",
"util",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -3760,6 +3776,7 @@ dependencies = [
"fuzzy",
"gpui",
"ignore",
"language",
"lazy_static",
"libc",
"log",
@ -6106,6 +6123,7 @@ dependencies = [
"client",
"editor",
"gpui",
"language",
"log",
"postage",
"project",
@ -6176,6 +6194,7 @@ dependencies = [
"ignore",
"image 0.23.14",
"indexmap",
"language",
"lazy_static",
"libc",
"log",

View file

@ -4,30 +4,20 @@ version = "0.1.0"
edition = "2018"
[features]
test-support = ["rand"]
test-support = ["rand", "seahash"]
[dependencies]
clock = { path = "../clock" }
gpui = { path = "../gpui" }
rpc = { path = "../rpc" }
sum_tree = { path = "../sum_tree" }
theme = { path = "../theme" }
anyhow = "1.0.38"
arrayvec = "0.7.1"
lazy_static = "1.4"
log = "0.4"
parking_lot = "0.11.1"
rand = { version = "0.8.3", optional = true }
seahash = "4.1"
serde = { version = "1", features = ["derive"] }
similar = "1.3"
seahash = { version = "4.1", optional = true }
smallvec = { version = "1.6", features = ["union"] }
smol = "1.2"
tree-sitter = "0.19.5"
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }
seahash = "4.1"
rand = "0.8.3"
tree-sitter-rust = "0.19.0"
unindent = "0.1.7"

File diff suppressed because it is too large Load diff

View file

@ -109,21 +109,3 @@ impl Ord for Point {
}
}
}
impl Into<tree_sitter::Point> for Point {
fn into(self) -> tree_sitter::Point {
tree_sitter::Point {
row: self.row as usize,
column: self.column as usize,
}
}
}
impl From<tree_sitter::Point> for Point {
fn from(point: tree_sitter::Point) -> Self {
Self {
row: point.row as u32,
column: point.column as u32,
}
}
}

View file

@ -1,2 +1,658 @@
mod buffer;
mod syntax;
use super::*;
use clock::ReplicaId;
use rand::prelude::*;
use std::{
cmp::Ordering,
env,
iter::Iterator,
time::{Duration, Instant},
};
#[test]
fn test_edit() {
let mut buffer = Buffer::new(0, 0, History::new("abc".into()));
assert_eq!(buffer.text(), "abc");
buffer.edit(vec![3..3], "def");
assert_eq!(buffer.text(), "abcdef");
buffer.edit(vec![0..0], "ghi");
assert_eq!(buffer.text(), "ghiabcdef");
buffer.edit(vec![5..5], "jkl");
assert_eq!(buffer.text(), "ghiabjklcdef");
buffer.edit(vec![6..7], "");
assert_eq!(buffer.text(), "ghiabjlcdef");
buffer.edit(vec![4..9], "mno");
assert_eq!(buffer.text(), "ghiamnoef");
}
#[gpui::test(iterations = 100)]
fn test_random_edits(mut rng: StdRng) {
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let reference_string_len = rng.gen_range(0..3);
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()));
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
let mut buffer_versions = Vec::new();
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
for _i in 0..operations {
let (old_ranges, new_text, _) = buffer.randomly_edit(&mut rng, 5);
for old_range in old_ranges.iter().rev() {
reference_string.replace_range(old_range.clone(), &new_text);
}
assert_eq!(buffer.text(), reference_string);
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
if rng.gen_bool(0.25) {
buffer.randomly_undo_redo(&mut rng);
reference_string = buffer.text();
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
}
let range = buffer.random_byte_range(0, &mut rng);
assert_eq!(
buffer.text_summary_for_range(range.clone()),
TextSummary::from(&reference_string[range])
);
if rng.gen_bool(0.3) {
buffer_versions.push(buffer.clone());
}
}
for mut old_buffer in buffer_versions {
let edits = buffer
.edits_since(old_buffer.version.clone())
.collect::<Vec<_>>();
log::info!(
"mutating old buffer version {:?}, text: {:?}, edits since: {:?}",
old_buffer.version(),
old_buffer.text(),
edits,
);
let mut delta = 0_isize;
for edit in edits {
let old_start = (edit.old_bytes.start as isize + delta) as usize;
let new_text: String = buffer.text_for_range(edit.new_bytes.clone()).collect();
old_buffer.edit(Some(old_start..old_start + edit.deleted_bytes()), new_text);
delta += edit.delta();
}
assert_eq!(old_buffer.text(), buffer.text());
}
}
#[test]
fn test_line_len() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(vec![0..0], "abcd\nefg\nhij");
buffer.edit(vec![12..12], "kl\nmno");
buffer.edit(vec![18..18], "\npqrs\n");
buffer.edit(vec![18..21], "\nPQ");
assert_eq!(buffer.line_len(0), 4);
assert_eq!(buffer.line_len(1), 3);
assert_eq!(buffer.line_len(2), 5);
assert_eq!(buffer.line_len(3), 3);
assert_eq!(buffer.line_len(4), 4);
assert_eq!(buffer.line_len(5), 0);
}
#[test]
fn test_text_summary_for_range() {
let buffer = Buffer::new(0, 0, History::new("ab\nefg\nhklm\nnopqrs\ntuvwxyz".into()));
assert_eq!(
buffer.text_summary_for_range(1..3),
TextSummary {
bytes: 2,
lines: Point::new(1, 0),
first_line_chars: 1,
last_line_chars: 0,
longest_row: 0,
longest_row_chars: 1,
}
);
assert_eq!(
buffer.text_summary_for_range(1..12),
TextSummary {
bytes: 11,
lines: Point::new(3, 0),
first_line_chars: 1,
last_line_chars: 0,
longest_row: 2,
longest_row_chars: 4,
}
);
assert_eq!(
buffer.text_summary_for_range(0..20),
TextSummary {
bytes: 20,
lines: Point::new(4, 1),
first_line_chars: 2,
last_line_chars: 1,
longest_row: 3,
longest_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(0..22),
TextSummary {
bytes: 22,
lines: Point::new(4, 3),
first_line_chars: 2,
last_line_chars: 3,
longest_row: 3,
longest_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(7..22),
TextSummary {
bytes: 15,
lines: Point::new(2, 3),
first_line_chars: 4,
last_line_chars: 3,
longest_row: 1,
longest_row_chars: 6,
}
);
}
#[test]
fn test_chars_at() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(vec![0..0], "abcd\nefgh\nij");
buffer.edit(vec![12..12], "kl\nmno");
buffer.edit(vec![18..18], "\npqrs");
buffer.edit(vec![18..21], "\nPQ");
let chars = buffer.chars_at(Point::new(0, 0));
assert_eq!(chars.collect::<String>(), "abcd\nefgh\nijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(1, 0));
assert_eq!(chars.collect::<String>(), "efgh\nijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(2, 0));
assert_eq!(chars.collect::<String>(), "ijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(3, 0));
assert_eq!(chars.collect::<String>(), "mno\nPQrs");
let chars = buffer.chars_at(Point::new(4, 0));
assert_eq!(chars.collect::<String>(), "PQrs");
// Regression test:
let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(vec![0..0], "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n");
buffer.edit(vec![60..60], "\n");
let chars = buffer.chars_at(Point::new(6, 0));
assert_eq!(chars.collect::<String>(), " \"xray_wasm\",\n]\n");
}
#[test]
fn test_anchors() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
buffer.edit(vec![0..0], "abc");
let left_anchor = buffer.anchor_before(2);
let right_anchor = buffer.anchor_after(2);
buffer.edit(vec![1..1], "def\n");
assert_eq!(buffer.text(), "adef\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 6);
assert_eq!(right_anchor.to_offset(&buffer), 6);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(vec![2..3], "");
assert_eq!(buffer.text(), "adf\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 5);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(vec![5..5], "ghi\n");
assert_eq!(buffer.text(), "adf\nbghi\nc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 9);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 });
buffer.edit(vec![7..9], "");
assert_eq!(buffer.text(), "adf\nbghc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 7);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 },);
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 3 });
// Ensure anchoring to a point is equivalent to anchoring to an offset.
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 0 }),
buffer.anchor_before(0)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 1 }),
buffer.anchor_before(1)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 2 }),
buffer.anchor_before(2)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 3 }),
buffer.anchor_before(3)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 0 }),
buffer.anchor_before(4)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 1 }),
buffer.anchor_before(5)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 2 }),
buffer.anchor_before(6)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 3 }),
buffer.anchor_before(7)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 4 }),
buffer.anchor_before(8)
);
// Comparison between anchors.
let anchor_at_offset_0 = buffer.anchor_before(0);
let anchor_at_offset_1 = buffer.anchor_before(1);
let anchor_at_offset_2 = buffer.anchor_before(2);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater
);
}
#[test]
fn test_anchors_at_start_and_end() {
let mut buffer = Buffer::new(0, 0, History::new("".into()));
let before_start_anchor = buffer.anchor_before(0);
let after_end_anchor = buffer.anchor_after(0);
buffer.edit(vec![0..0], "abc");
assert_eq!(buffer.text(), "abc");
assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_end_anchor.to_offset(&buffer), 3);
let after_start_anchor = buffer.anchor_after(0);
let before_end_anchor = buffer.anchor_before(3);
buffer.edit(vec![3..3], "def");
buffer.edit(vec![0..0], "ghi");
assert_eq!(buffer.text(), "ghiabcdef");
assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_start_anchor.to_offset(&buffer), 3);
assert_eq!(before_end_anchor.to_offset(&buffer), 6);
assert_eq!(after_end_anchor.to_offset(&buffer), 9);
}
#[test]
fn test_undo_redo() {
let mut buffer = Buffer::new(0, 0, History::new("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);
buffer.edit(vec![1..1], "abx");
buffer.edit(vec![3..4], "yzef");
buffer.edit(vec![3..5], "cd");
assert_eq!(buffer.text(), "1abcdef234");
let transactions = buffer.history.undo_stack.clone();
assert_eq!(transactions.len(), 3);
buffer.undo_or_redo(transactions[0].clone()).unwrap();
assert_eq!(buffer.text(), "1cdef234");
buffer.undo_or_redo(transactions[0].clone()).unwrap();
assert_eq!(buffer.text(), "1abcdef234");
buffer.undo_or_redo(transactions[1].clone()).unwrap();
assert_eq!(buffer.text(), "1abcdx234");
buffer.undo_or_redo(transactions[2].clone()).unwrap();
assert_eq!(buffer.text(), "1abx234");
buffer.undo_or_redo(transactions[1].clone()).unwrap();
assert_eq!(buffer.text(), "1abyzef234");
buffer.undo_or_redo(transactions[2].clone()).unwrap();
assert_eq!(buffer.text(), "1abcdef234");
buffer.undo_or_redo(transactions[2].clone()).unwrap();
assert_eq!(buffer.text(), "1abyzef234");
buffer.undo_or_redo(transactions[0].clone()).unwrap();
assert_eq!(buffer.text(), "1yzef234");
buffer.undo_or_redo(transactions[1].clone()).unwrap();
assert_eq!(buffer.text(), "1234");
}
#[test]
fn test_history() {
let mut now = Instant::now();
let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
let set_id = if let Operation::UpdateSelections { set_id, .. } =
buffer.add_selection_set(buffer.selections_from_ranges(vec![4..4]).unwrap())
{
set_id
} else {
unreachable!()
};
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer.edit(vec![2..4], "cd");
buffer.end_transaction_at(Some(set_id), now).unwrap();
assert_eq!(buffer.text(), "12cd56");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer
.update_selection_set(set_id, buffer.selections_from_ranges(vec![1..3]).unwrap())
.unwrap();
buffer.edit(vec![4..5], "e");
buffer.end_transaction_at(Some(set_id), now).unwrap();
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
now += buffer.history.group_interval + Duration::from_millis(1);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer
.update_selection_set(set_id, buffer.selections_from_ranges(vec![2..2]).unwrap())
.unwrap();
buffer.edit(vec![0..1], "a");
buffer.edit(vec![1..1], "b");
buffer.end_transaction_at(Some(set_id), now).unwrap();
assert_eq!(buffer.text(), "ab2cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
// Last transaction happened past the group interval, undo it on its
// own.
buffer.undo();
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
// First two transactions happened within the group interval, undo them
// together.
buffer.undo();
assert_eq!(buffer.text(), "123456");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
// Redo the first two transactions together.
buffer.redo();
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
// Redo the last transaction on its own.
buffer.redo();
assert_eq!(buffer.text(), "ab2cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
buffer.start_transaction_at(None, now).unwrap();
assert!(buffer.end_transaction_at(None, now).is_none());
buffer.undo();
assert_eq!(buffer.text(), "12cde6");
}
#[test]
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 buf1_op = buffer1.edit(vec![1..2], "12");
assert_eq!(buffer1.text(), "a12cdef");
let buf2_op = buffer2.edit(vec![3..4], "34");
assert_eq!(buffer2.text(), "abc34ef");
let buf3_op = buffer3.edit(vec![5..6], "56");
assert_eq!(buffer3.text(), "abcde56");
buffer1.apply_op(Operation::Edit(buf2_op.clone())).unwrap();
buffer1.apply_op(Operation::Edit(buf3_op.clone())).unwrap();
buffer2.apply_op(Operation::Edit(buf1_op.clone())).unwrap();
buffer2.apply_op(Operation::Edit(buf3_op.clone())).unwrap();
buffer3.apply_op(Operation::Edit(buf1_op.clone())).unwrap();
buffer3.apply_op(Operation::Edit(buf2_op.clone())).unwrap();
assert_eq!(buffer1.text(), "a12c34e56");
assert_eq!(buffer2.text(), "a12c34e56");
assert_eq!(buffer3.text(), "a12c34e56");
}
#[gpui::test(iterations = 100)]
fn test_random_concurrent_edits(mut rng: StdRng) {
let peers = env::var("PEERS")
.map(|i| i.parse().expect("invalid `PEERS` variable"))
.unwrap_or(5);
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let base_text_len = rng.gen_range(0..10);
let base_text = RandomCharIter::new(&mut rng)
.take(base_text_len)
.collect::<String>();
let mut replica_ids = Vec::new();
let mut buffers = Vec::new();
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()));
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
buffers.push(buffer);
replica_ids.push(i as u16);
network.add_peer(i as u16);
}
log::info!("initial text: {:?}", base_text);
let mut mutation_count = operations;
loop {
let replica_index = rng.gen_range(0..peers);
let replica_id = replica_ids[replica_index];
let buffer = &mut buffers[replica_index];
match rng.gen_range(0..=100) {
0..=50 if mutation_count != 0 => {
let ops = buffer.randomly_mutate(&mut rng);
network.broadcast(buffer.replica_id, ops);
log::info!("buffer {} text: {:?}", buffer.replica_id, buffer.text());
mutation_count -= 1;
}
51..=70 if mutation_count != 0 => {
let ops = buffer.randomly_undo_redo(&mut rng);
network.broadcast(buffer.replica_id, ops);
mutation_count -= 1;
}
71..=100 if network.has_unreceived(replica_id) => {
let ops = network.receive(replica_id);
if !ops.is_empty() {
log::info!(
"peer {} applying {} ops from the network.",
replica_id,
ops.len()
);
buffer.apply_ops(ops).unwrap();
}
}
_ => {}
}
if mutation_count == 0 && network.is_idle() {
break;
}
}
let first_buffer = &buffers[0];
for buffer in &buffers[1..] {
assert_eq!(
buffer.text(),
first_buffer.text(),
"Replica {} text != Replica 0 text",
buffer.replica_id
);
assert_eq!(
buffer.selection_sets().collect::<HashMap<_, _>>(),
first_buffer.selection_sets().collect::<HashMap<_, _>>()
);
assert_eq!(
buffer.all_selection_ranges().collect::<HashMap<_, _>>(),
first_buffer
.all_selection_ranges()
.collect::<HashMap<_, _>>()
);
}
}
#[derive(Clone)]
struct Envelope<T: Clone> {
message: T,
sender: ReplicaId,
}
struct Network<T: Clone, R: rand::Rng> {
inboxes: std::collections::BTreeMap<ReplicaId, Vec<Envelope<T>>>,
all_messages: Vec<T>,
rng: R,
}
impl<T: Clone, R: rand::Rng> Network<T, R> {
fn new(rng: R) -> Self {
Network {
inboxes: Default::default(),
all_messages: Vec::new(),
rng,
}
}
fn add_peer(&mut self, id: ReplicaId) {
self.inboxes.insert(id, Vec::new());
}
fn is_idle(&self) -> bool {
self.inboxes.values().all(|i| i.is_empty())
}
fn broadcast(&mut self, sender: ReplicaId, messages: Vec<T>) {
for (replica, inbox) in self.inboxes.iter_mut() {
if *replica != sender {
for message in &messages {
let min_index = inbox
.iter()
.enumerate()
.rev()
.find_map(|(index, envelope)| {
if sender == envelope.sender {
Some(index + 1)
} else {
None
}
})
.unwrap_or(0);
// Insert one or more duplicates of this message *after* the previous
// message delivered by this replica.
for _ in 0..self.rng.gen_range(1..4) {
let insertion_index = self.rng.gen_range(min_index..inbox.len() + 1);
inbox.insert(
insertion_index,
Envelope {
message: message.clone(),
sender,
},
);
}
}
}
}
self.all_messages.extend(messages);
}
fn has_unreceived(&self, receiver: ReplicaId) -> bool {
!self.inboxes[&receiver].is_empty()
}
fn receive(&mut self, receiver: ReplicaId) -> Vec<T> {
let inbox = self.inboxes.get_mut(&receiver).unwrap();
let count = self.rng.gen_range(0..inbox.len() + 1);
inbox
.drain(0..count)
.map(|envelope| envelope.message)
.collect()
}
}

View file

@ -1,790 +0,0 @@
use crate::*;
use clock::ReplicaId;
use rand::prelude::*;
use std::{
cell::RefCell,
cmp::Ordering,
env,
iter::Iterator,
mem,
rc::Rc,
time::{Duration, Instant},
};
#[gpui::test]
fn test_edit(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "abc", cx);
assert_eq!(buffer.text(), "abc");
buffer.edit(vec![3..3], "def", cx);
assert_eq!(buffer.text(), "abcdef");
buffer.edit(vec![0..0], "ghi", cx);
assert_eq!(buffer.text(), "ghiabcdef");
buffer.edit(vec![5..5], "jkl", cx);
assert_eq!(buffer.text(), "ghiabjklcdef");
buffer.edit(vec![6..7], "", cx);
assert_eq!(buffer.text(), "ghiabjlcdef");
buffer.edit(vec![4..9], "mno", cx);
assert_eq!(buffer.text(), "ghiamnoef");
buffer
});
}
#[gpui::test]
fn test_edit_events(cx: &mut gpui::MutableAppContext) {
let mut now = Instant::now();
let buffer_1_events = Rc::new(RefCell::new(Vec::new()));
let buffer_2_events = Rc::new(RefCell::new(Vec::new()));
let buffer1 = cx.add_model(|cx| Buffer::new(0, "abcdef", cx));
let buffer2 = cx.add_model(|cx| Buffer::new(1, "abcdef", cx));
let buffer_ops = buffer1.update(cx, |buffer, cx| {
let buffer_1_events = buffer_1_events.clone();
cx.subscribe(&buffer1, move |_, _, event, _| {
buffer_1_events.borrow_mut().push(event.clone())
})
.detach();
let buffer_2_events = buffer_2_events.clone();
cx.subscribe(&buffer2, move |_, _, event, _| {
buffer_2_events.borrow_mut().push(event.clone())
})
.detach();
// An edit emits an edited event, followed by a dirtied event,
// since the buffer was previously in a clean state.
buffer.edit(Some(2..4), "XYZ", cx);
// An empty transaction does not emit any events.
buffer.start_transaction(None).unwrap();
buffer.end_transaction(None, cx).unwrap();
// A transaction containing two edits emits one edited event.
now += Duration::from_secs(1);
buffer.start_transaction_at(None, now).unwrap();
buffer.edit(Some(5..5), "u", cx);
buffer.edit(Some(6..6), "w", cx);
buffer.end_transaction_at(None, now, cx).unwrap();
// Undoing a transaction emits one edited event.
buffer.undo(cx);
buffer.operations.clone()
});
// Incorporating a set of remote ops emits a single edited event,
// followed by a dirtied event.
buffer2.update(cx, |buffer, cx| {
buffer.apply_ops(buffer_ops, cx).unwrap();
});
let buffer_1_events = buffer_1_events.borrow();
assert_eq!(
*buffer_1_events,
vec![Event::Edited, Event::Dirtied, Event::Edited, Event::Edited]
);
let buffer_2_events = buffer_2_events.borrow();
assert_eq!(*buffer_2_events, vec![Event::Edited, Event::Dirtied]);
}
#[gpui::test(iterations = 100)]
fn test_random_edits(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let reference_string_len = rng.gen_range(0..3);
let mut reference_string = RandomCharIter::new(&mut rng)
.take(reference_string_len)
.collect::<String>();
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, reference_string.as_str(), cx);
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
let mut buffer_versions = Vec::new();
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
for _i in 0..operations {
let (old_ranges, new_text) = buffer.randomly_mutate(&mut rng, cx);
for old_range in old_ranges.iter().rev() {
reference_string.replace_range(old_range.clone(), &new_text);
}
assert_eq!(buffer.text(), reference_string);
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
if rng.gen_bool(0.25) {
buffer.randomly_undo_redo(&mut rng, cx);
reference_string = buffer.text();
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
}
let range = buffer.random_byte_range(0, &mut rng);
assert_eq!(
buffer.text_summary_for_range(range.clone()),
TextSummary::from(&reference_string[range])
);
if rng.gen_bool(0.3) {
buffer_versions.push(buffer.clone());
}
}
for mut old_buffer in buffer_versions {
let edits = buffer
.edits_since(old_buffer.version.clone())
.collect::<Vec<_>>();
log::info!(
"mutating old buffer version {:?}, text: {:?}, edits since: {:?}",
old_buffer.version(),
old_buffer.text(),
edits,
);
let mut delta = 0_isize;
for edit in edits {
let old_start = (edit.old_bytes.start as isize + delta) as usize;
let new_text: String = buffer.text_for_range(edit.new_bytes.clone()).collect();
old_buffer.edit(
Some(old_start..old_start + edit.deleted_bytes()),
new_text,
cx,
);
delta += edit.delta();
}
assert_eq!(old_buffer.text(), buffer.text());
}
buffer
});
}
#[gpui::test]
fn test_line_len(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "abcd\nefg\nhij", cx);
buffer.edit(vec![12..12], "kl\nmno", cx);
buffer.edit(vec![18..18], "\npqrs\n", cx);
buffer.edit(vec![18..21], "\nPQ", cx);
assert_eq!(buffer.line_len(0), 4);
assert_eq!(buffer.line_len(1), 3);
assert_eq!(buffer.line_len(2), 5);
assert_eq!(buffer.line_len(3), 3);
assert_eq!(buffer.line_len(4), 4);
assert_eq!(buffer.line_len(5), 0);
buffer
});
}
#[gpui::test]
fn test_text_summary_for_range(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz", cx);
assert_eq!(
buffer.text_summary_for_range(1..3),
TextSummary {
bytes: 2,
lines: Point::new(1, 0),
first_line_chars: 1,
last_line_chars: 0,
longest_row: 0,
longest_row_chars: 1,
}
);
assert_eq!(
buffer.text_summary_for_range(1..12),
TextSummary {
bytes: 11,
lines: Point::new(3, 0),
first_line_chars: 1,
last_line_chars: 0,
longest_row: 2,
longest_row_chars: 4,
}
);
assert_eq!(
buffer.text_summary_for_range(0..20),
TextSummary {
bytes: 20,
lines: Point::new(4, 1),
first_line_chars: 2,
last_line_chars: 1,
longest_row: 3,
longest_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(0..22),
TextSummary {
bytes: 22,
lines: Point::new(4, 3),
first_line_chars: 2,
last_line_chars: 3,
longest_row: 3,
longest_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(7..22),
TextSummary {
bytes: 15,
lines: Point::new(2, 3),
first_line_chars: 4,
last_line_chars: 3,
longest_row: 1,
longest_row_chars: 6,
}
);
buffer
});
}
#[gpui::test]
fn test_chars_at(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "abcd\nefgh\nij", cx);
buffer.edit(vec![12..12], "kl\nmno", cx);
buffer.edit(vec![18..18], "\npqrs", cx);
buffer.edit(vec![18..21], "\nPQ", cx);
let chars = buffer.chars_at(Point::new(0, 0));
assert_eq!(chars.collect::<String>(), "abcd\nefgh\nijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(1, 0));
assert_eq!(chars.collect::<String>(), "efgh\nijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(2, 0));
assert_eq!(chars.collect::<String>(), "ijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(3, 0));
assert_eq!(chars.collect::<String>(), "mno\nPQrs");
let chars = buffer.chars_at(Point::new(4, 0));
assert_eq!(chars.collect::<String>(), "PQrs");
// Regression test:
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n", cx);
buffer.edit(vec![60..60], "\n", cx);
let chars = buffer.chars_at(Point::new(6, 0));
assert_eq!(chars.collect::<String>(), " \"xray_wasm\",\n]\n");
buffer
});
}
#[gpui::test]
fn test_anchors(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "abc", cx);
let left_anchor = buffer.anchor_before(2);
let right_anchor = buffer.anchor_after(2);
buffer.edit(vec![1..1], "def\n", cx);
assert_eq!(buffer.text(), "adef\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 6);
assert_eq!(right_anchor.to_offset(&buffer), 6);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(vec![2..3], "", cx);
assert_eq!(buffer.text(), "adf\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 5);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(vec![5..5], "ghi\n", cx);
assert_eq!(buffer.text(), "adf\nbghi\nc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 9);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 });
buffer.edit(vec![7..9], "", cx);
assert_eq!(buffer.text(), "adf\nbghc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 7);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 },);
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 3 });
// Ensure anchoring to a point is equivalent to anchoring to an offset.
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 0 }),
buffer.anchor_before(0)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 1 }),
buffer.anchor_before(1)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 2 }),
buffer.anchor_before(2)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 3 }),
buffer.anchor_before(3)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 0 }),
buffer.anchor_before(4)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 1 }),
buffer.anchor_before(5)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 2 }),
buffer.anchor_before(6)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 3 }),
buffer.anchor_before(7)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 4 }),
buffer.anchor_before(8)
);
// Comparison between anchors.
let anchor_at_offset_0 = buffer.anchor_before(0);
let anchor_at_offset_1 = buffer.anchor_before(1);
let anchor_at_offset_2 = buffer.anchor_before(2);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater
);
buffer
});
}
#[gpui::test]
fn test_anchors_at_start_and_end(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
let before_start_anchor = buffer.anchor_before(0);
let after_end_anchor = buffer.anchor_after(0);
buffer.edit(vec![0..0], "abc", cx);
assert_eq!(buffer.text(), "abc");
assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_end_anchor.to_offset(&buffer), 3);
let after_start_anchor = buffer.anchor_after(0);
let before_end_anchor = buffer.anchor_before(3);
buffer.edit(vec![3..3], "def", cx);
buffer.edit(vec![0..0], "ghi", cx);
assert_eq!(buffer.text(), "ghiabcdef");
assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_start_anchor.to_offset(&buffer), 3);
assert_eq!(before_end_anchor.to_offset(&buffer), 6);
assert_eq!(after_end_anchor.to_offset(&buffer), 9);
buffer
});
}
#[gpui::test]
async fn test_apply_diff(mut cx: gpui::TestAppContext) {
let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
let text = "a\nccc\ndddd\nffffff\n";
let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
}
#[gpui::test]
fn test_undo_redo(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "1234", cx);
// Set group interval to zero so as to not group edits in the undo stack.
buffer.history.group_interval = Duration::from_secs(0);
buffer.edit(vec![1..1], "abx", cx);
buffer.edit(vec![3..4], "yzef", cx);
buffer.edit(vec![3..5], "cd", cx);
assert_eq!(buffer.text(), "1abcdef234");
let transactions = buffer.history.undo_stack.clone();
assert_eq!(transactions.len(), 3);
buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1cdef234");
buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abcdef234");
buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abcdx234");
buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abx234");
buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abyzef234");
buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abcdef234");
buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abyzef234");
buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1yzef234");
buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1234");
buffer
});
}
#[gpui::test]
fn test_history(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut now = Instant::now();
let mut buffer = Buffer::new(0, "123456", cx);
let set_id =
buffer.add_selection_set(buffer.selections_from_ranges(vec![4..4]).unwrap(), cx);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer.edit(vec![2..4], "cd", cx);
buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
assert_eq!(buffer.text(), "12cd56");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer
.update_selection_set(
set_id,
buffer.selections_from_ranges(vec![1..3]).unwrap(),
cx,
)
.unwrap();
buffer.edit(vec![4..5], "e", cx);
buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
now += buffer.history.group_interval + Duration::from_millis(1);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer
.update_selection_set(
set_id,
buffer.selections_from_ranges(vec![2..2]).unwrap(),
cx,
)
.unwrap();
buffer.edit(vec![0..1], "a", cx);
buffer.edit(vec![1..1], "b", cx);
buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
assert_eq!(buffer.text(), "ab2cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
// Last transaction happened past the group interval, undo it on its
// own.
buffer.undo(cx);
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
// First two transactions happened within the group interval, undo them
// together.
buffer.undo(cx);
assert_eq!(buffer.text(), "123456");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
// Redo the first two transactions together.
buffer.redo(cx);
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
// Redo the last transaction on its own.
buffer.redo(cx);
assert_eq!(buffer.text(), "ab2cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
buffer.start_transaction_at(None, now).unwrap();
buffer.end_transaction_at(None, now, cx).unwrap();
buffer.undo(cx);
assert_eq!(buffer.text(), "12cde6");
buffer
});
}
#[gpui::test]
fn test_concurrent_edits(cx: &mut gpui::MutableAppContext) {
let text = "abcdef";
let buffer1 = cx.add_model(|cx| Buffer::new(1, text, cx));
let buffer2 = cx.add_model(|cx| Buffer::new(2, text, cx));
let buffer3 = cx.add_model(|cx| Buffer::new(3, text, cx));
let buf1_op = buffer1.update(cx, |buffer, cx| {
buffer.edit(vec![1..2], "12", cx);
assert_eq!(buffer.text(), "a12cdef");
buffer.operations.last().unwrap().clone()
});
let buf2_op = buffer2.update(cx, |buffer, cx| {
buffer.edit(vec![3..4], "34", cx);
assert_eq!(buffer.text(), "abc34ef");
buffer.operations.last().unwrap().clone()
});
let buf3_op = buffer3.update(cx, |buffer, cx| {
buffer.edit(vec![5..6], "56", cx);
assert_eq!(buffer.text(), "abcde56");
buffer.operations.last().unwrap().clone()
});
buffer1.update(cx, |buffer, _| {
buffer.apply_op(buf2_op.clone()).unwrap();
buffer.apply_op(buf3_op.clone()).unwrap();
});
buffer2.update(cx, |buffer, _| {
buffer.apply_op(buf1_op.clone()).unwrap();
buffer.apply_op(buf3_op.clone()).unwrap();
});
buffer3.update(cx, |buffer, _| {
buffer.apply_op(buf1_op.clone()).unwrap();
buffer.apply_op(buf2_op.clone()).unwrap();
});
assert_eq!(buffer1.read(cx).text(), "a12c34e56");
assert_eq!(buffer2.read(cx).text(), "a12c34e56");
assert_eq!(buffer3.read(cx).text(), "a12c34e56");
}
#[gpui::test(iterations = 100)]
fn test_random_concurrent_edits(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
let peers = env::var("PEERS")
.map(|i| i.parse().expect("invalid `PEERS` variable"))
.unwrap_or(5);
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let base_text_len = rng.gen_range(0..10);
let base_text = RandomCharIter::new(&mut rng)
.take(base_text_len)
.collect::<String>();
let mut replica_ids = Vec::new();
let mut buffers = Vec::new();
let mut network = Network::new(rng.clone());
for i in 0..peers {
let buffer = cx.add_model(|cx| {
let mut buf = Buffer::new(i as ReplicaId, base_text.as_str(), cx);
buf.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
buf
});
buffers.push(buffer);
replica_ids.push(i as u16);
network.add_peer(i as u16);
}
log::info!("initial text: {:?}", base_text);
let mut mutation_count = operations;
loop {
let replica_index = rng.gen_range(0..peers);
let replica_id = replica_ids[replica_index];
buffers[replica_index].update(cx, |buffer, cx| match rng.gen_range(0..=100) {
0..=50 if mutation_count != 0 => {
buffer.randomly_mutate(&mut rng, cx);
network.broadcast(buffer.replica_id, mem::take(&mut buffer.operations));
log::info!("buffer {} text: {:?}", buffer.replica_id, buffer.text());
mutation_count -= 1;
}
51..=70 if mutation_count != 0 => {
buffer.randomly_undo_redo(&mut rng, cx);
network.broadcast(buffer.replica_id, mem::take(&mut buffer.operations));
mutation_count -= 1;
}
71..=100 if network.has_unreceived(replica_id) => {
let ops = network.receive(replica_id);
if !ops.is_empty() {
log::info!(
"peer {} applying {} ops from the network.",
replica_id,
ops.len()
);
buffer.apply_ops(ops, cx).unwrap();
}
}
_ => {}
});
if mutation_count == 0 && network.is_idle() {
break;
}
}
let first_buffer = buffers[0].read(cx);
for buffer in &buffers[1..] {
let buffer = buffer.read(cx);
assert_eq!(
buffer.text(),
first_buffer.text(),
"Replica {} text != Replica 0 text",
buffer.replica_id
);
assert_eq!(
buffer.selection_sets().collect::<HashMap<_, _>>(),
first_buffer.selection_sets().collect::<HashMap<_, _>>()
);
assert_eq!(
buffer.all_selection_ranges().collect::<HashMap<_, _>>(),
first_buffer
.all_selection_ranges()
.collect::<HashMap<_, _>>()
);
}
}
#[derive(Clone)]
struct Envelope<T: Clone> {
message: T,
sender: ReplicaId,
}
struct Network<T: Clone, R: rand::Rng> {
inboxes: std::collections::BTreeMap<ReplicaId, Vec<Envelope<T>>>,
all_messages: Vec<T>,
rng: R,
}
impl<T: Clone, R: rand::Rng> Network<T, R> {
fn new(rng: R) -> Self {
Network {
inboxes: Default::default(),
all_messages: Vec::new(),
rng,
}
}
fn add_peer(&mut self, id: ReplicaId) {
self.inboxes.insert(id, Vec::new());
}
fn is_idle(&self) -> bool {
self.inboxes.values().all(|i| i.is_empty())
}
fn broadcast(&mut self, sender: ReplicaId, messages: Vec<T>) {
for (replica, inbox) in self.inboxes.iter_mut() {
if *replica != sender {
for message in &messages {
let min_index = inbox
.iter()
.enumerate()
.rev()
.find_map(|(index, envelope)| {
if sender == envelope.sender {
Some(index + 1)
} else {
None
}
})
.unwrap_or(0);
// Insert one or more duplicates of this message *after* the previous
// message delivered by this replica.
for _ in 0..self.rng.gen_range(1..4) {
let insertion_index = self.rng.gen_range(min_index..inbox.len() + 1);
inbox.insert(
insertion_index,
Envelope {
message: message.clone(),
sender,
},
);
}
}
}
}
self.all_messages.extend(messages);
}
fn has_unreceived(&self, receiver: ReplicaId) -> bool {
!self.inboxes[&receiver].is_empty()
}
fn receive(&mut self, receiver: ReplicaId) -> Vec<T> {
let inbox = self.inboxes.get_mut(&receiver).unwrap();
let count = self.rng.gen_range(0..inbox.len() + 1);
inbox
.drain(0..count)
.map(|envelope| envelope.message)
.collect()
}
}

View file

@ -4,12 +4,17 @@ version = "0.1.0"
edition = "2018"
[features]
test-support = ["buffer/test-support", "gpui/test-support"]
test-support = [
"buffer/test-support",
"language/test-support",
"gpui/test-support",
]
[dependencies]
buffer = { path = "../buffer" }
clock = { path = "../clock" }
gpui = { path = "../gpui" }
language = { path = "../language" }
sum_tree = { path = "../sum_tree" }
theme = { path = "../theme" }
util = { path = "../util" }
@ -24,6 +29,7 @@ smol = "1.2"
[dev-dependencies]
buffer = { path = "../buffer", features = ["test-support"] }
language = { path = "../language", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
rand = "0.8"
unindent = "0.1.7"

View file

@ -2,9 +2,9 @@ mod fold_map;
mod tab_map;
mod wrap_map;
use buffer::{Anchor, Buffer, Point, ToOffset, ToPoint};
use fold_map::{FoldMap, ToFoldPoint as _};
use gpui::{fonts::FontId, Entity, ModelContext, ModelHandle};
use language::{Anchor, Buffer, Point, ToOffset, ToPoint};
use std::ops::Range;
use sum_tree::Bias;
use tab_map::TabMap;
@ -109,7 +109,7 @@ impl DisplayMap {
}
pub struct DisplayMapSnapshot {
buffer_snapshot: buffer::Snapshot,
buffer_snapshot: language::Snapshot,
folds_snapshot: fold_map::Snapshot,
tabs_snapshot: tab_map::Snapshot,
wraps_snapshot: wrap_map::Snapshot,
@ -358,8 +358,8 @@ impl ToDisplayPoint for Anchor {
mod tests {
use super::*;
use crate::{movement, test::*};
use buffer::{History, Language, LanguageConfig, RandomCharIter, SelectionGoal};
use gpui::{color::Color, MutableAppContext};
use language::{History, Language, LanguageConfig, RandomCharIter, SelectionGoal};
use rand::{prelude::StdRng, Rng};
use std::{env, sync::Arc};
use theme::SyntaxTheme;
@ -436,7 +436,7 @@ mod tests {
}
}
_ => {
buffer.update(&mut cx, |buffer, cx| buffer.randomly_mutate(&mut rng, cx));
buffer.update(&mut cx, |buffer, _| buffer.randomly_edit(&mut rng, 5));
}
}

View file

@ -1,5 +1,5 @@
use buffer::{Anchor, Buffer, Point, ToOffset, AnchorRangeExt, HighlightId, TextSummary};
use gpui::{AppContext, ModelHandle};
use language::{Anchor, AnchorRangeExt, Buffer, HighlightId, Point, TextSummary, ToOffset};
use parking_lot::Mutex;
use std::{
cmp::{self, Ordering},
@ -485,7 +485,7 @@ impl FoldMap {
pub struct Snapshot {
transforms: SumTree<Transform>,
folds: SumTree<Fold>,
buffer_snapshot: buffer::Snapshot,
buffer_snapshot: language::Snapshot,
pub version: usize,
}
@ -994,7 +994,7 @@ impl<'a> Iterator for Chunks<'a> {
pub struct HighlightedChunks<'a> {
transform_cursor: Cursor<'a, Transform, (FoldOffset, usize)>,
buffer_chunks: buffer::HighlightedChunks<'a>,
buffer_chunks: language::HighlightedChunks<'a>,
buffer_chunk: Option<(usize, &'a str, HighlightId)>,
buffer_offset: usize,
}
@ -1331,10 +1331,10 @@ mod tests {
snapshot_edits.extend(map.randomly_mutate(&mut rng, cx.as_ref()));
}
_ => {
let edits = buffer.update(cx, |buffer, cx| {
let edits = buffer.update(cx, |buffer, _| {
let start_version = buffer.version.clone();
let edit_count = rng.gen_range(1..=5);
buffer.randomly_edit(&mut rng, edit_count, cx);
buffer.randomly_edit(&mut rng, edit_count);
buffer.edits_since(start_version).collect::<Vec<_>>()
});
log::info!("editing {:?}", edits);

View file

@ -1,5 +1,5 @@
use super::fold_map::{self, FoldEdit, FoldPoint, Snapshot as FoldSnapshot};
use buffer::{rope, HighlightId};
use language::{rope, HighlightId};
use parking_lot::Mutex;
use std::{mem, ops::Range};
use sum_tree::Bias;

View file

@ -2,8 +2,8 @@ use super::{
fold_map,
tab_map::{self, Edit as TabEdit, Snapshot as TabSnapshot, TabPoint, TextSummary},
};
use buffer::{HighlightId, Point};
use gpui::{fonts::FontId, text_layout::LineWrapper, Entity, ModelContext, Task};
use language::{HighlightId, Point};
use lazy_static::lazy_static;
use smol::future::yield_now;
use std::{collections::VecDeque, ops::Range, time::Duration};
@ -899,7 +899,7 @@ mod tests {
display_map::{fold_map::FoldMap, tab_map::TabMap},
test::Observer,
};
use buffer::{Buffer, RandomCharIter};
use language::{Buffer, RandomCharIter};
use rand::prelude::*;
use std::env;
@ -990,7 +990,7 @@ mod tests {
}
}
_ => {
buffer.update(&mut cx, |buffer, cx| buffer.randomly_mutate(&mut rng, cx));
buffer.update(&mut cx, |buffer, _| buffer.randomly_mutate(&mut rng));
}
}

View file

@ -2,7 +2,6 @@ use super::{
DisplayPoint, Editor, EditorMode, EditorSettings, EditorStyle, Input, Scroll, Select,
SelectPhase, Snapshot, MAX_LINE_LEN,
};
use buffer::HighlightId;
use clock::ReplicaId;
use gpui::{
color::Color,
@ -18,6 +17,7 @@ use gpui::{
MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
};
use json::json;
use language::HighlightId;
use smallvec::SmallVec;
use std::{
cmp::{self, Ordering},
@ -1043,7 +1043,7 @@ mod tests {
test::sample_text,
{Editor, EditorSettings},
};
use buffer::Buffer;
use language::Buffer;
#[gpui::test]
fn test_layout_line_numbers(cx: &mut gpui::MutableAppContext) {

View file

@ -5,7 +5,6 @@ pub mod movement;
#[cfg(test)]
mod test;
use buffer::*;
use clock::ReplicaId;
pub use display_map::DisplayPoint;
use display_map::*;
@ -15,6 +14,7 @@ use gpui::{
text_layout, AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle,
MutableAppContext, RenderContext, View, ViewContext, WeakViewHandle,
};
use language::*;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use smol::Timer;
@ -2661,17 +2661,17 @@ impl Editor {
fn on_buffer_event(
&mut self,
_: ModelHandle<Buffer>,
event: &buffer::Event,
event: &language::Event,
cx: &mut ViewContext<Self>,
) {
match event {
buffer::Event::Edited => cx.emit(Event::Edited),
buffer::Event::Dirtied => cx.emit(Event::Dirtied),
buffer::Event::Saved => cx.emit(Event::Saved),
buffer::Event::FileHandleChanged => cx.emit(Event::FileHandleChanged),
buffer::Event::Reloaded => cx.emit(Event::FileHandleChanged),
buffer::Event::Closed => cx.emit(Event::Closed),
buffer::Event::Reparsed => {}
language::Event::Edited => cx.emit(Event::Edited),
language::Event::Dirtied => cx.emit(Event::Dirtied),
language::Event::Saved => cx.emit(Event::Saved),
language::Event::FileHandleChanged => cx.emit(Event::FileHandleChanged),
language::Event::Reloaded => cx.emit(Event::FileHandleChanged),
language::Event::Closed => cx.emit(Event::Closed),
language::Event::Reparsed => {}
}
}

View file

@ -0,0 +1,32 @@
[package]
name = "language"
version = "0.1.0"
edition = "2018"
[features]
test-support = ["rand", "buffer/test-support"]
[dependencies]
buffer = { path = "../buffer" }
clock = { path = "../clock" }
gpui = { path = "../gpui" }
rpc = { path = "../rpc" }
theme = { path = "../theme" }
util = { path = "../util" }
anyhow = "1.0.38"
futures = "0.3"
lazy_static = "1.4"
log = "0.4"
parking_lot = "0.11.1"
rand = { version = "0.8.3", optional = true }
serde = { version = "1", features = ["derive"] }
similar = "1.3"
smol = "1.2"
tree-sitter = "0.19.5"
[dev-dependencies]
buffer = { path = "../buffer", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
rand = "0.8.3"
tree-sitter-rust = "0.19.0"
unindent = "0.1.7"

1477
crates/language/src/lib.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,81 @@
use crate::*;
use super::*;
use gpui::{ModelHandle, MutableAppContext};
use std::rc::Rc;
use unindent::Unindent as _;
#[gpui::test]
fn test_edit_events(cx: &mut gpui::MutableAppContext) {
let mut now = Instant::now();
let buffer_1_events = Rc::new(RefCell::new(Vec::new()));
let buffer_2_events = Rc::new(RefCell::new(Vec::new()));
let buffer1 = cx.add_model(|cx| Buffer::new(0, "abcdef", cx));
let buffer2 = cx.add_model(|cx| Buffer::new(1, "abcdef", cx));
let buffer_ops = buffer1.update(cx, |buffer, cx| {
let buffer_1_events = buffer_1_events.clone();
cx.subscribe(&buffer1, move |_, _, event, _| {
buffer_1_events.borrow_mut().push(event.clone())
})
.detach();
let buffer_2_events = buffer_2_events.clone();
cx.subscribe(&buffer2, move |_, _, event, _| {
buffer_2_events.borrow_mut().push(event.clone())
})
.detach();
// An edit emits an edited event, followed by a dirtied event,
// since the buffer was previously in a clean state.
buffer.edit(Some(2..4), "XYZ", cx);
// An empty transaction does not emit any events.
buffer.start_transaction(None).unwrap();
buffer.end_transaction(None, cx).unwrap();
// A transaction containing two edits emits one edited event.
now += Duration::from_secs(1);
buffer.start_transaction_at(None, now).unwrap();
buffer.edit(Some(5..5), "u", cx);
buffer.edit(Some(6..6), "w", cx);
buffer.end_transaction_at(None, now, cx).unwrap();
// Undoing a transaction emits one edited event.
buffer.undo(cx);
buffer.operations.clone()
});
// Incorporating a set of remote ops emits a single edited event,
// followed by a dirtied event.
buffer2.update(cx, |buffer, cx| {
buffer.apply_ops(buffer_ops, cx).unwrap();
});
let buffer_1_events = buffer_1_events.borrow();
assert_eq!(
*buffer_1_events,
vec![Event::Edited, Event::Dirtied, Event::Edited, Event::Edited]
);
let buffer_2_events = buffer_2_events.borrow();
assert_eq!(*buffer_2_events, vec![Event::Edited, Event::Dirtied]);
}
#[gpui::test]
async fn test_apply_diff(mut cx: gpui::TestAppContext) {
let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
let text = "a\nccc\ndddd\nffffff\n";
let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
}
#[gpui::test]
async fn test_reparse(mut cx: gpui::TestAppContext) {
let buffer = cx.add_model(|cx| {
@ -351,6 +425,19 @@ fn test_contiguous_ranges() {
);
}
impl Buffer {
pub fn enclosing_bracket_point_ranges<T: ToOffset>(
&self,
range: Range<T>,
) -> Option<(Range<Point>, Range<Point>)> {
self.enclosing_bracket_ranges(range).map(|(start, end)| {
let point_start = start.start.to_point(self)..start.end.to_point(self);
let point_end = end.start.to_point(self)..end.end.to_point(self);
(point_start, point_end)
})
}
}
fn rust_lang() -> Arc<Language> {
Arc::new(
Language::new(

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2018"
[features]
test-support = []
test-support = ["language/test-support", "buffer/test-support"]
[dependencies]
buffer = { path = "../buffer" }
@ -12,6 +12,7 @@ clock = { path = "../clock" }
fsevent = { path = "../fsevent" }
fuzzy = { path = "../fuzzy" }
gpui = { path = "../gpui" }
language = { path = "../language" }
client = { path = "../client" }
sum_tree = { path = "../sum_tree" }
util = { path = "../util" }
@ -33,6 +34,8 @@ toml = "0.5"
[dev-dependencies]
client = { path = "../client", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
language = { path = "../language", features = ["test-support"] }
util = { path = "../util", features = ["test-support"] }
rpc = { path = "../rpc", features = ["test-support"] }

View file

@ -3,11 +3,11 @@ mod ignore;
mod worktree;
use anyhow::Result;
use buffer::LanguageRegistry;
use client::Client;
use futures::Future;
use fuzzy::{PathMatch, PathMatchCandidate, PathMatchCandidateSet};
use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
use language::LanguageRegistry;
use std::{
path::Path,
sync::{atomic::AtomicBool, Arc},
@ -302,9 +302,9 @@ impl Entity for Project {
#[cfg(test)]
mod tests {
use super::*;
use buffer::LanguageRegistry;
use fs::RealFs;
use gpui::TestAppContext;
use language::LanguageRegistry;
use serde_json::json;
use std::{os::unix, path::PathBuf};
use util::test::temp_tree;

View file

@ -4,7 +4,6 @@ use super::{
};
use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
use anyhow::{anyhow, Result};
use buffer::{Buffer, History, LanguageRegistry, Operation, Rope};
use client::{proto, Client, PeerId, TypedEnvelope};
use clock::ReplicaId;
use futures::{Stream, StreamExt};
@ -13,6 +12,7 @@ use gpui::{
executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
Task, UpgradeModelHandle, WeakModelHandle,
};
use language::{Buffer, History, LanguageRegistry, Operation, Rope};
use lazy_static::lazy_static;
use parking_lot::Mutex;
use postage::{
@ -587,54 +587,40 @@ impl Worktree {
}
};
let worktree_handle = cx.handle();
let mut buffers_to_delete = Vec::new();
for (buffer_id, buffer) in open_buffers {
if let Some(buffer) = buffer.upgrade(cx) {
buffer.update(cx, |buffer, cx| {
let buffer_is_clean = !buffer.is_dirty();
if let Some(file) = buffer.file_mut() {
let mut file_changed = false;
if let Some(entry) = file
if let Some(old_file) = buffer.file() {
let new_file = if let Some(entry) = old_file
.entry_id()
.and_then(|entry_id| self.entry_for_id(entry_id))
{
if entry.path != *file.path() {
file.set_path(entry.path.clone());
file_changed = true;
File {
entry_id: Some(entry.id),
mtime: entry.mtime,
path: entry.path.clone(),
worktree: worktree_handle.clone(),
}
} else if let Some(entry) = self.entry_for_path(old_file.path().as_ref()) {
File {
entry_id: Some(entry.id),
mtime: entry.mtime,
path: entry.path.clone(),
worktree: worktree_handle.clone(),
}
} else {
File {
entry_id: None,
path: old_file.path().clone(),
mtime: old_file.mtime(),
worktree: worktree_handle.clone(),
}
};
if entry.mtime != file.mtime() {
file.set_mtime(entry.mtime);
file_changed = true;
if let Some(worktree) = self.as_local() {
if buffer_is_clean {
let abs_path = worktree.absolutize(file.path().as_ref());
refresh_buffer(abs_path, &worktree.fs, cx);
}
}
}
} else if let Some(entry) = self.entry_for_path(file.path().as_ref()) {
file.set_entry_id(Some(entry.id));
file.set_mtime(entry.mtime);
if let Some(worktree) = self.as_local() {
if buffer_is_clean {
let abs_path = worktree.absolutize(file.path().as_ref());
refresh_buffer(abs_path, &worktree.fs, cx);
}
}
file_changed = true;
} else if !file.is_deleted() {
if buffer_is_clean {
cx.emit(buffer::Event::Dirtied);
}
file.set_entry_id(None);
file_changed = true;
}
if file_changed {
cx.emit(buffer::Event::FileHandleChanged);
if let Some(task) = buffer.file_updated(Box::new(new_file), cx) {
task.detach();
}
}
});
@ -862,7 +848,7 @@ impl LocalWorktree {
.update(&mut cx, |this, cx| this.as_local().unwrap().load(&path, cx))
.await?;
let language = this.read_with(&cx, |this, cx| {
use buffer::File;
use language::File;
this.languages()
.select_language(file.full_path(cx))
@ -909,7 +895,7 @@ impl LocalWorktree {
.insert(buffer.id() as u64, buffer.clone());
Ok(proto::OpenBufferResponse {
buffer: Some(buffer.update(cx.as_mut(), |buffer, cx| buffer.to_proto(cx))),
buffer: Some(buffer.update(cx.as_mut(), |buffer, _| buffer.to_proto())),
})
})
})
@ -1183,24 +1169,6 @@ fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
Ok(builder.build()?)
}
pub fn refresh_buffer(abs_path: PathBuf, fs: &Arc<dyn Fs>, cx: &mut ModelContext<Buffer>) {
let fs = fs.clone();
cx.spawn(|buffer, mut cx| async move {
let new_text = fs.load(&abs_path).await;
match new_text {
Err(error) => log::error!("error refreshing buffer after file changed: {}", error),
Ok(new_text) => {
buffer
.update(&mut cx, |buffer, cx| {
buffer.set_text_from_disk(new_text.into(), cx)
})
.await;
}
}
})
.detach()
}
impl Deref for LocalWorktree {
type Target = Snapshot;
@ -1279,7 +1247,7 @@ impl RemoteWorktree {
.ok_or_else(|| anyhow!("worktree was closed"))?;
let file = File::new(entry.id, this.clone(), entry.path, entry.mtime);
let language = this.read_with(&cx, |this, cx| {
use buffer::File;
use language::File;
this.languages()
.select_language(file.full_path(cx))
@ -1790,7 +1758,7 @@ impl File {
}
}
impl buffer::File for File {
impl language::File for File {
fn worktree_id(&self) -> usize {
self.worktree.id()
}
@ -1799,26 +1767,14 @@ impl buffer::File for File {
self.entry_id
}
fn set_entry_id(&mut self, entry_id: Option<usize>) {
self.entry_id = entry_id;
}
fn mtime(&self) -> SystemTime {
self.mtime
}
fn set_mtime(&mut self, mtime: SystemTime) {
self.mtime = mtime;
}
fn path(&self) -> &Arc<Path> {
&self.path
}
fn set_path(&mut self, path: Arc<Path>) {
self.path = path;
}
fn full_path(&self, cx: &AppContext) -> PathBuf {
let worktree = self.worktree.read(cx);
let mut full_path = PathBuf::new();
@ -1887,6 +1843,16 @@ impl buffer::File for File {
})
}
fn load_local(&self, cx: &AppContext) -> Option<Task<Result<String>>> {
let worktree = self.worktree.read(cx).as_local()?;
let abs_path = worktree.absolutize(&self.path);
let fs = worktree.fs.clone();
Some(
cx.background()
.spawn(async move { fs.load(&abs_path).await }),
)
}
fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext) {
self.worktree.update(cx, |worktree, cx| {
if let Some((rpc, remote_id)) = match worktree {
@ -1942,7 +1908,7 @@ impl buffer::File for File {
});
}
fn boxed_clone(&self) -> Box<dyn buffer::File> {
fn boxed_clone(&self) -> Box<dyn language::File> {
Box::new(self.clone())
}
@ -3268,7 +3234,7 @@ mod tests {
assert!(buffer.is_dirty());
assert_eq!(
*events.borrow(),
&[buffer::Event::Edited, buffer::Event::Dirtied]
&[language::Event::Edited, language::Event::Dirtied]
);
events.borrow_mut().clear();
buffer.did_save(buffer.version(), buffer.file().unwrap().mtime(), None, cx);
@ -3277,7 +3243,7 @@ mod tests {
// after saving, the buffer is not dirty, and emits a saved event.
buffer1.update(&mut cx, |buffer, cx| {
assert!(!buffer.is_dirty());
assert_eq!(*events.borrow(), &[buffer::Event::Saved]);
assert_eq!(*events.borrow(), &[language::Event::Saved]);
events.borrow_mut().clear();
buffer.edit(vec![1..1], "B", cx);
@ -3291,9 +3257,9 @@ mod tests {
assert_eq!(
*events.borrow(),
&[
buffer::Event::Edited,
buffer::Event::Dirtied,
buffer::Event::Edited
language::Event::Edited,
language::Event::Dirtied,
language::Event::Edited
],
);
events.borrow_mut().clear();
@ -3305,7 +3271,7 @@ mod tests {
assert!(buffer.is_dirty());
});
assert_eq!(*events.borrow(), &[buffer::Event::Edited]);
assert_eq!(*events.borrow(), &[language::Event::Edited]);
// When a file is deleted, the buffer is considered dirty.
let events = Rc::new(RefCell::new(Vec::new()));
@ -3325,7 +3291,7 @@ mod tests {
buffer2.condition(&cx, |b, _| b.is_dirty()).await;
assert_eq!(
*events.borrow(),
&[buffer::Event::Dirtied, buffer::Event::FileHandleChanged]
&[language::Event::Dirtied, language::Event::FileHandleChanged]
);
// When a file is already dirty when deleted, we don't emit a Dirtied event.
@ -3351,7 +3317,7 @@ mod tests {
buffer3
.condition(&cx, |_, _| !events.borrow().is_empty())
.await;
assert_eq!(*events.borrow(), &[buffer::Event::FileHandleChanged]);
assert_eq!(*events.borrow(), &[language::Event::FileHandleChanged]);
cx.read(|cx| assert!(buffer3.read(cx).is_dirty()));
}
@ -3442,12 +3408,13 @@ mod tests {
buffer.update(&mut cx, |buffer, cx| {
buffer.edit(vec![0..0], " ", cx);
assert!(buffer.is_dirty());
assert!(!buffer.has_conflict());
});
// Change the file on disk again, adding blank lines to the beginning.
fs::write(&abs_path, "\n\n\nAAAA\naaa\nBB\nbbbbb\n").unwrap();
// Becaues the buffer is modified, it doesn't reload from disk, but is
// Because the buffer is modified, it doesn't reload from disk, but is
// marked as having a conflict.
buffer
.condition(&cx, |buffer, _| buffer.has_conflict())

View file

@ -976,13 +976,13 @@ mod tests {
time::Duration,
};
use zed::{
buffer::LanguageRegistry,
client::{
self, test::FakeHttpClient, Channel, ChannelDetails, ChannelList, Client, Credentials,
EstablishConnectionError, UserStore,
},
editor::{Editor, EditorSettings, Input},
fs::{FakeFs, Fs as _},
language::LanguageRegistry,
people_panel::JoinWorktree,
project::{ProjectPath, Worktree},
workspace::{Workspace, WorkspaceParams},

View file

@ -8,7 +8,7 @@ test-support = [
"client/test-support",
"project/test-support",
"tree-sitter",
"tree-sitter-rust"
"tree-sitter-rust",
]
[dependencies]
@ -16,6 +16,7 @@ buffer = { path = "../buffer" }
client = { path = "../client" }
editor = { path = "../editor" }
gpui = { path = "../gpui" }
language = { path = "../language" }
project = { path = "../project" }
theme = { path = "../theme" }
anyhow = "1.0.38"

View file

@ -1,9 +1,9 @@
use super::{Item, ItemView};
use crate::Settings;
use anyhow::Result;
use buffer::{Buffer, File as _};
use editor::{Editor, EditorSettings, Event};
use gpui::{fonts::TextStyle, AppContext, ModelHandle, Task, ViewContext};
use language::{Buffer, File as _};
use postage::watch;
use project::{ProjectPath, Worktree};
use std::path::Path;

View file

@ -5,7 +5,7 @@ pub mod settings;
pub mod sidebar;
use anyhow::Result;
use buffer::{Buffer, LanguageRegistry};
use language::{Buffer, LanguageRegistry};
use client::{Authenticate, ChannelList, Client, UserStore};
use gpui::{
action, elements::*, json::to_string_pretty, keymap::Binding, platform::CursorStyle,
@ -271,8 +271,8 @@ impl WorkspaceParams {
#[cfg(any(test, feature = "test-support"))]
pub fn test(cx: &mut MutableAppContext) -> Self {
let mut languages = LanguageRegistry::new();
languages.add(Arc::new(buffer::Language::new(
buffer::LanguageConfig {
languages.add(Arc::new(language::Language::new(
language::LanguageConfig {
name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()],
..Default::default()

View file

@ -18,6 +18,7 @@ test-support = [
"buffer/test-support",
"client/test-support",
"gpui/test-support",
"language/test-support",
"project/test-support",
"rpc/test-support",
"tempdir",
@ -33,6 +34,7 @@ fuzzy = { path = "../fuzzy" }
editor = { path = "../editor" }
file_finder = { path = "../file_finder" }
gpui = { path = "../gpui" }
language = { path = "../language" }
people_panel = { path = "../people_panel" }
project = { path = "../project" }
project_panel = { path = "../project_panel" }
@ -85,6 +87,7 @@ url = "2.2"
buffer = { path = "../buffer", features = ["test-support"] }
editor = { path = "../editor", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
language = { path = "../language", features = ["test-support"] }
project = { path = "../project", features = ["test-support"] }
rpc = { path = "../rpc", features = ["test-support"] }
client = { path = "../client", features = ["test-support"] }

View file

@ -1,4 +1,4 @@
use buffer::{Language, LanguageRegistry};
pub use language::{Language, LanguageRegistry};
use rust_embed::RustEmbed;
use std::borrow::Cow;
use std::{str, sync::Arc};

View file

@ -4,8 +4,7 @@ pub mod menus;
#[cfg(any(test, feature = "test-support"))]
pub mod test;
pub use buffer;
use buffer::LanguageRegistry;
use self::language::LanguageRegistry;
use chat_panel::ChatPanel;
pub use client;
pub use editor;

View file

@ -1,7 +1,7 @@
use crate::{assets::Assets, AppState};
use buffer::LanguageRegistry;
use client::{http::ServerResponse, test::FakeHttpClient, ChannelList, Client, UserStore};
use gpui::{AssetSource, MutableAppContext};
use language::LanguageRegistry;
use parking_lot::Mutex;
use postage::watch;
use project::fs::FakeFs;