Merge branch 'zed2' of github.com:zed-industries/zed into zed2

This commit is contained in:
Marshall Bowers 2023-10-25 20:38:03 +02:00
commit b5eae86b67
16 changed files with 1931 additions and 120 deletions

33
Cargo.lock generated
View file

@ -1471,7 +1471,7 @@ dependencies = [
"async-recursion 0.3.2",
"async-tungstenite",
"collections",
"db",
"db2",
"feature_flags2",
"futures 0.3.28",
"gpui2",
@ -2991,6 +2991,35 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "fs2"
version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"collections",
"fsevent",
"futures 0.3.28",
"git2",
"gpui2",
"lazy_static",
"libc",
"log",
"parking_lot 0.11.2",
"rand 0.8.5",
"regex",
"rope",
"serde",
"serde_derive",
"serde_json",
"smol",
"sum_tree",
"tempfile",
"text",
"time",
"util",
]
[[package]]
name = "fsevent"
version = "2.0.2"
@ -7485,7 +7514,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"collections",
"feature_flags",
"feature_flags2",
"fs",
"futures 0.3.28",
"gpui2",

View file

@ -34,6 +34,7 @@ members = [
"crates/feedback",
"crates/file_finder",
"crates/fs",
"crates/fs2",
"crates/fsevent",
"crates/fuzzy",
"crates/fuzzy2",

View file

@ -13,7 +13,7 @@ test-support = ["collections/test-support", "gpui2/test-support", "rpc/test-supp
[dependencies]
collections = { path = "../collections" }
db = { path = "../db" }
db2 = { path = "../db2" }
gpui2 = { path = "../gpui2" }
util = { path = "../util" }
rpc = { path = "../rpc" }

41
crates/fs2/Cargo.toml Normal file
View file

@ -0,0 +1,41 @@
[package]
name = "fs2"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
path = "src/fs2.rs"
[dependencies]
collections = { path = "../collections" }
rope = { path = "../rope" }
text = { path = "../text" }
util = { path = "../util" }
sum_tree = { path = "../sum_tree" }
anyhow.workspace = true
async-trait.workspace = true
futures.workspace = true
tempfile = "3"
fsevent = { path = "../fsevent" }
lazy_static.workspace = true
parking_lot.workspace = true
smol.workspace = true
regex.workspace = true
git2.workspace = true
serde.workspace = true
serde_derive.workspace = true
serde_json.workspace = true
log.workspace = true
libc = "0.2"
time.workspace = true
gpui2 = { path = "../gpui2", optional = true}
[dev-dependencies]
gpui2 = { path = "../gpui2", features = ["test-support"] }
rand.workspace = true
[features]
test-support = ["gpui2/test-support"]

1283
crates/fs2/src/fs2.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,417 @@
use anyhow::Result;
use collections::HashMap;
use git2::{BranchType, StatusShow};
use parking_lot::Mutex;
use serde_derive::{Deserialize, Serialize};
use std::{
cmp::Ordering,
ffi::OsStr,
os::unix::prelude::OsStrExt,
path::{Component, Path, PathBuf},
sync::Arc,
time::SystemTime,
};
use sum_tree::{MapSeekTarget, TreeMap};
use util::ResultExt;
pub use git2::Repository as LibGitRepository;
#[derive(Clone, Debug, Hash, PartialEq)]
pub struct Branch {
pub name: Box<str>,
/// Timestamp of most recent commit, normalized to Unix Epoch format.
pub unix_timestamp: Option<i64>,
}
#[async_trait::async_trait]
pub trait GitRepository: Send {
fn reload_index(&self);
fn load_index_text(&self, relative_file_path: &Path) -> Option<String>;
fn branch_name(&self) -> Option<String>;
/// Get the statuses of all of the files in the index that start with the given
/// path and have changes with resepect to the HEAD commit. This is fast because
/// the index stores hashes of trees, so that unchanged directories can be skipped.
fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus>;
/// Get the status of a given file in the working directory with respect to
/// the index. In the common case, when there are no changes, this only requires
/// an index lookup. The index stores the mtime of each file when it was added,
/// so there's no work to do if the mtime matches.
fn unstaged_status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus>;
/// Get the status of a given file in the working directory with respect to
/// the HEAD commit. In the common case, when there are no changes, this only
/// requires an index lookup and blob comparison between the index and the HEAD
/// commit. The index stores the mtime of each file when it was added, so there's
/// no need to consider the working directory file if the mtime matches.
fn status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus>;
fn branches(&self) -> Result<Vec<Branch>>;
fn change_branch(&self, _: &str) -> Result<()>;
fn create_branch(&self, _: &str) -> Result<()>;
}
impl std::fmt::Debug for dyn GitRepository {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("dyn GitRepository<...>").finish()
}
}
impl GitRepository for LibGitRepository {
fn reload_index(&self) {
if let Ok(mut index) = self.index() {
_ = index.read(false);
}
}
fn load_index_text(&self, relative_file_path: &Path) -> Option<String> {
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
const STAGE_NORMAL: i32 = 0;
let index = repo.index()?;
// This check is required because index.get_path() unwraps internally :(
check_path_to_repo_path_errors(relative_file_path)?;
let oid = match index.get_path(&relative_file_path, STAGE_NORMAL) {
Some(entry) => entry.id,
None => return Ok(None),
};
let content = repo.find_blob(oid)?.content().to_owned();
Ok(Some(String::from_utf8(content)?))
}
match logic(&self, relative_file_path) {
Ok(value) => return value,
Err(err) => log::error!("Error loading head text: {:?}", err),
}
None
}
fn branch_name(&self) -> Option<String> {
let head = self.head().log_err()?;
let branch = String::from_utf8_lossy(head.shorthand_bytes());
Some(branch.to_string())
}
fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus> {
let mut map = TreeMap::default();
let mut options = git2::StatusOptions::new();
options.pathspec(path_prefix);
options.show(StatusShow::Index);
if let Some(statuses) = self.statuses(Some(&mut options)).log_err() {
for status in statuses.iter() {
let path = RepoPath(PathBuf::from(OsStr::from_bytes(status.path_bytes())));
let status = status.status();
if !status.contains(git2::Status::IGNORED) {
if let Some(status) = read_status(status) {
map.insert(path, status)
}
}
}
}
map
}
fn unstaged_status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus> {
// If the file has not changed since it was added to the index, then
// there can't be any changes.
if matches_index(self, path, mtime) {
return None;
}
let mut options = git2::StatusOptions::new();
options.pathspec(&path.0);
options.disable_pathspec_match(true);
options.include_untracked(true);
options.recurse_untracked_dirs(true);
options.include_unmodified(true);
options.show(StatusShow::Workdir);
let statuses = self.statuses(Some(&mut options)).log_err()?;
let status = statuses.get(0).and_then(|s| read_status(s.status()));
status
}
fn status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus> {
let mut options = git2::StatusOptions::new();
options.pathspec(&path.0);
options.disable_pathspec_match(true);
options.include_untracked(true);
options.recurse_untracked_dirs(true);
options.include_unmodified(true);
// If the file has not changed since it was added to the index, then
// there's no need to examine the working directory file: just compare
// the blob in the index to the one in the HEAD commit.
if matches_index(self, path, mtime) {
options.show(StatusShow::Index);
}
let statuses = self.statuses(Some(&mut options)).log_err()?;
let status = statuses.get(0).and_then(|s| read_status(s.status()));
status
}
fn branches(&self) -> Result<Vec<Branch>> {
let local_branches = self.branches(Some(BranchType::Local))?;
let valid_branches = local_branches
.filter_map(|branch| {
branch.ok().and_then(|(branch, _)| {
let name = branch.name().ok().flatten().map(Box::from)?;
let timestamp = branch.get().peel_to_commit().ok()?.time();
let unix_timestamp = timestamp.seconds();
let timezone_offset = timestamp.offset_minutes();
let utc_offset =
time::UtcOffset::from_whole_seconds(timezone_offset * 60).ok()?;
let unix_timestamp =
time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?;
Some(Branch {
name,
unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()),
})
})
})
.collect();
Ok(valid_branches)
}
fn change_branch(&self, name: &str) -> Result<()> {
let revision = self.find_branch(name, BranchType::Local)?;
let revision = revision.get();
let as_tree = revision.peel_to_tree()?;
self.checkout_tree(as_tree.as_object(), None)?;
self.set_head(
revision
.name()
.ok_or_else(|| anyhow::anyhow!("Branch name could not be retrieved"))?,
)?;
Ok(())
}
fn create_branch(&self, name: &str) -> Result<()> {
let current_commit = self.head()?.peel_to_commit()?;
self.branch(name, &current_commit, false)?;
Ok(())
}
}
fn matches_index(repo: &LibGitRepository, path: &RepoPath, mtime: SystemTime) -> bool {
if let Some(index) = repo.index().log_err() {
if let Some(entry) = index.get_path(&path, 0) {
if let Some(mtime) = mtime.duration_since(SystemTime::UNIX_EPOCH).log_err() {
if entry.mtime.seconds() == mtime.as_secs() as i32
&& entry.mtime.nanoseconds() == mtime.subsec_nanos()
{
return true;
}
}
}
}
false
}
fn read_status(status: git2::Status) -> Option<GitFileStatus> {
if status.contains(git2::Status::CONFLICTED) {
Some(GitFileStatus::Conflict)
} else if status.intersects(
git2::Status::WT_MODIFIED
| git2::Status::WT_RENAMED
| git2::Status::INDEX_MODIFIED
| git2::Status::INDEX_RENAMED,
) {
Some(GitFileStatus::Modified)
} else if status.intersects(git2::Status::WT_NEW | git2::Status::INDEX_NEW) {
Some(GitFileStatus::Added)
} else {
None
}
}
#[derive(Debug, Clone, Default)]
pub struct FakeGitRepository {
state: Arc<Mutex<FakeGitRepositoryState>>,
}
#[derive(Debug, Clone, Default)]
pub struct FakeGitRepositoryState {
pub index_contents: HashMap<PathBuf, String>,
pub worktree_statuses: HashMap<RepoPath, GitFileStatus>,
pub branch_name: Option<String>,
}
impl FakeGitRepository {
pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<Mutex<dyn GitRepository>> {
Arc::new(Mutex::new(FakeGitRepository { state }))
}
}
#[async_trait::async_trait]
impl GitRepository for FakeGitRepository {
fn reload_index(&self) {}
fn load_index_text(&self, path: &Path) -> Option<String> {
let state = self.state.lock();
state.index_contents.get(path).cloned()
}
fn branch_name(&self) -> Option<String> {
let state = self.state.lock();
state.branch_name.clone()
}
fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus> {
let mut map = TreeMap::default();
let state = self.state.lock();
for (repo_path, status) in state.worktree_statuses.iter() {
if repo_path.0.starts_with(path_prefix) {
map.insert(repo_path.to_owned(), status.to_owned());
}
}
map
}
fn unstaged_status(&self, _path: &RepoPath, _mtime: SystemTime) -> Option<GitFileStatus> {
None
}
fn status(&self, path: &RepoPath, _mtime: SystemTime) -> Option<GitFileStatus> {
let state = self.state.lock();
state.worktree_statuses.get(path).cloned()
}
fn branches(&self) -> Result<Vec<Branch>> {
Ok(vec![])
}
fn change_branch(&self, name: &str) -> Result<()> {
let mut state = self.state.lock();
state.branch_name = Some(name.to_owned());
Ok(())
}
fn create_branch(&self, name: &str) -> Result<()> {
let mut state = self.state.lock();
state.branch_name = Some(name.to_owned());
Ok(())
}
}
fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {
match relative_file_path.components().next() {
None => anyhow::bail!("repo path should not be empty"),
Some(Component::Prefix(_)) => anyhow::bail!(
"repo path `{}` should be relative, not a windows prefix",
relative_file_path.to_string_lossy()
),
Some(Component::RootDir) => {
anyhow::bail!(
"repo path `{}` should be relative",
relative_file_path.to_string_lossy()
)
}
Some(Component::CurDir) => {
anyhow::bail!(
"repo path `{}` should not start with `.`",
relative_file_path.to_string_lossy()
)
}
Some(Component::ParentDir) => {
anyhow::bail!(
"repo path `{}` should not start with `..`",
relative_file_path.to_string_lossy()
)
}
_ => Ok(()),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GitFileStatus {
Added,
Modified,
Conflict,
}
impl GitFileStatus {
pub fn merge(
this: Option<GitFileStatus>,
other: Option<GitFileStatus>,
prefer_other: bool,
) -> Option<GitFileStatus> {
if prefer_other {
return other;
} else {
match (this, other) {
(Some(GitFileStatus::Conflict), _) | (_, Some(GitFileStatus::Conflict)) => {
Some(GitFileStatus::Conflict)
}
(Some(GitFileStatus::Modified), _) | (_, Some(GitFileStatus::Modified)) => {
Some(GitFileStatus::Modified)
}
(Some(GitFileStatus::Added), _) | (_, Some(GitFileStatus::Added)) => {
Some(GitFileStatus::Added)
}
_ => None,
}
}
}
}
#[derive(Clone, Debug, Ord, Hash, PartialOrd, Eq, PartialEq)]
pub struct RepoPath(pub PathBuf);
impl RepoPath {
pub fn new(path: PathBuf) -> Self {
debug_assert!(path.is_relative(), "Repo paths must be relative");
RepoPath(path)
}
}
impl From<&Path> for RepoPath {
fn from(value: &Path) -> Self {
RepoPath::new(value.to_path_buf())
}
}
impl From<PathBuf> for RepoPath {
fn from(value: PathBuf) -> Self {
RepoPath::new(value)
}
}
impl Default for RepoPath {
fn default() -> Self {
RepoPath(PathBuf::new())
}
}
impl AsRef<Path> for RepoPath {
fn as_ref(&self) -> &Path {
self.0.as_ref()
}
}
impl std::ops::Deref for RepoPath {
type Target = PathBuf;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug)]
pub struct RepoPathDescendants<'a>(pub &'a Path);
impl<'a> MapSeekTarget<RepoPath> for RepoPathDescendants<'a> {
fn cmp_cursor(&self, key: &RepoPath) -> Ordering {
if key.starts_with(&self.0) {
Ordering::Greater
} else {
self.0.cmp(key)
}
}
}

