Start on adding a randomized test for BackgroundWrapper
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
8d3c3d7c46
commit
1ba9e48271
@@ -21,7 +21,9 @@ impl DisplayMap {
|
||||
let fold_map = FoldMap::new(buffer.clone(), cx);
|
||||
let (snapshot, edits) = fold_map.read(cx);
|
||||
assert_eq!(edits.len(), 0);
|
||||
let wrap_map = WrapMap::new(snapshot, cx);
|
||||
// TODO: take `wrap_width` as a parameter.
|
||||
let config = { todo!() };
|
||||
let wrap_map = WrapMap::new(snapshot, config, cx);
|
||||
DisplayMap {
|
||||
buffer,
|
||||
fold_map,
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
editor::{display_map::fold_map, Point, TextSummary},
|
||||
settings::Settings,
|
||||
sum_tree::{self, SumTree},
|
||||
util::Bias,
|
||||
};
|
||||
use gpui::{AppContext, Task};
|
||||
use gpui::{font_cache::FamilyId, AppContext, FontCache, FontSystem, Task};
|
||||
use parking_lot::Mutex;
|
||||
use postage::{prelude::Sink, watch};
|
||||
use smol::channel;
|
||||
@@ -19,6 +22,12 @@ struct State {
|
||||
interpolated_version: usize,
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
pub wrap_width: f32,
|
||||
pub font_family: FamilyId,
|
||||
pub font_size: f32,
|
||||
}
|
||||
|
||||
pub struct WrapMap {
|
||||
state: Mutex<State>,
|
||||
edits_tx: channel::Sender<(fold_map::Snapshot, Vec<fold_map::Edit>)>,
|
||||
@@ -27,7 +36,9 @@ pub struct WrapMap {
|
||||
}
|
||||
|
||||
impl WrapMap {
|
||||
pub fn new(folds_snapshot: fold_map::Snapshot, cx: &AppContext) -> Self {
|
||||
pub fn new(folds_snapshot: fold_map::Snapshot, config: Config, cx: &AppContext) -> Self {
|
||||
let font_cache = cx.font_cache().clone();
|
||||
let font_system = cx.platform().fonts();
|
||||
let snapshot = Snapshot {
|
||||
transforms: SumTree::from_item(
|
||||
Transform {
|
||||
@@ -45,8 +56,10 @@ impl WrapMap {
|
||||
watch::channel_with(snapshot.clone());
|
||||
let (edits_tx, edits_rx) = channel::unbounded();
|
||||
let background_task = cx.background().spawn(async move {
|
||||
let mut wrapper = BackgroundWrapper::new(edits_rx, background_snapshots_tx);
|
||||
wrapper.run(folds_snapshot).await;
|
||||
let mut wrapper = BackgroundWrapper::new(config, font_cache, font_system);
|
||||
wrapper
|
||||
.run(folds_snapshot, edits_rx, background_snapshots_tx)
|
||||
.await;
|
||||
});
|
||||
|
||||
Self {
|
||||
@@ -68,19 +81,18 @@ impl WrapMap {
|
||||
}
|
||||
|
||||
struct BackgroundWrapper {
|
||||
edits_rx: channel::Receiver<(fold_map::Snapshot, Vec<fold_map::Edit>)>,
|
||||
snapshots_tx: watch::Sender<Snapshot>,
|
||||
config: Config,
|
||||
font_cache: Arc<FontCache>,
|
||||
font_system: Arc<dyn FontSystem>,
|
||||
snapshot: Snapshot,
|
||||
}
|
||||
|
||||
impl BackgroundWrapper {
|
||||
fn new(
|
||||
edits_rx: channel::Receiver<(fold_map::Snapshot, Vec<fold_map::Edit>)>,
|
||||
snapshots_tx: watch::Sender<Snapshot>,
|
||||
) -> Self {
|
||||
fn new(config: Config, font_cache: Arc<FontCache>, font_system: Arc<dyn FontSystem>) -> Self {
|
||||
Self {
|
||||
edits_rx,
|
||||
snapshots_tx,
|
||||
config,
|
||||
font_cache,
|
||||
font_system,
|
||||
snapshot: Snapshot {
|
||||
transforms: Default::default(),
|
||||
version: Default::default(),
|
||||
@@ -88,23 +100,30 @@ impl BackgroundWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&mut self, snapshot: fold_map::Snapshot) {
|
||||
async fn run(
|
||||
&mut self,
|
||||
snapshot: fold_map::Snapshot,
|
||||
edits_rx: channel::Receiver<(fold_map::Snapshot, Vec<fold_map::Edit>)>,
|
||||
mut snapshots_tx: watch::Sender<Snapshot>,
|
||||
) {
|
||||
let edit = fold_map::Edit {
|
||||
old_bytes: 0..0,
|
||||
new_bytes: 0..snapshot.len(),
|
||||
};
|
||||
if !self.sync(snapshot, vec![edit]).await {
|
||||
self.sync(snapshot, vec![edit]);
|
||||
if snapshots_tx.send(self.snapshot.clone()).await.is_err() {
|
||||
return;
|
||||
}
|
||||
|
||||
while let Ok((snapshot, edits)) = self.edits_rx.recv().await {
|
||||
if !self.sync(snapshot, edits).await {
|
||||
while let Ok((snapshot, edits)) = edits_rx.recv().await {
|
||||
self.sync(snapshot, edits);
|
||||
if snapshots_tx.send(self.snapshot.clone()).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync(&mut self, snapshot: fold_map::Snapshot, edits: Vec<fold_map::Edit>) -> bool {
|
||||
fn sync(&mut self, snapshot: fold_map::Snapshot, edits: Vec<fold_map::Edit>) {
|
||||
let mut new_transforms = SumTree::new();
|
||||
{
|
||||
// let mut old_cursor = self.snapshot.transforms.cursor::<Point, ()>();
|
||||
@@ -118,7 +137,6 @@ impl BackgroundWrapper {
|
||||
|
||||
self.snapshot.transforms = new_transforms;
|
||||
self.snapshot.version = snapshot.version;
|
||||
self.snapshots_tx.send(self.snapshot.clone()).await.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,3 +174,60 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for Point {
|
||||
*self += &summary.folded.lines;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::editor::display_map::fold_map::FoldMap;
|
||||
use crate::editor::{Buffer, ToPoint};
|
||||
use crate::util::RandomCharIter;
|
||||
use rand::prelude::*;
|
||||
use std::env;
|
||||
use Bias::{Left, Right};
|
||||
|
||||
#[gpui::test]
|
||||
fn test_random_folds(cx: &mut gpui::MutableAppContext) {
|
||||
let iterations = env::var("ITERATIONS")
|
||||
.map(|i| i.parse().expect("invalid `ITERATIONS` variable"))
|
||||
.unwrap_or(100);
|
||||
let operations = env::var("OPERATIONS")
|
||||
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
|
||||
.unwrap_or(10);
|
||||
let seed_range = if let Ok(seed) = env::var("SEED") {
|
||||
let seed = seed.parse().expect("invalid `SEED` variable");
|
||||
seed..seed + 1
|
||||
} else {
|
||||
0..iterations
|
||||
};
|
||||
|
||||
for seed in seed_range {
|
||||
dbg!(seed);
|
||||
let mut rng = StdRng::seed_from_u64(seed);
|
||||
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
Buffer::new(0, text, cx)
|
||||
});
|
||||
let fold_map = FoldMap::new(buffer.clone(), cx.as_ref());
|
||||
let (snapshot, _) = fold_map.read(cx.as_ref());
|
||||
let font_cache = cx.font_cache().clone();
|
||||
let font_system = cx.platform().fonts();
|
||||
let config = Config {
|
||||
wrap_width: rng.gen_range(100.0..=1000.0),
|
||||
font_family: font_cache.load_family(&["Helvetica"]).unwrap(),
|
||||
font_size: 14.0,
|
||||
};
|
||||
let mut wrapper = BackgroundWrapper::new(config, font_cache.clone(), font_system);
|
||||
let edit = fold_map::Edit {
|
||||
old_bytes: 0..0,
|
||||
new_bytes: 0..snapshot.len(),
|
||||
};
|
||||
wrapper.sync(snapshot, vec![edit]);
|
||||
// for line in wrapper.snapshot.text().lines() {
|
||||
// font_cache.
|
||||
// }
|
||||
// assert_eq!(wrap_map.snapshot().text())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user