From 0a8e2f6bb0720f57944e482930cc827785ad851d Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Tue, 11 Oct 2022 13:03:36 -0700 Subject: [PATCH] Moved fs to it's own crate, build failing due to cyclic dependency on rope --- Cargo.lock | 16 ++++++++ crates/fs/Cargo.toml | 18 +++++++++ crates/{project => fs}/src/fs.rs | 59 +++++++++++++++++++++++++++- crates/{git => fs}/src/repository.rs | 0 crates/text/Cargo.toml | 1 + crates/text/src/text.rs | 56 -------------------------- crates/zed/src/settings_file.rs | 4 ++ 7 files changed, 96 insertions(+), 58 deletions(-) create mode 100644 crates/fs/Cargo.toml rename crates/{project => fs}/src/fs.rs (96%) rename crates/{git => fs}/src/repository.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index cea25c027a..408758f7ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1984,6 +1984,22 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "fs" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "collections", + "fsevent", + "futures", + "git", + "parking_lot 0.11.2", + "smol", + "text", + "util", +] + [[package]] name = "fs-set-times" version = "0.15.0" diff --git a/crates/fs/Cargo.toml b/crates/fs/Cargo.toml new file mode 100644 index 0000000000..1a4e325fa4 --- /dev/null +++ b/crates/fs/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "fs" +version = "0.1.0" +edition = "2021" + +[lib] +path = "src/fs.rs" + +[dependencies] +collections = { path = "../collections" } +fsevent = { path = "../fsevent" } +anyhow = "1.0.57" +async-trait = "0.1" +futures = "0.3" +parking_lot = "0.11.1" +smol = "1.2.5" +text = { path = "../text" } +util = { path = "../util" } diff --git a/crates/project/src/fs.rs b/crates/fs/src/fs.rs similarity index 96% rename from crates/project/src/fs.rs rename to crates/fs/src/fs.rs index a9a0a1707f..c24387844c 100644 --- a/crates/project/src/fs.rs +++ b/crates/fs/src/fs.rs @@ -1,8 +1,8 @@ +pub mod repository; + use anyhow::{anyhow, Result}; use fsevent::EventStream; use futures::{future::BoxFuture, Stream, StreamExt}; -use git::repository::{GitRepository, LibGitRepository}; -use language::LineEnding; use parking_lot::Mutex as SyncMutex; use smol::io::{AsyncReadExt, AsyncWriteExt}; use std::sync::Arc; @@ -25,6 +25,61 @@ use git::repository::FakeGitRepositoryState; #[cfg(any(test, feature = "test-support"))] use std::sync::Weak; +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum LineEnding { + Unix, + Windows, +} + +impl Default for LineEnding { + fn default() -> Self { + #[cfg(unix)] + return Self::Unix; + + #[cfg(not(unix))] + return Self::CRLF; + } +} + +impl LineEnding { + pub fn as_str(&self) -> &'static str { + match self { + LineEnding::Unix => "\n", + LineEnding::Windows => "\r\n", + } + } + + pub fn detect(text: &str) -> Self { + let mut max_ix = cmp::min(text.len(), 1000); + while !text.is_char_boundary(max_ix) { + max_ix -= 1; + } + + if let Some(ix) = text[..max_ix].find(&['\n']) { + if ix > 0 && text.as_bytes()[ix - 1] == b'\r' { + Self::Windows + } else { + Self::Unix + } + } else { + Self::default() + } + } + + pub fn normalize(text: &mut String) { + if let Cow::Owned(replaced) = CARRIAGE_RETURNS_REGEX.replace_all(text, "\n") { + *text = replaced; + } + } + + fn normalize_arc(text: Arc) -> Arc { + if let Cow::Owned(replaced) = CARRIAGE_RETURNS_REGEX.replace_all(&text, "\n") { + replaced.into() + } else { + text + } + } +} #[async_trait::async_trait] pub trait Fs: Send + Sync { async fn create_dir(&self, path: &Path) -> Result<()>; diff --git a/crates/git/src/repository.rs b/crates/fs/src/repository.rs similarity index 100% rename from crates/git/src/repository.rs rename to crates/fs/src/repository.rs diff --git a/crates/text/Cargo.toml b/crates/text/Cargo.toml index 4fc09eff46..0d47525021 100644 --- a/crates/text/Cargo.toml +++ b/crates/text/Cargo.toml @@ -13,6 +13,7 @@ test-support = ["rand"] [dependencies] clock = { path = "../clock" } collections = { path = "../collections" } +fs = { path = "../fs" } sum_tree = { path = "../sum_tree" } anyhow = "1.0.38" arrayvec = "0.7.1" diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index d201f87443..52859b7515 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -96,12 +96,6 @@ pub struct Transaction { pub start: clock::Global, } -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum LineEnding { - Unix, - Windows, -} - impl HistoryEntry { pub fn transaction_id(&self) -> TransactionId { self.transaction.id @@ -2370,56 +2364,6 @@ impl operation_queue::Operation for Operation { } } -impl Default for LineEnding { - fn default() -> Self { - #[cfg(unix)] - return Self::Unix; - - #[cfg(not(unix))] - return Self::CRLF; - } -} - -impl LineEnding { - pub fn as_str(&self) -> &'static str { - match self { - LineEnding::Unix => "\n", - LineEnding::Windows => "\r\n", - } - } - - pub fn detect(text: &str) -> Self { - let mut max_ix = cmp::min(text.len(), 1000); - while !text.is_char_boundary(max_ix) { - max_ix -= 1; - } - - if let Some(ix) = text[..max_ix].find(&['\n']) { - if ix > 0 && text.as_bytes()[ix - 1] == b'\r' { - Self::Windows - } else { - Self::Unix - } - } else { - Self::default() - } - } - - pub fn normalize(text: &mut String) { - if let Cow::Owned(replaced) = CARRIAGE_RETURNS_REGEX.replace_all(text, "\n") { - *text = replaced; - } - } - - fn normalize_arc(text: Arc) -> Arc { - if let Cow::Owned(replaced) = CARRIAGE_RETURNS_REGEX.replace_all(&text, "\n") { - replaced.into() - } else { - text - } - } -} - pub trait ToOffset { fn to_offset(&self, snapshot: &BufferSnapshot) -> usize; } diff --git a/crates/zed/src/settings_file.rs b/crates/zed/src/settings_file.rs index 14c9f63e95..25bd065b47 100644 --- a/crates/zed/src/settings_file.rs +++ b/crates/zed/src/settings_file.rs @@ -12,6 +12,10 @@ use util::ResultExt; #[derive(Clone)] pub struct WatchedJsonFile(pub watch::Receiver); +// 1) Do the refactoring to pull WatchedJSON and fs out and into everything else +// 2) Scaffold this by making the basic structs we'll need SettingsFile::atomic_write_theme() +// 3) Fix the overeager settings writing, if that works, and there's no data loss, call it? + impl WatchedJsonFile where T: 'static + for<'de> Deserialize<'de> + Clone + Default + Send + Sync,