View file

@ -39,72 +39,11 @@ pub struct App(Arc<Mutex<AppContext>>);
impl App {
pub fn production(asset_source: Arc<dyn AssetSource>) -> Self {
let http_client = http::client();
Self::new(current_platform(), asset_source, http_client)
}
#[cfg(any(test, feature = "test-support"))]
pub fn test(seed: u64) -> Self {
let platform = Arc::new(crate::TestPlatform::new(seed));
let asset_source = Arc::new(());
let http_client = util::http::FakeHttpClient::with_404_response();
Self::new(platform, asset_source, http_client)
}
fn new(
platform: Arc<dyn Platform>,
asset_source: Arc<dyn AssetSource>,
http_client: Arc<dyn HttpClient>,
) -> Self {
let executor = platform.executor();
assert!(
executor.is_main_thread(),
"must construct App on main thread"
);
let text_system = Arc::new(TextSystem::new(platform.text_system()));
let mut entities = EntityMap::new();
let unit_entity = entities.insert(entities.reserve(), ());
let app_metadata = AppMetadata {
os_name: platform.os_name(),
os_version: platform.os_version().ok(),
app_version: platform.app_version().ok(),
};
Self(Arc::new_cyclic(|this| {
Mutex::new(AppContext {
this: this.clone(),
text_system,
platform: MainThreadOnly::new(platform, executor.clone()),
app_metadata,
flushing_effects: false,
pending_updates: 0,
next_frame_callbacks: Default::default(),
executor,
svg_renderer: SvgRenderer::new(asset_source.clone()),
asset_source,
image_cache: ImageCache::new(http_client),
text_style_stack: Vec::new(),
globals_by_type: HashMap::default(),
unit_entity,
entities,
windows: SlotMap::with_key(),
keymap: Arc::new(RwLock::new(Keymap::default())),
global_action_listeners: HashMap::default(),
action_builders: HashMap::default(),
pending_effects: VecDeque::new(),
pending_notifications: HashSet::default(),
pending_global_notifications: HashSet::default(),
observers: SubscriberSet::new(),
event_listeners: SubscriberSet::new(),
release_listeners: SubscriberSet::new(),
global_observers: SubscriberSet::new(),
quit_observers: SubscriberSet::new(),
layout_id_buffer: Default::default(),
propagate_event: true,
active_drag: None,
})
}))
Self(AppContext::new(
current_platform(),
asset_source,
http::client(),
))
}
pub fn run<F>(self, on_finish_launching: F)
@ -210,6 +149,62 @@ pub struct AppContext {
}
impl AppContext {
pub(crate) fn new(
platform: Arc<dyn Platform>,
asset_source: Arc<dyn AssetSource>,
http_client: Arc<dyn HttpClient>,
) -> Arc<Mutex<Self>> {
let executor = platform.executor();
assert!(
executor.is_main_thread(),
"must construct App on main thread"
);
let text_system = Arc::new(TextSystem::new(platform.text_system()));
let mut entities = EntityMap::new();
let unit_entity = entities.insert(entities.reserve(), ());
let app_metadata = AppMetadata {
os_name: platform.os_name(),
os_version: platform.os_version().ok(),
app_version: platform.app_version().ok(),
};
Arc::new_cyclic(|this| {
Mutex::new(AppContext {
this: this.clone(),
text_system,
platform: MainThreadOnly::new(platform, executor.clone()),
app_metadata,
flushing_effects: false,
pending_updates: 0,
next_frame_callbacks: Default::default(),
executor,
svg_renderer: SvgRenderer::new(asset_source.clone()),
asset_source,
image_cache: ImageCache::new(http_client),
text_style_stack: Vec::new(),
globals_by_type: HashMap::default(),
unit_entity,
entities,
windows: SlotMap::with_key(),
keymap: Arc::new(RwLock::new(Keymap::default())),
global_action_listeners: HashMap::default(),
action_builders: HashMap::default(),
pending_effects: VecDeque::new(),
pending_notifications: HashSet::default(),
pending_global_notifications: HashSet::default(),
observers: SubscriberSet::new(),
event_listeners: SubscriberSet::new(),
release_listeners: SubscriberSet::new(),
global_observers: SubscriberSet::new(),
quit_observers: SubscriberSet::new(),
layout_id_buffer: Default::default(),
propagate_event: true,
active_drag: None,
})
})
}
pub fn quit(&mut self) {
let mut futures = Vec::new();

View file

@ -68,6 +68,7 @@ impl EntityMap {
/// Move an entity to the stack.
pub fn lease<'a, T>(&mut self, handle: &'a Handle<T>) -> Lease<'a, T> {
self.assert_valid_context(handle);
let entity = Some(
self.entities
.remove(handle.entity_id)
@ -87,9 +88,17 @@ impl EntityMap {
}
pub fn read<T: 'static>(&self, handle: &Handle<T>) -> &T {
self.assert_valid_context(handle);
self.entities[handle.entity_id].downcast_ref().unwrap()
}
fn assert_valid_context(&self, handle: &AnyHandle) {
debug_assert!(
Weak::ptr_eq(&handle.entity_map, &Arc::downgrade(&self.ref_counts)),
"used a handle with the wrong context"
);
}
pub fn take_dropped(&mut self) -> Vec<(EntityId, AnyBox)> {
let dropped_entity_ids = mem::take(&mut self.ref_counts.write().dropped_entity_ids);
dropped_entity_ids

View file

@ -1,6 +1,6 @@
use crate::{
AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, Handle, MainThread,
ModelContext, Result, Task, WindowContext,
ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
};
use parking_lot::Mutex;
use std::{any::Any, future::Future, sync::Arc};
@ -37,6 +37,17 @@ impl Context for TestAppContext {
}
impl TestAppContext {
pub fn new(dispatcher: TestDispatcher) -> Self {
let executor = Executor::new(Arc::new(dispatcher));
let platform = Arc::new(TestPlatform::new(executor.clone()));
let asset_source = Arc::new(());
let http_client = util::http::FakeHttpClient::with_404_response();
Self {
app: AppContext::new(platform, asset_source, http_client),
executor,
}
}
pub fn refresh(&mut self) -> Result<()> {
let mut lock = self.app.lock();
lock.refresh();
@ -47,27 +58,27 @@ impl TestAppContext {
&self.executor
}
pub fn update<R>(&self, f: impl FnOnce(&mut AppContext) -> R) -> Result<R> {
pub fn update<R>(&self, f: impl FnOnce(&mut AppContext) -> R) -> R {
let mut lock = self.app.lock();
Ok(f(&mut *lock))
f(&mut *lock)
}
pub fn read_window<R>(
&self,
handle: AnyWindowHandle,
update: impl FnOnce(&WindowContext) -> R,
) -> Result<R> {
read: impl FnOnce(&WindowContext) -> R,
) -> R {
let mut app_context = self.app.lock();
app_context.read_window(handle.id, update)
app_context.read_window(handle.id, read).unwrap()
}
pub fn update_window<R>(
&self,
handle: AnyWindowHandle,
update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<R> {
) -> R {
let mut app = self.app.lock();
app.update_window(handle.id, update)
app.update_window(handle.id, update).unwrap()
}
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
@ -94,22 +105,22 @@ impl TestAppContext {
pub fn run_on_main<R>(
&self,
f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static,
) -> Result<Task<R>>
) -> Task<R>
where
R: Send + 'static,
{
let mut app_context = self.app.lock();
Ok(app_context.run_on_main(f))
app_context.run_on_main(f)
}
pub fn has_global<G: 'static>(&self) -> Result<bool> {
pub fn has_global<G: 'static>(&self) -> bool {
let lock = self.app.lock();
Ok(lock.has_global::<G>())
lock.has_global::<G>()
}
pub fn read_global<G: 'static, R>(&self, read: impl FnOnce(&G, &AppContext) -> R) -> Result<R> {
pub fn read_global<G: 'static, R>(&self, read: impl FnOnce(&G, &AppContext) -> R) -> R {
let lock = self.app.lock();
Ok(read(lock.global(), &lock))
read(lock.global(), &lock)
}
pub fn try_read_global<G: 'static, R>(
@ -123,9 +134,9 @@ impl TestAppContext {
pub fn update_global<G: 'static, R>(
&mut self,
update: impl FnOnce(&mut G, &mut AppContext) -> R,
) -> Result<R> {
) -> R {
let mut lock = self.app.lock();
Ok(lock.update_global(update))
lock.update_global(update)
}
fn to_async(&self) -> AsyncAppContext {

View file

@ -207,8 +207,8 @@ impl Executor {
}
#[cfg(any(test, feature = "test-support"))]
pub async fn simulate_random_delay(&self) {
todo!("simulate_random_delay")
pub fn simulate_random_delay(&self) -> impl Future<Output = ()> {
self.dispatcher.as_test().unwrap().simulate_random_delay()
}
pub fn num_cpus(&self) -> usize {

View file

@ -162,8 +162,11 @@ pub trait PlatformDispatcher: Send + Sync {
fn dispatch_on_main_thread(&self, runnable: Runnable);
fn dispatch_after(&self, duration: Duration, runnable: Runnable);
fn poll(&self) -> bool;
#[cfg(any(test, feature = "test-support"))]
fn advance_clock(&self, duration: Duration);
fn as_test(&self) -> Option<&TestDispatcher> {
None
}
}
pub trait PlatformTextSystem: Send + Sync {

View file

@ -71,11 +71,6 @@ impl PlatformDispatcher for MacDispatcher {
fn poll(&self) -> bool {
false
}
#[cfg(any(test, feature = "test-support"))]
fn advance_clock(&self, _: Duration) {
unimplemented!()
}
}
extern "C" fn trampoline(runnable: *mut c_void) {

View file

@ -4,7 +4,10 @@ use collections::{BTreeMap, HashMap, VecDeque};
use parking_lot::Mutex;
use rand::prelude::*;
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
time::{Duration, Instant},
};
use util::post_inc;
@ -44,6 +47,34 @@ impl TestDispatcher {
state: Arc::new(Mutex::new(state)),
}
}
pub fn advance_clock(&self, by: Duration) {
self.state.lock().time += by;
}
pub fn simulate_random_delay(&self) -> impl Future<Output = ()> {
pub struct YieldNow {
count: usize,
}
impl Future for YieldNow {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
if self.count > 0 {
self.count -= 1;
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
}
}
YieldNow {
count: self.state.lock().random.gen_range(0..10),
}
}
}
impl Clone for TestDispatcher {
@ -131,8 +162,8 @@ impl PlatformDispatcher for TestDispatcher {
true
}
fn advance_clock(&self, by: Duration) {
self.state.lock().time += by;
fn as_test(&self) -> Option<&TestDispatcher> {
Some(self)
}
}

View file

@ -1,5 +1,5 @@
use crate::{DisplayId, Executor, Platform, PlatformTextSystem, TestDispatcher};
use rand::prelude::*;
use crate::{DisplayId, Executor, Platform, PlatformTextSystem};
use anyhow::{anyhow, Result};
use std::sync::Arc;
pub struct TestPlatform {
@ -7,11 +7,8 @@ pub struct TestPlatform {
}
impl TestPlatform {
pub fn new(seed: u64) -> Self {
let rng = StdRng::seed_from_u64(seed);
TestPlatform {
executor: Executor::new(Arc::new(TestDispatcher::new(rng))),
}
pub fn new(executor: Executor) -> Self {
TestPlatform { executor }
}
}
@ -136,18 +133,18 @@ impl Platform for TestPlatform {
}
fn os_name(&self) -> &'static str {
unimplemented!()
"test"
}
fn os_version(&self) -> anyhow::Result<crate::SemanticVersion> {
unimplemented!()
fn os_version(&self) -> Result<crate::SemanticVersion> {
Err(anyhow!("os_version called on TestPlatform"))
}
fn app_version(&self) -> anyhow::Result<crate::SemanticVersion> {
unimplemented!()
fn app_version(&self) -> Result<crate::SemanticVersion> {
Err(anyhow!("app_version called on TestPlatform"))
}
fn app_path(&self) -> anyhow::Result<std::path::PathBuf> {
fn app_path(&self) -> Result<std::path::PathBuf> {
unimplemented!()
}
@ -155,7 +152,7 @@ impl Platform for TestPlatform {
unimplemented!()
}
fn path_for_auxiliary_executable(&self, _name: &str) -> anyhow::Result<std::path::PathBuf> {
fn path_for_auxiliary_executable(&self, _name: &str) -> Result<std::path::PathBuf> {
unimplemented!()
}
@ -175,20 +172,15 @@ impl Platform for TestPlatform {
unimplemented!()
}
fn write_credentials(
&self,
_url: &str,
_username: &str,
_password: &[u8],
) -> anyhow::Result<()> {
fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Result<()> {
unimplemented!()
}
fn read_credentials(&self, _url: &str) -> anyhow::Result<Option<(String, Vec<u8>)>> {
fn read_credentials(&self, _url: &str) -> Result<Option<(String, Vec<u8>)>> {
unimplemented!()
}
fn delete_credentials(&self, _url: &str) -> anyhow::Result<()> {
fn delete_credentials(&self, _url: &str) -> Result<()> {
unimplemented!()
}
}

View file

@ -139,11 +139,15 @@ impl Boundary {
#[cfg(test)]
mod tests {
use super::*;
use crate::{font, App};
use crate::{font, TestAppContext, TestDispatcher};
use rand::prelude::*;
#[test]
fn test_wrap_line() {
App::test(0).run(|cx| {
let dispatcher = TestDispatcher::new(StdRng::seed_from_u64(0));
let cx = TestAppContext::new(dispatcher);
cx.update(|cx| {
let text_system = cx.text_system().clone();
let mut wrapper = LineWrapper::new(
text_system.font_id(&font("Courier")).unwrap(),

View file

@ -16,7 +16,7 @@ collections = { path = "../collections" }
gpui2 = { path = "../gpui2" }
sqlez = { path = "../sqlez" }
fs = { path = "../fs" }
feature_flags = { path = "../feature_flags" }
feature_flags2 = { path = "../feature_flags2" }
util = { path = "../util" }
anyhow.workspace = true