Rely more on conditions for state derived after worktree changes

This commit is contained in:
Antonio Scandurra 2021-05-13 11:25:29 +02:00
parent 2c430668fc
commit 19f51bc480
3 changed files with 15 additions and 29 deletions

View File

@ -3148,12 +3148,13 @@ mod tests {
}); });
fs::remove_file(dir.path().join("file2")).unwrap(); fs::remove_file(dir.path().join("file2")).unwrap();
tree.flush_fs_events(&app).await; buffer2
.condition(&app, |buffer2, _| buffer2.is_dirty())
.await;
assert_eq!( assert_eq!(
*events.borrow(), *events.borrow(),
&[Event::Dirtied, Event::FileHandleChanged] &[Event::Dirtied, Event::FileHandleChanged]
); );
app.read(|ctx| assert!(buffer2.read(ctx).is_dirty()));
// When a file is already dirty when deleted, we don't emit a Dirtied event. // When a file is already dirty when deleted, we don't emit a Dirtied event.
let events = Rc::new(RefCell::new(Vec::new())); let events = Rc::new(RefCell::new(Vec::new()));
@ -3173,7 +3174,9 @@ mod tests {
}); });
events.borrow_mut().clear(); events.borrow_mut().clear();
fs::remove_file(dir.path().join("file3")).unwrap(); fs::remove_file(dir.path().join("file3")).unwrap();
tree.flush_fs_events(&app).await; buffer3
.condition(&app, |_, _| !events.borrow().is_empty())
.await;
assert_eq!(*events.borrow(), &[Event::FileHandleChanged]); assert_eq!(*events.borrow(), &[Event::FileHandleChanged]);
app.read(|ctx| assert!(buffer3.read(ctx).is_dirty())); app.read(|ctx| assert!(buffer3.read(ctx).is_dirty()));
}); });
@ -3222,7 +3225,6 @@ mod tests {
}); });
let new_contents = "AAAA\naaa\nBB\nbbbbb\n"; let new_contents = "AAAA\naaa\nBB\nbbbbb\n";
fs::write(&abs_path, new_contents).unwrap(); fs::write(&abs_path, new_contents).unwrap();
tree.flush_fs_events(&app).await;
// Because the buffer was not modified, it is reloaded from disk. Its // Because the buffer was not modified, it is reloaded from disk. Its
// contents are edited according to the diff between the old and new // contents are edited according to the diff between the old and new

View File

@ -1030,20 +1030,17 @@ mod tests {
app.update(|ctx| editor.update(ctx, |editor, ctx| editor.insert(&"x".to_string(), ctx))); app.update(|ctx| editor.update(ctx, |editor, ctx| editor.insert(&"x".to_string(), ctx)));
fs::write(dir.path().join("a.txt"), "changed").unwrap(); fs::write(dir.path().join("a.txt"), "changed").unwrap();
tree.flush_fs_events(&app).await; editor
app.read(|ctx| { .condition(&app, |editor, ctx| editor.has_conflict(ctx))
assert!(editor.is_dirty(ctx)); .await;
assert!(editor.has_conflict(ctx)); app.read(|ctx| assert!(editor.is_dirty(ctx)));
});
app.update(|ctx| workspace.update(ctx, |w, ctx| w.save_active_item(&(), ctx))); app.update(|ctx| workspace.update(ctx, |w, ctx| w.save_active_item(&(), ctx)));
app.simulate_prompt_answer(window_id, 0); app.simulate_prompt_answer(window_id, 0);
tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx)) editor
.condition(&app, |editor, ctx| !editor.is_dirty(ctx))
.await; .await;
app.read(|ctx| { app.read(|ctx| assert!(!editor.has_conflict(ctx)));
assert!(!editor.is_dirty(ctx));
assert!(!editor.has_conflict(ctx));
});
} }
#[gpui::test] #[gpui::test]
@ -1097,7 +1094,8 @@ mod tests {
}); });
// When the save completes, the buffer's title is updated. // When the save completes, the buffer's title is updated.
tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx)) editor
.condition(&app, |editor, ctx| !editor.is_dirty(ctx))
.await; .await;
app.read(|ctx| { app.read(|ctx| {
assert!(!editor.is_dirty(ctx)); assert!(!editor.is_dirty(ctx));

View File

@ -134,20 +134,6 @@ impl Worktree {
} }
} }
pub fn next_scan_complete(&self, ctx: &mut ModelContext<Self>) -> impl Future<Output = ()> {
let scan_id = self.snapshot.scan_id;
let mut scan_state = self.scan_state.1.clone();
ctx.spawn(|this, ctx| async move {
while let Some(scan_state) = scan_state.recv().await {
if this.read_with(&ctx, |this, _| {
matches!(scan_state, ScanState::Idle) && this.snapshot.scan_id > scan_id
}) {
break;
}
}
})
}
fn observe_scan_state(&mut self, scan_state: ScanState, ctx: &mut ModelContext<Self>) { fn observe_scan_state(&mut self, scan_state: ScanState, ctx: &mut ModelContext<Self>) {
let _ = self.scan_state.0.blocking_send(scan_state); let _ = self.scan_state.0.blocking_send(scan_state);
self.poll_entries(ctx); self.poll_entries(ctx);